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

The following examples show how to use org.keycloak.models.ClientModel#getId() . 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: InfinispanUserSessionProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public AuthenticatedClientSessionModel createClientSession(RealmModel realm, ClientModel client, UserSessionModel userSession) {
    final UUID clientSessionId = keyGenerator.generateKeyUUID(session, clientSessionCache);
    AuthenticatedClientSessionEntity entity = new AuthenticatedClientSessionEntity(clientSessionId);
    entity.setRealmId(realm.getId());
    entity.setTimestamp(Time.currentTime());

    InfinispanChangelogBasedTransaction<String, UserSessionEntity> userSessionUpdateTx = getTransaction(false);
    InfinispanChangelogBasedTransaction<UUID, AuthenticatedClientSessionEntity> clientSessionUpdateTx = getClientSessionTransaction(false);
    AuthenticatedClientSessionAdapter adapter = new AuthenticatedClientSessionAdapter(session, this, entity, client, userSession, userSessionUpdateTx, clientSessionUpdateTx, false);

    SessionUpdateTask<AuthenticatedClientSessionEntity> createClientSessionTask = Tasks.addIfAbsentSync();
    clientSessionUpdateTx.addTask(clientSessionId, createClientSessionTask, entity);

    SessionUpdateTask registerClientSessionTask = new RegisterClientSessionTask(client.getId(), clientSessionId);
    userSessionUpdateTx.addTask(userSession.getId(), registerClientSessionTask);

    return adapter;
}
 
Example 2
Source File: JpaUserProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void preRemove(RealmModel realm, ClientModel client) {
    StorageId clientStorageId = new StorageId(client.getId());
    if (clientStorageId.isLocal()) {
        int num = em.createNamedQuery("deleteUserConsentClientScopesByClient")
                .setParameter("clientId", client.getId())
                .executeUpdate();
        num = em.createNamedQuery("deleteUserConsentsByClient")
                .setParameter("clientId", client.getId())
                .executeUpdate();
    } else {
        em.createNamedQuery("deleteUserConsentClientScopesByExternalClient")
                .setParameter("clientStorageProvider", clientStorageId.getProviderId())
                .setParameter("externalClientId", clientStorageId.getExternalId())
                .executeUpdate();
        em.createNamedQuery("deleteUserConsentsByExternalClient")
                .setParameter("clientStorageProvider", clientStorageId.getProviderId())
                .setParameter("externalClientId", clientStorageId.getExternalId())
                .executeUpdate();

    }
}
 
Example 3
Source File: JpaUserFederatedStorageProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void preRemove(RealmModel realm, ClientModel client) {
    StorageId clientStorageId = new StorageId(client.getId());
    if (clientStorageId.isLocal()) {
        em.createNamedQuery("deleteFederatedUserConsentClientScopesByClient").setParameter("clientId", client.getId()).executeUpdate();
        em.createNamedQuery("deleteFederatedUserConsentsByClient").setParameter("clientId", client.getId()).executeUpdate();
    } else {
        em.createNamedQuery("deleteFederatedUserConsentClientScopesByExternalClient")
                .setParameter("clientStorageProvider", clientStorageId.getProviderId())
                .setParameter("externalClientId",clientStorageId.getExternalId())
                .executeUpdate();
        em.createNamedQuery("deleteFederatedUserConsentsByExternalClient")
                .setParameter("clientStorageProvider", clientStorageId.getProviderId())
                .setParameter("externalClientId",clientStorageId.getExternalId())
                .executeUpdate();

    }
}
 
Example 4
Source File: InfinispanUserSessionProvider.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected long getUserSessionsCount(RealmModel realm, ClientModel client, boolean offline) {
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline);
    cache = CacheDecorators.skipCacheLoaders(cache);

    final String clientUuid = client.getId();

    return cache.entrySet().stream()
            .filter(UserSessionPredicate.create(realm.getId()).client(clientUuid))
            .count();
}
 
Example 5
Source File: ClientRemovedEvent.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ClientRemovedEvent create(ClientModel client) {
    ClientRemovedEvent event = new ClientRemovedEvent();

    event.realmId = client.getRealm().getId();
    event.clientUuid = client.getId();
    event.clientId = client.getClientId();
    event.clientRoles = new HashMap<>();
    for (RoleModel clientRole : client.getRoles()) {
        event.clientRoles.put(clientRole.getId(), clientRole.getName());
    }

    return event;
}
 
