KEYCLOAK-1070 add setProtocolMappers/getProtocolMappers to ClientSession
This commit is contained in:
1 parent
dae219d32e
commit
535023d800
27 files changed
+345
-70
No files matched your search
@@ -27,6 +27,7 @@ import org.keycloak.representations.AccessTokenResponse;
|
||||
import org.keycloak.representations.IDToken;
|
||||
import org.keycloak.representations.RefreshToken;
|
||||
import org.keycloak.services.managers.AuthenticationManager;
|
||||
import org.keycloak.services.managers.ClientSessionCode;
|
||||
import org.keycloak.util.Time;
|
||||
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
@@ -208,6 +209,14 @@ public class TokenManager {
|
||||
requestedRoles.add(r.getId());
|
||||
}
|
||||
clientSession.setRoles(requestedRoles);
|
||||
|
||||
Set<String> requestedProtocolMappers = new HashSet<String>();
|
||||
for (ProtocolMapperModel protocolMapper : clientSession.getClient().getProtocolMappers()) {
|
||||
if (protocolMapper.getProtocol().equals(clientSession.getAuthMethod())) {
|
||||
requestedProtocolMappers.add(protocolMapper.getId());
|
||||
}
|
||||
}
|
||||
clientSession.setProtocolMappers(requestedProtocolMappers);
|
||||
}
|
||||
|
||||
public static void dettachClientSession(UserSessionProvider sessions, RealmModel realm, ClientSessionModel clientSession) {
|
||||
@@ -218,6 +227,7 @@ public class TokenManager {
|
||||
|
||||
clientSession.setUserSession(null);
|
||||
clientSession.setRoles(null);
|
||||
clientSession.setProtocolMappers(null);
|
||||
|
||||
if (userSession.getClientSessions().isEmpty()) {
|
||||
sessions.removeUserSession(realm, userSession);
|
||||
@@ -274,23 +284,20 @@ public class TokenManager {
|
||||
|
||||
public AccessToken transformAccessToken(KeycloakSession session, AccessToken token, RealmModel realm, ClientModel client, UserModel user,
|
||||
UserSessionModel userSession, ClientSessionModel clientSession) {
|
||||
Set<ProtocolMapperModel> mappings = client.getProtocolMappers();
|
||||
Set<ProtocolMapperModel> mappings = new ClientSessionCode(realm, clientSession).getRequestedProtocolMappers();
|
||||
KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
|
||||
for (ProtocolMapperModel mapping : mappings) {
|
||||
if (!mapping.getProtocol().equals(OIDCLoginProtocol.LOGIN_PROTOCOL)) continue;
|
||||
|
||||
ProtocolMapper mapper = (ProtocolMapper)sessionFactory.getProviderFactory(ProtocolMapper.class, mapping.getProtocolMapper());
|
||||
if (mapper == null || !(mapper instanceof OIDCAccessTokenMapper)) continue;
|
||||
token = ((OIDCAccessTokenMapper)mapper).transformAccessToken(token, mapping, session, userSession, clientSession);
|
||||
|
||||
|
||||
|
||||
}
|
||||
return token;
|
||||
}
|
||||
public void transformIDToken(KeycloakSession session, IDToken token, RealmModel realm, ClientModel client, UserModel user,
|
||||
UserSessionModel userSession, ClientSessionModel clientSession) {
|
||||
Set<ProtocolMapperModel> mappings = client.getProtocolMappers();
|
||||
Set<ProtocolMapperModel> mappings = new ClientSessionCode(realm, clientSession).getRequestedProtocolMappers();
|
||||
KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
|
||||
for (ProtocolMapperModel mapping : mappings) {
|
||||
if (!mapping.getProtocol().equals(OIDCLoginProtocol.LOGIN_PROTOCOL)) continue;
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.keycloak.events.Details;
|
||||
import org.keycloak.events.EventBuilder;
|
||||
import org.keycloak.events.EventType;
|
||||
import org.keycloak.models.ClientModel;
|
||||
import org.keycloak.models.ClientSessionModel;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.UserModel;
|
||||
@@ -122,10 +123,11 @@ public class UserInfoEndpoint {
|
||||
}
|
||||
|
||||
UserSessionModel userSession = session.sessions().getUserSession(realm, token.getSessionState());
|
||||
ClientSessionModel clientSession = session.sessions().getClientSession(token.getClientSession());
|
||||
ClientModel clientModel = realm.getClientByClientId(token.getIssuedFor());
|
||||
UserModel userModel = userSession.getUser();
|
||||
AccessToken userInfo = new AccessToken();
|
||||
tokenManager.transformAccessToken(session, userInfo, realm, clientModel, userModel, userSession, null);
|
||||
tokenManager.transformAccessToken(session, userInfo, realm, clientModel, userModel, userSession, clientSession);
|
||||
|
||||
event
|
||||
.detail(Details.USERNAME, userModel.getUsername())
|
||||
|
||||
@@ -439,8 +439,8 @@ public class AuthenticationManager {
|
||||
}
|
||||
|
||||
List<ProtocolMapperModel> protocolMappers = new LinkedList<ProtocolMapperModel>();
|
||||
for (ProtocolMapperModel protocolMapper : client.getProtocolMappers()) {
|
||||
if (protocolMapper.isConsentRequired() && protocolMapper.getProtocol().equals(clientSession.getAuthMethod()) && protocolMapper.getConsentText() != null) {
|
||||
for (ProtocolMapperModel protocolMapper : accessCode.getRequestedProtocolMappers()) {
|
||||
if (protocolMapper.isConsentRequired() && protocolMapper.getConsentText() != null) {
|
||||
if (grantedConsent == null || !grantedConsent.isProtocolMapperGranted(protocolMapper)) {
|
||||
protocolMappers.add(protocolMapper);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.keycloak.services.managers;
|
||||
|
||||
import org.keycloak.models.ClientSessionModel;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.ProtocolMapperModel;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.RoleModel;
|
||||
import org.keycloak.models.UserModel.RequiredAction;
|
||||
@@ -112,12 +113,25 @@ public class ClientSessionCode {
|
||||
for (String roleId : clientSession.getRoles()) {
|
||||
RoleModel role = realm.getRoleById(roleId);
|
||||
if (role != null) {
|
||||
requestedRoles.add(realm.getRoleById(roleId));
|
||||
requestedRoles.add(role);
|
||||
}
|
||||
}
|
||||
return requestedRoles;
|
||||
}
|
||||
|
||||
public Set<ProtocolMapperModel> getRequestedProtocolMappers() {
|
||||
Set<ProtocolMapperModel> requestedProtocolMappers = new HashSet<ProtocolMapperModel>();
|
||||
if (clientSession.getProtocolMappers() != null) {
|
||||
for (String protocolMapperId : clientSession.getProtocolMappers()) {
|
||||
ProtocolMapperModel protocolMapper = clientSession.getClient().getProtocolMapperById(protocolMapperId);
|
||||
if (protocolMapper != null) {
|
||||
requestedProtocolMappers.add(protocolMapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
return requestedProtocolMappers;
|
||||
}
|
||||
|
||||
public void setAction(ClientSessionModel.Action action) {
|
||||
clientSession.setAction(action);
|
||||
clientSession.setNote(ACTION_KEY, UUID.randomUUID().toString());
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.keycloak.jose.jws.JWSBuilder;
|
||||
import org.keycloak.login.LoginFormsProvider;
|
||||
import org.keycloak.models.ClientModel;
|
||||
import org.keycloak.models.ClientSessionModel;
|
||||
import org.keycloak.models.RoleModel;
|
||||
import org.keycloak.models.UserConsentModel;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.ModelException;
|
||||
@@ -611,16 +612,15 @@ public class LoginActionsService {
|
||||
|
||||
UserConsentModel grantedConsent = user.getConsentByClient(client.getId());
|
||||
if (grantedConsent == null) {
|
||||
grantedConsent = new UserConsentModel(realm, client.getId());
|
||||
grantedConsent = new UserConsentModel(client);
|
||||
user.addConsent(grantedConsent);
|
||||
}
|
||||
for (String roleId : clientSession.getRoles()) {
|
||||
grantedConsent.addGrantedRole(roleId);
|
||||
for (RoleModel role : accessCode.getRequestedRoles()) {
|
||||
grantedConsent.addGrantedRole(role);
|
||||
}
|
||||
// TODO: It's not 100% sure that approved protocolMappers are same like the protocolMappers retrieved here from the client. Maybe clientSession.setProtocolMappers/getProtocolMappers should be added...
|
||||
for (ProtocolMapperModel protocolMapper : client.getProtocolMappers()) {
|
||||
if (protocolMapper.isConsentRequired() && protocolMapper.getProtocol().equals(clientSession.getAuthMethod()) && protocolMapper.getConsentText() != null) {
|
||||
grantedConsent.addGrantedProtocolMapper(protocolMapper.getId());
|
||||
for (ProtocolMapperModel protocolMapper : accessCode.getRequestedProtocolMappers()) {
|
||||
if (protocolMapper.isConsentRequired() && protocolMapper.getConsentText() != null) {
|
||||
grantedConsent.addGrantedProtocolMapper(protocolMapper);
|
||||
}
|
||||
}
|
||||
user.updateConsent(grantedConsent);
|
||||
|
||||
Reference in new issue
Block a user