Search in sources :

Example 56 with ClientModel

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

the class LoginTimeoutValidationTest method testIsLoginTimeoutValid.

@Test
@ModelTest
public void testIsLoginTimeoutValid(KeycloakSession keycloakSession) {
    RealmModel realm = keycloakSession.realms().getRealmByName("test");
    UserSessionModel userSession = keycloakSession.sessions().createUserSession(realm, keycloakSession.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", true, null, null);
    ClientModel client = realm.getClientByClientId("account");
    AuthenticationSessionModel authSession = keycloakSession.authenticationSessions().createRootAuthenticationSession(realm).createAuthenticationSession(client);
    ClientSessionCode clientSessionCode = new ClientSessionCode(keycloakSession, realm, authSession);
    /*
         * KEYCLOAK-10636 Large Login timeout causes login failure
         * realm > Realm setting > Tokens > Login timeout
         */
    // Login timeout
    int accessCodeLifespanLoginOrig = realm.getAccessCodeLifespanLogin();
    realm.setAccessCodeLifespanLogin(Integer.MAX_VALUE);
    Assert.assertTrue("Login validataion with large Login Timeout failed", clientSessionCode.isActionActive(ClientSessionCode.ActionType.LOGIN));
    realm.setAccessCodeLifespanLogin(accessCodeLifespanLoginOrig);
    /*
         * KEYCLOAK-10637 Large Login Action timeout causes login failure
         * realm > Realm setting > Tokens > Login Action timeout
         */
    // Login Action timeout
    int accessCodeLifespanUserActionOrig = realm.getAccessCodeLifespanUserAction();
    realm.setAccessCodeLifespanUserAction(Integer.MAX_VALUE);
    Assert.assertTrue("Login validataion with large Login Action Timeout failed", clientSessionCode.isActionActive(ClientSessionCode.ActionType.USER));
    realm.setAccessCodeLifespanUserAction(accessCodeLifespanUserActionOrig);
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) UserSessionModel(org.keycloak.models.UserSessionModel) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) ClientSessionCode(org.keycloak.services.managers.ClientSessionCode) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 57 with ClientModel

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

the class UserSessionAdapter method getAuthenticatedClientSessionByClient.

@Override
public AuthenticatedClientSessionModel getAuthenticatedClientSessionByClient(String clientUUID) {
    AuthenticatedClientSessionStore clientSessionEntities = entity.getAuthenticatedClientSessions();
    final UUID clientSessionId = clientSessionEntities.get(clientUUID);
    if (clientSessionId == null) {
        return null;
    }
    ClientModel client = realm.getClientById(clientUUID);
    if (client != null) {
        return provider.getClientSession(this, client, clientSessionId, offline);
    }
    removeAuthenticatedClientSessions(Collections.singleton(clientUUID));
    return null;
}
Also used : ClientModel(org.keycloak.models.ClientModel) UUID(java.util.UUID) AuthenticatedClientSessionStore(org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionStore)

Example 58 with ClientModel

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

the class UserSessionAdapter method getAuthenticatedClientSessions.

@Override
public Map<String, AuthenticatedClientSessionModel> getAuthenticatedClientSessions() {
    AuthenticatedClientSessionStore clientSessionEntities = entity.getAuthenticatedClientSessions();
    Map<String, AuthenticatedClientSessionModel> result = new HashMap<>();
    List<String> removedClientUUIDS = new LinkedList<>();
    if (clientSessionEntities != null) {
        clientSessionEntities.forEach((String key, UUID value) -> {
            // Check if client still exists
            ClientModel client = realm.getClientById(key);
            if (client != null) {
                final AuthenticatedClientSessionAdapter clientSession = provider.getClientSession(this, client, value, offline);
                if (clientSession != null) {
                    result.put(key, clientSession);
                }
            } else {
                removedClientUUIDS.add(key);
            }
        });
    }
    removeAuthenticatedClientSessions(removedClientUUIDS);
    return Collections.unmodifiableMap(result);
}
Also used : ClientModel(org.keycloak.models.ClientModel) HashMap(java.util.HashMap) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel) UUID(java.util.UUID) AuthenticatedClientSessionStore(org.keycloak.models.sessions.infinispan.entities.AuthenticatedClientSessionStore) LinkedList(java.util.LinkedList)

Example 59 with ClientModel

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

the class RepresentationToModel method updateClientProtocolMappers.

