org.keycloak.models.utils

## Class KeycloakModelUtils

- java.lang.Object
- - org.keycloak.models.utils.KeycloakModelUtils

-     public final class KeycloakModelUtils
      extends Object

  Set of helper methods, which are useful in various model implementations.
  Author:
    Marek Posolda

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static ClientModel` | `createClient(RealmModel realm, String name)`
    `static UserFederationMapperModel` | `createUserFederationMapperModel(String name, String federationProviderId, String mapperType, String... config)`
    `static void` | `deepFindAuthenticationExecutions(RealmModel realm, AuthenticationFlowModel flow, List<AuthenticationExecutionModel> result)` Recursively find all AuthenticationExecutionModel from specified flow or all it's subflows
    `static void` | `ensureUniqueDisplayName(String displayName, UserFederationProviderModel myProvider, List<UserFederationProviderModel> federationProviders)` Ensure that displayName of myProvider (if not null) is unique and there is no other provider with same displayName in the list.
    `static GroupModel` | `findGroupByPath(RealmModel realm, String path)`
    `static UserModel` | `findUserByNameOrEmail(KeycloakSession session, RealmModel realm, String username)` Try to find user by username or email
    `static UserFederationProviderModel` | `findUserFederationProviderByDisplayName(String displayName, RealmModel realm)`
    `static UserFederationProviderModel` | `findUserFederationProviderById(String fedProviderId, RealmModel realm)`
    `static String` | `generateCodeSecret()`
    `static String` | `generateId()`
    `static CertificateRepresentation` | `generateKeyPairCertificate(String subject)`
    `static void` | `generateRealmCertificate(RealmModel realm)`
    `static void` | `generateRealmKeys(RealmModel realm)`
    `static String` | `generateSecret()`
    `static UserCredentialModel` | `generateSecret(ClientModel client)`
    `static String` | `generateSecret(int bytes)`
    `static X509Certificate` | `getCertificate(String cert)`
    `static Set<RoleModel>` | `getClientScopeMappings(ClientModel client, ScopeContainerModel container)`
    `static ClientTemplateModel` | `getClientTemplateByName(RealmModel realm, String templateName)`
    `static String` | `getDefaultClientAuthenticatorType()`
    `static UserFederationProviderFactory` | `getFederationProviderFactory(KeycloakSession session, UserFederationProviderModel model)`
    `static UserFederationProvider` | `getFederationProviderInstance(KeycloakSession session, UserFederationProviderModel model)`
    `static String` | `getMasterRealmAdminApplicationClientId(String realmName)`
    `static String` | `getPemFromCertificate(X509Certificate certificate)`
    `static String` | `getPemFromKey(Key key)`
    `static PrivateKey` | `getPrivateKey(String privateKeyPem)`
    `static PublicKey` | `getPublicKey(String publicKeyPem)`
    `static RoleModel` | `getRoleFromString(RealmModel realm, String roleName)`
    `static Key` | `getSecretKey(String secret)`
    `static boolean` | `hasRole(Set<RoleModel> roles, RoleModel targetRole)`
    `static boolean` | `isClientTemplateUsed(RealmModel realm, ClientTemplateModel template)`
    `static boolean` | `isFlowUsed(RealmModel realm, AuthenticationFlowModel model)` Check to see if a flow is currently in use
    `static boolean` | `isMember(Set<GroupModel> groups, GroupModel targetGroup)`
    `static String[]` | `parseRole(String role)`
    `static List<String>` | `resolveAttribute(GroupModel group, String name)`
    `static List<String>` | `resolveAttribute(UserModel user, String name)`
    `static String` | `resolveFirstAttribute(GroupModel group, String name)`
    `static String` | `resolveFirstAttribute(UserModel user, String name)`
    `static void` | `runJobInTransaction(KeycloakSessionFactory factory, KeycloakSessionTask task)` Wrap given runnable job into KeycloakTransaction.
    `static boolean` | `searchFor(RoleModel role, RoleModel composite, Set<RoleModel> visited)` Deep search if given role is descendant of composite role
    `static void` | `setupAuthorizationServices(RealmModel realm)`
    `static void` | `setupOfflineTokens(RealmModel realm)`
    `static String` | `toLowerCaseSafe(String str)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### generateId

          public static String generateId()

    - #### generateSecret

          public static String generateSecret()

    - #### generateSecret

          public static String generateSecret(int bytes)

    - #### getPublicKey

          public static PublicKey getPublicKey(String publicKeyPem)

    - #### getCertificate

          public static X509Certificate getCertificate(String cert)

    - #### getPrivateKey

          public static PrivateKey getPrivateKey(String privateKeyPem)

    - #### getSecretKey

          public static Key getSecretKey(String secret)

    - #### getPemFromKey

          public static String getPemFromKey(Key key)

    - #### getPemFromCertificate

          public static String getPemFromCertificate(X509Certificate certificate)

    - #### generateRealmKeys

          public static void generateRealmKeys(RealmModel realm)

    - #### generateRealmCertificate

          public static void generateRealmCertificate(RealmModel realm)

    - #### generateKeyPairCertificate

          public static CertificateRepresentation generateKeyPairCertificate(String subject)

    - #### generateSecret

          public static UserCredentialModel generateSecret(ClientModel client)

    - #### getDefaultClientAuthenticatorType

          public static String getDefaultClientAuthenticatorType()

    - #### generateCodeSecret

          public static String generateCodeSecret()

    - #### createClient

          public static ClientModel createClient(RealmModel realm,
                                                 String name)

    - #### searchFor

          public static boolean searchFor(RoleModel role,
                                          RoleModel composite,
                                          Set<RoleModel> visited)

      Deep search if given role is descendant of composite role
      Parameters:
        `role` - role to check
        `composite` - composite role
        `visited` - set of already visited roles (used for recursion)
      Returns:
        true if "role" is descendant of "composite"

    - #### findUserByNameOrEmail

          public static UserModel findUserByNameOrEmail(KeycloakSession session,
                                                        RealmModel realm,
                                                        String username)

      Try to find user by username or email
      Parameters:
        `realm` - realm
        `username` - username or email of user
      Returns:
        found user

    - #### runJobInTransaction

          public static void runJobInTransaction(KeycloakSessionFactory factory,
                                                 KeycloakSessionTask task)

      Wrap given runnable job into KeycloakTransaction.
      Parameters:
        `factory` -
        `task` -

    - #### getMasterRealmAdminApplicationClientId

          public static String getMasterRealmAdminApplicationClientId(String realmName)

    - #### hasRole

          public static boolean hasRole(Set<RoleModel> roles,
                                        RoleModel targetRole)

      Parameters:
        `roles` -
        `targetRole` -
      Returns:
        true if targetRole is in roles (directly or indirectly via composite role)

    - #### isMember

          public static boolean isMember(Set<GroupModel> groups,
                                         GroupModel targetGroup)

      Parameters:
        `groups` -
        `targetGroup` -
      Returns:
        true if targetGroup is in groups (directly or indirectly via parent child relationship)

    - #### ensureUniqueDisplayName

          public static void ensureUniqueDisplayName(String displayName,
                                                     UserFederationProviderModel myProvider,
                                                     List<UserFederationProviderModel> federationProviders)
                                              throws ModelDuplicateException

      Ensure that displayName of myProvider (if not null) is unique and there is no other provider with same displayName in the list.
      Parameters:
        `displayName` - to check for duplications
        `myProvider` - provider, which is excluded from the list (if present)
        `federationProviders` -
      Throws:
        `ModelDuplicateException` - if there is other provider with same displayName

    - #### findUserFederationProviderByDisplayName

          public static UserFederationProviderModel findUserFederationProviderByDisplayName(String displayName,
                                                                                            RealmModel realm)

    - #### findUserFederationProviderById

          public static UserFederationProviderModel findUserFederationProviderById(String fedProviderId,
                                                                                   RealmModel realm)

    - #### createUserFederationMapperModel

          public static UserFederationMapperModel createUserFederationMapperModel(String name,
                                                                                  String federationProviderId,
                                                                                  String mapperType,
                                                                                  String... config)

    - #### getFederationProviderFactory

          public static UserFederationProviderFactory getFederationProviderFactory(KeycloakSession session,
                                                                                   UserFederationProviderModel model)

    - #### getFederationProviderInstance

          public static UserFederationProvider getFederationProviderInstance(KeycloakSession session,
                                                                             UserFederationProviderModel model)

    - #### toLowerCaseSafe

          public static String toLowerCaseSafe(String str)

    - #### setupOfflineTokens

          public static void setupOfflineTokens(RealmModel realm)

    - #### deepFindAuthenticationExecutions

          public static void deepFindAuthenticationExecutions(RealmModel realm,
                                                              AuthenticationFlowModel flow,
                                                              List<AuthenticationExecutionModel> result)

      Recursively find all AuthenticationExecutionModel from specified flow or all it's subflows
      Parameters:
        `realm` -
        `flow` -
        `result` - input should be empty list. At the end will be all executions added to this list

    - #### resolveFirstAttribute

          public static String resolveFirstAttribute(GroupModel group,
                                                     String name)

    - #### resolveFirstAttribute

          public static String resolveFirstAttribute(UserModel user,
                                                     String name)

      Parameters:
        `user` -
        `name` -
      Returns:

    - #### resolveAttribute

          public static List<String> resolveAttribute(GroupModel group,
                                                      String name)

    - #### resolveAttribute

          public static List<String> resolveAttribute(UserModel user,
                                                      String name)

    - #### findGroupByPath

          public static GroupModel findGroupByPath(RealmModel realm,
                                                   String path)

    - #### getClientScopeMappings

          public static Set<RoleModel> getClientScopeMappings(ClientModel client,
                                                              ScopeContainerModel container)

    - #### getRoleFromString

          public static RoleModel getRoleFromString(RealmModel realm,
                                                    String roleName)

    - #### parseRole

          public static String[] parseRole(String role)

    - #### isFlowUsed

          public static boolean isFlowUsed(RealmModel realm,
                                           AuthenticationFlowModel model)

      Check to see if a flow is currently in use
      Parameters:
        `realm` -
        `model` -
      Returns:

    - #### isClientTemplateUsed

          public static boolean isClientTemplateUsed(RealmModel realm,
                                                     ClientTemplateModel template)

    - #### getClientTemplateByName

          public static ClientTemplateModel getClientTemplateByName(RealmModel realm,
                                                                    String templateName)

    - #### setupAuthorizationServices

          public static void setupAuthorizationServices(RealmModel realm)

Copyright © 2016 JBoss by Red Hat. All rights reserved.