Search in sources :

Example 21 with ClientSessionContext

use of org.keycloak.models.ClientSessionContext in project keycloak by keycloak.

the class RoleListMapper method mapRoles.

@Override
public void mapRoles(AttributeStatementType roleAttributeStatement, ProtocolMapperModel mappingModel, KeycloakSession session, UserSessionModel userSession, ClientSessionContext clientSessionCtx) {
    String single = mappingModel.getConfig().get(SINGLE_ROLE_ATTRIBUTE);
    boolean singleAttribute = Boolean.parseBoolean(single);
    List<SamlProtocol.ProtocolMapperProcessor<SAMLRoleNameMapper>> roleNameMappers = new LinkedList<>();
    AtomicReference<AttributeType> singleAttributeType = new AtomicReference<>(null);
    ProtocolMapperUtils.getSortedProtocolMappers(session, clientSessionCtx).forEach(entry -> {
        ProtocolMapperModel mapping = entry.getKey();
        ProtocolMapper mapper = entry.getValue();
        if (mapper instanceof SAMLRoleNameMapper) {
            roleNameMappers.add(new SamlProtocol.ProtocolMapperProcessor<>((SAMLRoleNameMapper) mapper, mapping));
        }
        if (mapper instanceof HardcodedRole) {
            AttributeType attributeType;
            if (singleAttribute) {
                if (singleAttributeType.get() == null) {
                    singleAttributeType.set(AttributeStatementHelper.createAttributeType(mappingModel));
                    roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(singleAttributeType.get()));
                }
                attributeType = singleAttributeType.get();
            } else {
                attributeType = AttributeStatementHelper.createAttributeType(mappingModel);
                roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(attributeType));
            }
            attributeType.addAttributeValue(mapping.getConfig().get(HardcodedRole.ROLE_ATTRIBUTE));
        }
    });
    List<String> allRoleNames = clientSessionCtx.getRolesStream().map(roleModel -> roleNameMappers.stream().map(entry -> entry.mapper.mapName(entry.model, roleModel)).filter(Objects::nonNull).findFirst().orElse(roleModel.getName())).collect(Collectors.toList());
    for (String roleName : allRoleNames) {
        AttributeType attributeType;
        if (singleAttribute) {
            if (singleAttributeType.get() == null) {
                singleAttributeType.set(AttributeStatementHelper.createAttributeType(mappingModel));
                roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(singleAttributeType.get()));
            }
            attributeType = singleAttributeType.get();
        } else {
            attributeType = AttributeStatementHelper.createAttributeType(mappingModel);
            roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(attributeType));
        }
        attributeType.addAttributeValue(roleName);
    }
}
Also used : ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) KeycloakSession(org.keycloak.models.KeycloakSession) SamlProtocol(org.keycloak.protocol.saml.SamlProtocol) HashMap(java.util.HashMap) UserSessionModel(org.keycloak.models.UserSessionModel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) ArrayList(java.util.ArrayList) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) Objects(java.util.Objects) List(java.util.List) ClientSessionContext(org.keycloak.models.ClientSessionContext) Map(java.util.Map) ProtocolMapperUtils(org.keycloak.protocol.ProtocolMapperUtils) ProtocolMapper(org.keycloak.protocol.ProtocolMapper) LinkedList(java.util.LinkedList) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtocolMapper(org.keycloak.protocol.ProtocolMapper) SamlProtocol(org.keycloak.protocol.saml.SamlProtocol) LinkedList(java.util.LinkedList) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) Objects(java.util.Objects)

Aggregations

ClientSessionContext (org.keycloak.models.ClientSessionContext)21 UserSessionModel (org.keycloak.models.UserSessionModel)15 DefaultClientSessionContext (org.keycloak.services.util.DefaultClientSessionContext)12 UserModel (org.keycloak.models.UserModel)11 AuthenticationSessionModel (org.keycloak.sessions.AuthenticationSessionModel)11 RootAuthenticationSessionModel (org.keycloak.sessions.RootAuthenticationSessionModel)9 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)8 ClientModel (org.keycloak.models.ClientModel)8 CorsErrorResponseException (org.keycloak.services.CorsErrorResponseException)8 Response (javax.ws.rs.core.Response)7 AuthenticationSessionManager (org.keycloak.services.managers.AuthenticationSessionManager)7 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)6 RealmModel (org.keycloak.models.RealmModel)5 AccessToken (org.keycloak.representations.AccessToken)4 ClientScopeModel (org.keycloak.models.ClientScopeModel)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 TokenManager (org.keycloak.protocol.oidc.TokenManager)3 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)3 ErrorResponseException (org.keycloak.services.ErrorResponseException)3 HashMap (java.util.HashMap)2