Java Code Examples for org.keycloak.models.UserModel#getFirstAttribute()

The following examples show how to use org.keycloak.models.UserModel#getFirstAttribute() . 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: SamlProtocol.java    From keycloak with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to retrieve the persistent type NameId as follows:
 *
 * <ol>
 *     <li>saml.persistent.name.id.for.$clientId user attribute</li>
 *     <li>saml.persistent.name.id.for.* user attribute</li>
 *     <li>G-$randomUuid</li>
 * </ol>
 *
 * If a randomUuid is generated, an attribute for the given saml.persistent.name.id.for.$clientId will be generated,
 * otherwise no state change will occur with respect to the user's attributes.
 *
 * @return the user's persistent NameId
 */
protected String getPersistentNameId(final CommonClientSessionModel clientSession, final UserSessionModel userSession) {
    // attempt to retrieve the UserID for the client-specific attribute
    final UserModel user = userSession.getUser();
    final String clientNameId = String.format("%s.%s", SAML_PERSISTENT_NAME_ID_FOR,
            clientSession.getClient().getClientId());
    String samlPersistentNameId = user.getFirstAttribute(clientNameId);
    if (samlPersistentNameId != null) {
        return samlPersistentNameId;
    }

    // check for a wildcard attribute
    final String wildcardNameId = String.format("%s.*", SAML_PERSISTENT_NAME_ID_FOR);
    samlPersistentNameId = user.getFirstAttribute(wildcardNameId);
    if (samlPersistentNameId != null) {
        return samlPersistentNameId;
    }

    // default to generated.  "G-" stands for "generated"
    samlPersistentNameId = "G-" + UUID.randomUUID().toString();
    user.setSingleAttribute(clientNameId, samlPersistentNameId);
    return samlPersistentNameId;
}
 
Example 2
Source File: LoginNotifyEmailAuthenticator.java    From keycloak-extension-playground with Apache License 2.0 5 votes vote down vote up
protected long detectLastLoginTimeForUser(UserModel user, long currentLoginTime) {

        long lastLoginTime = currentLoginTime;

        String lastLoginTimeString = user.getFirstAttribute("lastLoginTime");
        if (lastLoginTimeString != null) {
            lastLoginTime = Long.parseLong(lastLoginTimeString);
        }

        return lastLoginTime;
    }
 
Example 3
Source File: KeycloakModelUtils.java    From keycloak with Apache License 2.0 5 votes vote down vote up
/**
 *
 *
 * @param user
 * @param name
 * @return
 */
public static String resolveFirstAttribute(UserModel user, String name) {
    String value = user.getFirstAttribute(name);
    if (value != null) return value;
    for (GroupModel group : user.getGroups()) {
        value = resolveFirstAttribute(group, name);
        if (value != null) return value;
    }
    return null;

}
 
Example 4
Source File: AddressMapper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private String getUserModelAttributeValue(UserModel user, ProtocolMapperModel mappingModel, String claim) {
    String modelPropertyName = getModelPropertyName(claim);
    String userAttrName = mappingModel.getConfig().get(modelPropertyName);

    if (userAttrName == null) {
        userAttrName = claim;
    }

    return user.getFirstAttribute(userAttrName);
}
 
Example 5
Source File: DefaultLocaleSelectorProvider.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private Locale getUserProfileSelection(RealmModel realm, UserModel user) {
    if (user == null) {
        return null;
    }

    String locale = user.getFirstAttribute(UserModel.LOCALE);
    if (locale == null) {
        return null;
    }

    return findLocale(realm, locale);
}
 
Example 6
Source File: DynamicIdpRedirectAuthenticator.java    From keycloak-extension-playground with Apache License 2.0 4 votes vote down vote up
protected String determineTargetIdpViaAttribute(UserModel user) {
    return user.getFirstAttribute(TARGET_IDP_ATTRIBUTE);
}
 
Example 7
Source File: LDAPMSADFullNameTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private static void assertDnStartsWith(KeycloakSession session, LDAPTestContext ctx, UserModel user, String expectedRDn) {
    String usersDn = ctx.getLdapProvider().getLdapIdentityStore().getConfig().getUsersDn();
    String userDN = user.getFirstAttribute(LDAPConstants.LDAP_ENTRY_DN);
    Assert.assertTrue(userDN.equalsIgnoreCase(expectedRDn + "," + usersDn));
}