218 AccessToken accessToken = null;
221 String subject = representation.getUserId();
223 UserSessionModel userSession = null;
224 if (subject != null) {
225 UserModel userModel = keycloakSession.users().getUserById(subject, realm);
227 if (userModel != null) {
228 String clientId = representation.getClientId();
230 if (clientId == null) {
234 if (clientId != null) {
235 ClientModel clientModel = realm.getClientById(clientId);
237 AuthenticationSessionModel authSession = keycloakSession.authenticationSessions().createRootAuthenticationSession(realm)
238 .createAuthenticationSession(clientModel);
239 authSession.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
240 authSession.setAuthenticatedUser(userModel);
241 userSession = keycloakSession.sessions().createUserSession(authSession.getParentSession().getId(), realm, userModel, userModel.getUsername(),
"127.0.0.1",
"passwd",
false, null, null);
243 AuthenticationManager.setClientScopesInSession(authSession);
244 ClientSessionContext clientSessionCtx = TokenManager.attachAuthenticationSession(keycloakSession, userSession, authSession);
246 accessToken =
new TokenManager().createClientAccessToken(keycloakSession, realm, clientModel, userModel, userSession, clientSessionCtx);
251 if (accessToken == null) {
252 accessToken =
new AccessToken();
254 accessToken.subject(representation.getUserId());
255 accessToken.issuedFor(representation.getClientId());
256 accessToken.audience(representation.getClientId());
257 accessToken.issuer(Urls.realmIssuer(keycloakSession.getContext().getUri().getBaseUri(), realm.getName()));
258 accessToken.setRealmAccess(
new AccessToken.Access());
262 AccessToken.Access realmAccess = accessToken.getRealmAccess();
264 if (representation.getRoleIds() != null) {
265 representation.getRoleIds().forEach(roleName -> realmAccess.addRole(roleName));
268 return new CloseableKeycloakIdentity(accessToken, keycloakSession, userSession);
KeycloakContext getContext()
KeycloakSession getKeycloakSession()
Definition: AuthorizationProvider.java:165
final ResourceServer resourceServer
Definition: PolicyEvaluationService.java:86
final AuthorizationProvider authorization
Definition: PolicyEvaluationService.java:84