Example 6
Source File: InfinispanUserSessionProvider.java    From keycloak with Apache License 2.0 4 votes vote down vote up
protected List<UserSessionModel> getUserSessions(final RealmModel realm, ClientModel client, int firstResult, int maxResults, final boolean offline) {
    final String clientUuid = client.getId();
    UserSessionPredicate predicate = UserSessionPredicate.create(realm.getId()).client(clientUuid);

    return getUserSessionModels(realm, firstResult, maxResults, offline, predicate);
}
 
Example 7
Source File: SamlSessionUtils.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static String getSessionIndex(AuthenticatedClientSessionModel clientSession) {
    UserSessionModel userSession = clientSession.getUserSession();
    ClientModel client = clientSession.getClient();

    return userSession.getId() + DELIMITER + client.getId();
}
 
Example 8
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getResourceName(ClientModel client) {
    return "client.resource." + client.getId();
}
 
Example 9
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getManagePermissionName(ClientModel client) {
    return "manage.permission.client." + client.getId();
}
 
Example 10
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getConfigurePermissionName(ClientModel client) {
    return "configure.permission.client." + client.getId();
}
 
Example 11
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getViewPermissionName(ClientModel client) {
    return "view.permission.client." + client.getId();
}
 
Example 12
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getMapRolesPermissionName(ClientModel client) {
    return MAP_ROLES_SCOPE + ".permission.client." + client.getId();
}
 
Example 13
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getMapRolesClientScopePermissionName(ClientModel client) {
    return MAP_ROLES_CLIENT_SCOPE + ".permission.client." + client.getId();
}
 
Example 14
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getMapRolesCompositePermissionName(ClientModel client) {
    return MAP_ROLES_COMPOSITE_SCOPE + ".permission.client." + client.getId();
}
 
Example 15
Source File: ClientPermissions.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String getExchangeToPermissionName(ClientModel client) {
    return TOKEN_EXCHANGE + ".permission.client." + client.getId();
}
 
Example 16
Source File: KeycloakIdentity.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public KeycloakIdentity(AccessToken accessToken, KeycloakSession keycloakSession) {
    if (accessToken == null) {
        throw new ErrorResponseException("invalid_bearer_token", "Could not obtain bearer access_token from request.", Status.FORBIDDEN);
    }
    if (keycloakSession == null) {
        throw new ErrorResponseException("no_keycloak_session", "No keycloak session", Status.FORBIDDEN);
    }
    this.accessToken = accessToken;
    this.keycloakSession = keycloakSession;
    this.realm = keycloakSession.getContext().getRealm();

    Map<String, Collection<String>> attributes = new HashMap<>();

    try {
        ObjectNode objectNode = JsonSerialization.createObjectNode(this.accessToken);
        Iterator<String> iterator = objectNode.fieldNames();

        while (iterator.hasNext()) {
            String fieldName = iterator.next();
            JsonNode fieldValue = objectNode.get(fieldName);
            List<String> values = new ArrayList<>();

            if (fieldValue.isArray()) {
                Iterator<JsonNode> valueIterator = fieldValue.iterator();

                while (valueIterator.hasNext()) {
                    values.add(valueIterator.next().asText());
                }
            } else {
                String value = fieldValue.asText();

                if (StringUtil.isNullOrEmpty(value)) {
                    continue;
                }

                values.add(value);
            }

            if (!values.isEmpty()) {
                attributes.put(fieldName, values);
            }
        }

        AccessToken.Access realmAccess = accessToken.getRealmAccess();

        if (realmAccess != null) {
            attributes.put("kc.realm.roles", realmAccess.getRoles());
        }

        Map<String, AccessToken.Access> resourceAccess = accessToken.getResourceAccess();

        if (resourceAccess != null) {
            resourceAccess.forEach((clientId, access) -> attributes.put("kc.client." + clientId + ".roles", access.getRoles()));
        }

        ClientModel clientModel = getTargetClient();
        UserModel clientUser = null;

        if (clientModel != null) {
            clientUser = this.keycloakSession.users().getServiceAccount(clientModel);
        }

        UserModel userSession = getUserFromSessionState();

        this.resourceServer = clientUser != null && userSession.getId().equals(clientUser.getId());

        if (resourceServer) {
            this.id = clientModel.getId();
        } else {
            this.id = userSession.getId();
        }
    } catch (Exception e) {
        throw new RuntimeException("Error while reading attributes from security token.", e);
    }

    this.attributes = Attributes.from(attributes);
}