public static void updateClientProtocolMappers(ClientRepresentation rep, ClientModel resource) {
    if (rep.getProtocolMappers() != null) {
        Map<String, ProtocolMapperModel> existingProtocolMappers = resource.getProtocolMappersStream().collect(Collectors.toMap(mapper -> generateProtocolNameKey(mapper.getProtocol(), mapper.getName()), Function.identity()));
        for (ProtocolMapperRepresentation protocolMapperRepresentation : rep.getProtocolMappers()) {
            String protocolNameKey = generateProtocolNameKey(protocolMapperRepresentation.getProtocol(), protocolMapperRepresentation.getName());
            ProtocolMapperModel existingMapper = existingProtocolMappers.get(protocolNameKey);
            if (existingMapper != null) {
                ProtocolMapperModel updatedProtocolMapperModel = toModel(protocolMapperRepresentation);
                updatedProtocolMapperModel.setId(existingMapper.getId());
                resource.updateProtocolMapper(updatedProtocolMapperModel);
                existingProtocolMappers.remove(protocolNameKey);
            } else {
                resource.addProtocolMapper(toModel(protocolMapperRepresentation));
            }
        }
        for (Map.Entry<String, ProtocolMapperModel> entryToDelete : existingProtocolMappers.entrySet()) {
            resource.removeProtocolMapper(entryToDelete.getValue());
        }
    }
}
Also used : Arrays(java.util.Arrays) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) AuthenticationExecutionExportRepresentation(org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation) LDAPConstants(org.keycloak.models.LDAPConstants) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) OAuth2DeviceConfig(org.keycloak.models.OAuth2DeviceConfig) Map(java.util.Map) SocialLinkRepresentation(org.keycloak.representations.idm.SocialLinkRepresentation) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) UserCredentialModel(org.keycloak.models.UserCredentialModel) PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) Set(java.util.Set) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) PolicyStore(org.keycloak.authorization.store.PolicyStore) ResourceStore(org.keycloak.authorization.store.ResourceStore) ClientTemplateRepresentation(org.keycloak.representations.idm.ClientTemplateRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) SocialIdentityProvider(org.keycloak.broker.social.SocialIdentityProvider) ResourceServerRepresentation(org.keycloak.representations.idm.authorization.ResourceServerRepresentation) SslRequired(org.keycloak.common.enums.SslRequired) PermissionTicketStore(org.keycloak.authorization.store.PermissionTicketStore) OAuth2Constants(org.keycloak.OAuth2Constants) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) ClaimRepresentation(org.keycloak.representations.idm.ClaimRepresentation) Constants(org.keycloak.models.Constants) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) MigrationProvider(org.keycloak.migration.MigrationProvider) ArrayList(java.util.ArrayList) PermissionTicket(org.keycloak.authorization.model.PermissionTicket) OTPSecretData(org.keycloak.models.credential.dto.OTPSecretData) UserModel(org.keycloak.models.UserModel) UserFederationProviderRepresentation(org.keycloak.representations.idm.UserFederationProviderRepresentation) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) MigrationUtils(org.keycloak.migration.migrators.MigrationUtils) ValidationUtil(org.keycloak.validation.ValidationUtil) PolicyProviderFactory(org.keycloak.authorization.policy.provider.PolicyProviderFactory) CredentialModel(org.keycloak.credential.CredentialModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) WebAuthnPolicy(org.keycloak.models.WebAuthnPolicy) KeycloakSession(org.keycloak.models.KeycloakSession) RequiredActionProviderRepresentation(org.keycloak.representations.idm.RequiredActionProviderRepresentation) IOException(java.io.IOException) ResourceServerStore(org.keycloak.authorization.store.ResourceServerStore) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) ListIterator(java.util.ListIterator) MigrateTo8_0_0(org.keycloak.migration.migrators.MigrateTo8_0_0) PasswordPolicyNotMetException(org.keycloak.policy.PasswordPolicyNotMetException) Config(org.keycloak.Config) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) ClaimMask(org.keycloak.models.ClaimMask) ComponentModel(org.keycloak.component.ComponentModel) BrowserSecurityHeaders(org.keycloak.models.BrowserSecurityHeaders) AuthenticationExecutionRepresentation(org.keycloak.representations.idm.AuthenticationExecutionRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) PolicyEnforcementMode(org.keycloak.representations.idm.authorization.PolicyEnforcementMode) UriUtils(org.keycloak.common.util.UriUtils) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) AuthorizationProviderFactory(org.keycloak.authorization.AuthorizationProviderFactory) RoleModel(org.keycloak.models.RoleModel) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) PermissionTicketRepresentation(org.keycloak.representations.idm.authorization.PermissionTicketRepresentation) UserProvider(org.keycloak.models.UserProvider) List(java.util.List) ScopeStore(org.keycloak.authorization.store.ScopeStore) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Entry(java.util.Map.Entry) RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) ClientModel(org.keycloak.models.ClientModel) Scope(org.keycloak.authorization.model.Scope) IdentityProviderFactory(org.keycloak.broker.provider.IdentityProviderFactory) Profile(org.keycloak.common.Profile) IdentityProviderMapperModel(org.keycloak.models.IdentityProviderMapperModel) ScopeMappingRepresentation(org.keycloak.representations.idm.ScopeMappingRepresentation) Logger(org.jboss.logging.Logger) StoreFactory(org.keycloak.authorization.store.StoreFactory) HashMap(java.util.HashMap) OAuthClientRepresentation(org.keycloak.representations.idm.OAuthClientRepresentation) Function(java.util.function.Function) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) HashSet(java.util.HashSet) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ComponentExportRepresentation(org.keycloak.representations.idm.ComponentExportRepresentation) UserFederationMapperRepresentation(org.keycloak.representations.idm.UserFederationMapperRepresentation) UserConsentModel(org.keycloak.models.UserConsentModel) KeyProvider(org.keycloak.keys.KeyProvider) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel) IdentityProviderMapperRepresentation(org.keycloak.representations.idm.IdentityProviderMapperRepresentation) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) OTPPolicy(org.keycloak.models.OTPPolicy) GroupModel(org.keycloak.models.GroupModel) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) LinkedList(java.util.LinkedList) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) IdentityProvider(org.keycloak.broker.provider.IdentityProvider) ScopeContainerModel(org.keycloak.models.ScopeContainerModel) ResourceServer(org.keycloak.authorization.model.ResourceServer) FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) RolesRepresentation(org.keycloak.representations.idm.RolesRepresentation) OTPCredentialData(org.keycloak.models.credential.dto.OTPCredentialData) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Iterator(java.util.Iterator) CibaConfig(org.keycloak.models.CibaConfig) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ApplicationRepresentation(org.keycloak.representations.idm.ApplicationRepresentation) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) DecisionStrategy(org.keycloak.representations.idm.authorization.DecisionStrategy) JsonSerialization(org.keycloak.util.JsonSerialization) Policy(org.keycloak.authorization.model.Policy) AuthenticatorConfigRepresentation(org.keycloak.representations.idm.AuthenticatorConfigRepresentation) ModelException(org.keycloak.models.ModelException) ParConfig(org.keycloak.models.ParConfig) PasswordPolicy(org.keycloak.models.PasswordPolicy) Resource(org.keycloak.authorization.model.Resource) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 60 with ClientModel

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

