Search in sources :

Example 1 with Algorithm

use of org.keycloak.crypto.Algorithm in project keycloak by keycloak.

the class DefaultKeyManager method getActiveKey.

@Override
public KeyWrapper getActiveKey(RealmModel realm, KeyUse use, String algorithm) {
    KeyWrapper activeKey = getActiveKey(getProviders(realm), realm, use, algorithm);
    if (activeKey != null) {
        return activeKey;
    }
    logger.debugv("Failed to find active key for realm, trying fallback: realm={0} algorithm={1} use={2}", realm.getName(), algorithm, use.name());
    Optional<KeyProviderFactory> keyProviderFactory = session.getKeycloakSessionFactory().getProviderFactoriesStream(KeyProvider.class).map(KeyProviderFactory.class::cast).filter(kf -> kf.createFallbackKeys(session, use, algorithm)).findFirst();
    if (keyProviderFactory.isPresent()) {
        providersMap.remove(realm.getId());
        List<KeyProvider> providers = getProviders(realm);
        activeKey = getActiveKey(providers, realm, use, algorithm);
        if (activeKey != null) {
            logger.infov("No keys found for realm={0} and algorithm={1} for use={2}. Generating keys.", realm.getName(), algorithm, use.name());
            return activeKey;
        }
    }
    logger.errorv("Failed to create fallback key for realm: realm={0} algorithm={1} use={2", realm.getName(), algorithm, use.name());
    throw new RuntimeException("Failed to find key: realm=" + realm.getName() + " algorithm=" + algorithm + " use=" + use.name());
}
Also used : KeyWrapper(org.keycloak.crypto.KeyWrapper) KeyWrapper(org.keycloak.crypto.KeyWrapper) RealmModel(org.keycloak.models.RealmModel) Logger(org.jboss.logging.Logger) KeycloakSession(org.keycloak.models.KeycloakSession) Algorithm(org.keycloak.crypto.Algorithm) PublicKey(java.security.PublicKey) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) KeyManager(org.keycloak.models.KeyManager) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) Certificate(java.security.cert.Certificate) Stream(java.util.stream.Stream) KeyUse(org.keycloak.crypto.KeyUse) PrivateKey(java.security.PrivateKey) Map(java.util.Map) ComponentModel(org.keycloak.component.ComponentModel) Optional(java.util.Optional) SecretKey(javax.crypto.SecretKey) Comparator(java.util.Comparator) ProviderFactory(org.keycloak.provider.ProviderFactory)

Aggregations

PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 Certificate (java.security.cert.Certificate)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 SecretKey (javax.crypto.SecretKey)1 Logger (org.jboss.logging.Logger)1 ComponentModel (org.keycloak.component.ComponentModel)1 Algorithm (org.keycloak.crypto.Algorithm)1 KeyUse (org.keycloak.crypto.KeyUse)1 KeyWrapper (org.keycloak.crypto.KeyWrapper)1 KeyManager (org.keycloak.models.KeyManager)1 KeycloakSession (org.keycloak.models.KeycloakSession)1