Prifulnath
Prifulnath

Reputation: 567

Generate token from Keycloak SPI

I am trying to generate token with client id and client secret from an custom keycloak API, this token will further used by other APIs to communicate with keycloak admin APIs.

So I have two options. One is using keycloak API to generate token. But token generated from keycloak SPI, issuer is localhost:8080 which cannot be used by the API in the other domain.

And the second option is using keycloak functions to generate token. So I gone through the keycloak source code and found some methods like below,

TokenManager tokenManager = new TokenManager();
AccessToken token = tokenManager.createClientAccessToken(session, realm, client, null, null, null);

and

  EventBuilder event = new EventBuilder(session.getContext().getRealm(), session,
          session.getContext().getConnection());
  AccessTokenResponseBuilder responseBuilder = tokenManager
          .responseBuilder(realm, client, event, session, this.authResult.getSession(), null)
          .generateAccessToken();
  responseBuilder.getAccessToken().setAllowedOrigins(this.authResult.getToken().getAllowedOrigins());
  AccessTokenResponse atr = responseBuilder.build();
  String accessToken = atr.getToken();

But both the code requires user or user session, but I need client based token.

Can anyone suggest me a better option?

Upvotes: 2

Views: 241

Answers (0)

Related Questions