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

The following examples show how to use org.keycloak.models.UserModel#getAttribute() . 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: KeycloakModelUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static Collection<String> resolveAttribute(UserModel user, String name, boolean aggregateAttrs) {
    List<String> values = user.getAttribute(name);
    Set<String> aggrValues = new HashSet<String>();
    if (!values.isEmpty()) {
        if (!aggregateAttrs) {
            return values;
        }
        aggrValues.addAll(values);
    }
    for (GroupModel group : user.getGroups()) {
        values = resolveAttribute(group, name);
        if (values != null && !values.isEmpty()) {
            if (!aggregateAttrs) {
                return values;
            }
            aggrValues.addAll(values);
        }
    }
    return aggrValues;
}
 
Example 2
Source File: SetUserAttributeAuthenticator.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void authenticate(AuthenticationFlowContext context) {
    // Retrieve configuration
    Map<String, String> config = context.getAuthenticatorConfig().getConfig();
    String attrName = config.get(SetUserAttributeAuthenticatorFactory.CONF_ATTR_NAME);
    String attrValue = config.get(SetUserAttributeAuthenticatorFactory.CONF_ATTR_VALUE);

    UserModel user = context.getUser();
    if (user.getAttribute(attrName) == null) {
        user.setSingleAttribute(attrName, attrValue);
    }
    else {
        List<String> attrValues = new ArrayList<>(user.getAttribute(attrName));
        if (!attrValues.contains(attrValue)) {
            attrValues.add(attrValue);
        }
        user.setAttribute(attrName, attrValues);
    }

    context.success();
}
 
Example 3
Source File: ConditionalUserAttributeValue.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matchCondition(AuthenticationFlowContext context) {
    boolean result = false;

    // Retrieve configuration
    Map<String, String> config = context.getAuthenticatorConfig().getConfig();
    String attributeName = config.get(ConditionalUserAttributeValueFactory.CONF_ATTRIBUTE_NAME);
    String attributeValue = config.get(ConditionalUserAttributeValueFactory.CONF_ATTRIBUTE_EXPECTED_VALUE);
    boolean negateOutput = Boolean.parseBoolean(config.get(ConditionalUserAttributeValueFactory.CONF_NOT));

    UserModel user = context.getUser();
    if (user == null) {
        throw new AuthenticationFlowException("authenticator: " + ConditionalUserAttributeValueFactory.PROVIDER_ID, AuthenticationFlowError.UNKNOWN_USER);
    }

    List<String> lstValues = user.getAttribute(attributeName);
    if (lstValues != null) {
        result = lstValues.contains(attributeValue);
    }

    if (negateOutput) {
        result = !result;
    }

    return result;
}
 
Example 4
Source File: UserAttributeMapper.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void updateBrokeredUser(KeycloakSession session, RealmModel realm, UserModel user, IdentityProviderMapperModel mapperModel, BrokeredIdentityContext context) {
    String attribute = mapperModel.getConfig().get(USER_ATTRIBUTE);
    if(StringUtil.isNullOrEmpty(attribute)){
        return;
    }
    Object value = getClaimValue(mapperModel, context);
    List<String> values = toList(value);
    if (EMAIL.equalsIgnoreCase(attribute)) {
        setIfNotEmpty(user::setEmail, values);
    } else if (FIRST_NAME.equalsIgnoreCase(attribute)) {
        setIfNotEmpty(user::setFirstName, values);
    } else if (LAST_NAME.equalsIgnoreCase(attribute)) {
        setIfNotEmpty(user::setLastName, values);
    } else {
        List<String> current = user.getAttribute(attribute);
        if (!CollectionUtil.collectionEquals(values, current)) {
            user.setAttribute(attribute, values);
        } else if (values.isEmpty()) {
            user.removeAttribute(attribute);
        }
    }
}
 
Example 5
Source File: WebAuthnAuthenticatorsBean.java    From keycloak-webauthn-authenticator with Apache License 2.0 5 votes vote down vote up
public WebAuthnAuthenticatorsBean(UserModel user) {
    // should consider multiple credentials in the future, but only single credential supported now.
    List<String> credentialIds = user.getAttribute(WebAuthnConstants.PUBKEY_CRED_ID_ATTR);
    List<String> labels = user.getAttribute(WebAuthnConstants.PUBKEY_CRED_LABEL_ATTR);
    if (credentialIds != null && credentialIds.size() == 1 && !credentialIds.get(0).isEmpty()) {
        String credentialId = credentialIds.get(0);
        String label = (labels.size() == 1 && !labels.get(0).isEmpty()) ? labels.get(0) : "label missing";
        authenticators.add(new WebAuthnAuthenticatorBean(credentialId, label));
    }
}
 
Example 6
Source File: SMSAuthenticatorUtil.java    From keycloak-sms-authenticator with Eclipse Public License 2.0 5 votes vote down vote up
public static String getAttributeValue(UserModel user, String attributeName) {
    String result = null;
    List<String> values = user.getAttribute(attributeName);
    if(values != null && values.size() > 0) {
        result = values.get(0);
    }

    return result;
}
 
Example 7
Source File: ConditionalOtpFormAuthenticator.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private OtpDecision voteForUserOtpControlAttribute(UserModel user, Map<String, String> config) {

        if (!config.containsKey(OTP_CONTROL_USER_ATTRIBUTE)) {
            return ABSTAIN;
        }

        String attributeName = config.get(OTP_CONTROL_USER_ATTRIBUTE);
        if (attributeName == null) {
            return ABSTAIN;
        }

        List<String> values = user.getAttribute(attributeName);

        if (values.isEmpty()) {
            return ABSTAIN;
        }

        String value = values.get(0).trim();

        switch (value) {
            case SKIP:
                return SKIP_OTP;
            case FORCE:
                return SHOW_OTP;
            default:
                return ABSTAIN;
        }
    }
 
Example 8
Source File: UserAttributeLDAPStorageMapper.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void onRegisterUserToLDAP(LDAPObject ldapUser, UserModel localUser, RealmModel realm) {
    String userModelAttrName = getUserModelAttribute();
    String ldapAttrName = getLdapAttributeName();
    boolean isMandatoryInLdap = parseBooleanParameter(mapperModel, IS_MANDATORY_IN_LDAP);

    Property<Object> userModelProperty = userModelProperties.get(userModelAttrName.toLowerCase());

    if (userModelProperty != null) {

        // we have java property on UserModel. Assuming we support just properties of simple types
        Object attrValue = userModelProperty.getValue(localUser);

        if (attrValue == null) {
            if (isMandatoryInLdap) {
                ldapUser.setSingleAttribute(ldapAttrName, LDAPConstants.EMPTY_ATTRIBUTE_VALUE);
            } else {
                ldapUser.setAttribute(ldapAttrName, new LinkedHashSet<String>());
            }
        } else {
            ldapUser.setSingleAttribute(ldapAttrName, attrValue.toString());
        }
    } else {

        // we don't have java property. Let's set attribute
        List<String> attrValues = localUser.getAttribute(userModelAttrName);

        if (attrValues.size() == 0) {
            if (isMandatoryInLdap) {
                ldapUser.setSingleAttribute(ldapAttrName, LDAPConstants.EMPTY_ATTRIBUTE_VALUE);
            } else {
                ldapUser.setAttribute(ldapAttrName, new LinkedHashSet<String>());
            }
        } else {
            ldapUser.setAttribute(ldapAttrName, new LinkedHashSet<>(attrValues));
        }
    }

    if (isReadOnly()) {
        ldapUser.addReadOnlyAttributeName(ldapAttrName);
    }
}