the class RepresentationToModel method createFederatedRoleMappings.

public static void createFederatedRoleMappings(UserFederatedStorageProvider federatedStorage, UserRepresentation userRep, RealmModel realm) {
    if (userRep.getRealmRoles() != null) {
        for (String roleString : userRep.getRealmRoles()) {
            RoleModel role = realm.getRole(roleString.trim());
            if (role == null) {
                role = realm.addRole(roleString.trim());
            }
            federatedStorage.grantRole(realm, userRep.getId(), role);
        }
    }
    if (userRep.getClientRoles() != null) {
        for (Map.Entry<String, List<String>> entry : userRep.getClientRoles().entrySet()) {
            ClientModel client = realm.getClientByClientId(entry.getKey());
            if (client == null) {
                throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey());
            }
            createFederatedClientRoleMappings(federatedStorage, realm, client, userRep, entry.getValue());
        }
    }
}
Also used : ClientModel(org.keycloak.models.ClientModel) RoleModel(org.keycloak.models.RoleModel) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap)

Aggregations

ClientModel (org.keycloak.models.ClientModel)344 RealmModel (org.keycloak.models.RealmModel)148 UserModel (org.keycloak.models.UserModel)88 RoleModel (org.keycloak.models.RoleModel)74 KeycloakSession (org.keycloak.models.KeycloakSession)67 Test (org.junit.Test)64 UserSessionModel (org.keycloak.models.UserSessionModel)41 ResourceServer (org.keycloak.authorization.model.ResourceServer)39 Policy (org.keycloak.authorization.model.Policy)38 HashMap (java.util.HashMap)37 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)36 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)34 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)34 List (java.util.List)32 Map (java.util.Map)32 Path (javax.ws.rs.Path)29 LinkedList (java.util.LinkedList)28 ClientScopeModel (org.keycloak.models.ClientScopeModel)28 ArrayList (java.util.ArrayList)27 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)27