Search in sources :

Example 61 with UserModel

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

the class UsersPartialImport method create.

@Override
public void create(RealmModel realm, KeycloakSession session, UserRepresentation user) {
    user.setId(KeycloakModelUtils.generateId());
    UserModel userModel = RepresentationToModel.createUser(session, realm, user);
    if (userModel == null)
        throw new RuntimeException("Unable to create user " + getName(user));
    createdIds.put(getName(user), userModel.getId());
}
Also used : UserModel(org.keycloak.models.UserModel)

Example 62 with UserModel

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

the class AuthorizationEndpointBase method createAuthenticationSession.

protected AuthenticationSessionModel createAuthenticationSession(ClientModel client, String requestState) {
    AuthenticationSessionManager manager = new AuthenticationSessionManager(session);
    RootAuthenticationSessionModel rootAuthSession = manager.getCurrentRootAuthenticationSession(realm);
    AuthenticationSessionModel authSession;
    if (rootAuthSession != null) {
        authSession = rootAuthSession.createAuthenticationSession(client);
        logger.debugf("Sent request to authz endpoint. Root authentication session with ID '%s' exists. Client is '%s' . Created new authentication session with tab ID: %s", rootAuthSession.getId(), client.getClientId(), authSession.getTabId());
    } else {
        UserSessionCrossDCManager userSessionCrossDCManager = new UserSessionCrossDCManager(session);
        UserSessionModel userSession = userSessionCrossDCManager.getUserSessionIfExistsRemotely(manager, realm);
        if (userSession != null) {
            UserModel user = userSession.getUser();
            if (user != null && !user.isEnabled()) {
                authSession = createNewAuthenticationSession(manager, client);
                AuthenticationManager.backchannelLogout(session, userSession, true);
            } else {
                String userSessionId = userSession.getId();
                rootAuthSession = session.authenticationSessions().createRootAuthenticationSession(realm, userSessionId);
                authSession = rootAuthSession.createAuthenticationSession(client);
                logger.debugf("Sent request to authz endpoint. We don't have root authentication session with ID '%s' but we have userSession." + "Re-created root authentication session with same ID. Client is: %s . New authentication session tab ID: %s", userSessionId, client.getClientId(), authSession.getTabId());
            }
        } else {
            authSession = createNewAuthenticationSession(manager, client);
        }
    }
    session.getProvider(LoginFormsProvider.class).setAuthenticationSession(authSession);
    return authSession;
}
Also used : AuthenticationSessionManager(org.keycloak.services.managers.AuthenticationSessionManager) UserModel(org.keycloak.models.UserModel) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) UserSessionModel(org.keycloak.models.UserSessionModel) LoginFormsProvider(org.keycloak.forms.login.LoginFormsProvider) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) UserSessionCrossDCManager(org.keycloak.services.managers.UserSessionCrossDCManager)

Example 63 with UserModel

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

the class ClientsPartialImport method remove.

@Override
public void remove(RealmModel realm, KeycloakSession session, ClientRepresentation clientRep) {
    ClientModel clientModel = realm.getClientByClientId(getName(clientRep));
    // remove the associated service account if the account exists
    if (clientModel.isServiceAccountsEnabled()) {
        UserModel serviceAccountUser = session.users().getServiceAccount(clientModel);
        if (serviceAccountUser != null) {
            session.users().removeUser(realm, serviceAccountUser);
        }
    }
    // the authorization resource server seems to be removed using the delete event, so it's not needed
    // remove the client itself
    realm.removeClient(clientModel.getId());
}
Also used : UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel)

Example 64 with UserModel

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

the class AccessTokenIntrospectionProvider method introspect.

public Response introspect(String token) {
    try {
        AccessToken accessToken = verifyAccessToken(token);
        ObjectNode tokenMetadata;
        if (accessToken != null) {
            tokenMetadata = JsonSerialization.createObjectNode(accessToken);
            tokenMetadata.put("client_id", accessToken.getIssuedFor());
            if (!tokenMetadata.has("username")) {
                if (accessToken.getPreferredUsername() != null) {
                    tokenMetadata.put("username", accessToken.getPreferredUsername());
                } else {
                    UserModel userModel = session.users().getUserById(realm, accessToken.getSubject());
                    if (userModel != null) {
                        tokenMetadata.put("username", userModel.getUsername());
                    }
                }
            }
        } else {
            tokenMetadata = JsonSerialization.createObjectNode();
        }
        tokenMetadata.put("active", accessToken != null);
        return Response.ok(JsonSerialization.writeValueAsBytes(tokenMetadata)).type(MediaType.APPLICATION_JSON_TYPE).build();
    } catch (Exception e) {
        throw new RuntimeException("Error creating token introspection response.", e);
    }
}
Also used : UserModel(org.keycloak.models.UserModel) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AccessToken(org.keycloak.representations.AccessToken) VerificationException(org.keycloak.common.VerificationException)

Example 65 with UserModel

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

the class UserStorageManager method getUserById.

/**
 * {@link UserRegistrationProvider} methods implementations end here
 *        {@link UserLookupProvider} methods implementations start here
 */
@Override
public UserModel getUserById(RealmModel realm, String id) {
    StorageId storageId = new StorageId(id);
    if (storageId.getProviderId() == null) {
        UserModel user = localStorage().getUserById(realm, id);
        return importValidation(realm, user);
    }
    UserLookupProvider provider = getStorageProviderInstance(realm, storageId.getProviderId(), UserLookupProvider.class);
    if (provider == null)
        return null;
    return provider.getUserById(realm, id);
}
Also used : UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserLookupProvider(org.keycloak.storage.user.UserLookupProvider)

Aggregations

UserModel (org.keycloak.models.UserModel)383 RealmModel (org.keycloak.models.RealmModel)220 Test (org.junit.Test)126 ClientModel (org.keycloak.models.ClientModel)86 KeycloakSession (org.keycloak.models.KeycloakSession)81 CachedUserModel (org.keycloak.models.cache.CachedUserModel)52 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)43 List (java.util.List)41 UserSessionModel (org.keycloak.models.UserSessionModel)40 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)40 RoleModel (org.keycloak.models.RoleModel)39 ComponentModel (org.keycloak.component.ComponentModel)31 HashMap (java.util.HashMap)30 Response (javax.ws.rs.core.Response)29 Path (javax.ws.rs.Path)28 UserManager (org.keycloak.models.UserManager)28 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)27 Map (java.util.Map)25 GroupModel (org.keycloak.models.GroupModel)24 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)24