Keycloak: REST url for custom endpoint
Asked Answered
W

2

10

Implementing a custom rest endpoint in keycloak I used these example:

https://github.com/keycloak/keycloak/tree/master/examples/providers/domain-extension/src/main/java/org/keycloak/examples/domainextension/rest

After embedding the provider to keycloak it's loaded during keycloak startup. Guess that's fine. In server info I can see the the endpoint as well.

Problem:

How may I call that endpoint?

Do I need to registrate the endpoint or mount it on a client?

(If so which settings does the client need (admin rights etc...)

What is the URL for calling the endpoint?

Windermere answered 27/2, 2019 at 7:56 Comment(1)
although you linked your example sources it is not quite a MCVE, i think you would attract many more people if you were to post an actual, full example - preferrably one which is downloadable with only a few clicks / one click and is able to run on any standard IDENanny
M
14

Your don't need register or mount your endpoint. the effective url is calculated with the given id in your ProviderFactory.

{{keycloakUrl}}/auth/realms/{{reamlName}}/{{id in providerFactory}}/...

For your example the url is

{{keycloakUrl}}/auth/realms/{{realmName}}/example/companies
Microfilm answered 28/2, 2019 at 7:35 Comment(0)
M
0

Extending from Patrick Hesse's answer

For the latest versions of Keycloak (24.0.2 as of July 2024)

prefix auth has been dropped, it now looks like this

Format

{{keycloakUrl}}/realms/{{reamlName}}/{{id in providerFactory}}/...

Example

http://localhost/realms/master/otp-validator/validate

Sample Realm Resource Provider Factory

public class GoodRealmResourceProviderFactory implements RealmResourceProviderFactory, RealmResourceProvider {

    public static final String ID = "otp-validator";

    private KeycloakSession session;

    public RealmResourceProvider create(KeycloakSession session) {
        this.session = session;
        return this;
    }

    public void init(Config.Scope config) {
        // NOOP
    }

    public void postInit(KeycloakSessionFactory factory) {
        // NOOP
    }

    public void close() {
        // NOOP
    }

    public String getId() {
        return ID;
    }

    public Object getResource() {
        return new GoodRealmResourceProvider(session);
    }
}

Resource Provider

public class GoodRealmResourceProvider implements RealmResourceProvider {

    private final KeycloakSession session;

    public GoodRealmResourceProvider(KeycloakSession session) {
        this.session = session;
    }

    @Override
    public Object getResource() {
        return this;
    }

    @GET
    @Path("/validate")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCustomMessage() {
        System.out.println("something amaziiing {}: "+session.getContext().getUri());
        return "{\"message\": \"Hello from custom endpoint!\"}";
    }

    @Override
    public void close() {
        // Cleanup resources if necessary
    }
Mojave answered 26/7 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.