Java Code Examples for org.keycloak.models.ClientModel#getRealm()

The following examples show how to use org.keycloak.models.ClientModel#getRealm() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JpaUserProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public UserModel getServiceAccount(ClientModel client) {
    TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByServiceAccount", UserEntity.class);
    query.setParameter("realmId", client.getRealm().getId());
    query.setParameter("clientInternalId", client.getId());
    List<UserEntity> results = query.getResultList();
    if (results.isEmpty()) {
        return null;
    } else if (results.size() > 1) {
        throw new IllegalStateException("More service account linked users found for client=" + client.getClientId() +
                ", results=" + results);
    } else {
        UserEntity user = results.get(0);
        return new UserAdapter(session, client.getRealm(), em, user);
    }
}
 
Example 2
Source File: UserSessionManager.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public boolean revokeOfflineToken(UserModel user, ClientModel client) {
    RealmModel realm = client.getRealm();

    List<UserSessionModel> userSessions = kcSession.sessions().getOfflineUserSessions(realm, user);
    boolean anyRemoved = false;
    for (UserSessionModel userSession : userSessions) {
        AuthenticatedClientSessionModel clientSession = userSession.getAuthenticatedClientSessionByClient(client.getId());
        if (clientSession != null) {
            if (logger.isTraceEnabled()) {
                logger.tracef("Removing existing offline token for user '%s' and client '%s' .",
                        user.getUsername(), client.getClientId());
            }

            clientSession.detachFromUserSession();
            persister.removeClientSession(userSession.getId(), client.getId(), true);
            checkOfflineUserSessionHasClientSessions(realm, user, userSession);
            anyRemoved = true;
        }
    }

    return anyRemoved;
}
 
Example 3
Source File: UserCacheSession.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public UserModel findServiceAccount(ClientModel client) {
    String username = ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX + client.getClientId();
    logger.tracev("getServiceAccount: {0}", username);
    username = username.toLowerCase();
    RealmModel realm = client.getRealm();
    if (realmInvalidations.contains(realm.getId())) {
        logger.tracev("realmInvalidations");
        return getDelegate().getServiceAccount(client);
    }
    String cacheKey = getUserByUsernameCacheKey(realm.getId(), username);
    if (invalidations.contains(cacheKey)) {
        logger.tracev("invalidations");
        return getDelegate().getServiceAccount(client);
    }
    UserListQuery query = cache.get(cacheKey, UserListQuery.class);

    String userId = null;
    if (query == null) {
        logger.tracev("query null");
        Long loaded = cache.getCurrentRevision(cacheKey);
        UserModel model = getDelegate().getServiceAccount(client);
        if (model == null) {
            logger.tracev("model from delegate null");
            return null;
        }
        userId = model.getId();
        if (invalidations.contains(userId)) return model;
        if (managedUsers.containsKey(userId)) {
            logger.tracev("return managed user");
            return managedUsers.get(userId);
        }

        UserModel adapter = getUserAdapter(realm, userId, loaded, model);
        if (adapter instanceof UserAdapter) { // this was cached, so we can cache query too
            query = new UserListQuery(loaded, cacheKey, realm, model.getId());
            cache.addRevisioned(query, startupRevision);
        }
        managedUsers.put(userId, adapter);
        return adapter;
    } else {
        userId = query.getUsers().iterator().next();
        if (invalidations.contains(userId)) {
            logger.tracev("invalidated cache return delegate");
            return getDelegate().getUserByUsername(username, realm);

        }
        logger.trace("return getUserById");
        return getUserById(userId, realm);
    }
}
 
Example 4
Source File: ClientModelIdentity.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public ClientModelIdentity(KeycloakSession session, ClientModel client) {
    this.realm = client.getRealm();
    this.client = client;
    this.serviceAccount = session.users().getServiceAccount(client);
}