Java Code Examples for org.keycloak.models.RealmModel#isRegistrationEmailAsUsername()
The following examples show how to use
org.keycloak.models.RealmModel#isRegistrationEmailAsUsername() .
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: Validation.java From keycloak with Apache License 2.0 | 6 votes |
public static List<FormMessage> validateUpdateProfileForm(RealmModel realm, MultivaluedMap<String, String> formData, boolean userNameRequired) { List<FormMessage> errors = new ArrayList<>(); if (!realm.isRegistrationEmailAsUsername() && userNameRequired && isBlank(formData.getFirst(FIELD_USERNAME))) { addError(errors, FIELD_USERNAME, Messages.MISSING_USERNAME); } if (isBlank(formData.getFirst(FIELD_FIRST_NAME))) { addError(errors, FIELD_FIRST_NAME, Messages.MISSING_FIRST_NAME); } if (isBlank(formData.getFirst(FIELD_LAST_NAME))) { addError(errors, FIELD_LAST_NAME, Messages.MISSING_LAST_NAME); } if (isBlank(formData.getFirst(FIELD_EMAIL))) { addError(errors, FIELD_EMAIL, Messages.MISSING_EMAIL); } else if (!isEmailValid(formData.getFirst(FIELD_EMAIL))) { addError(errors, FIELD_EMAIL, Messages.INVALID_EMAIL); } return errors; }
Example 2
Source File: AccountFormService.java From keycloak with Apache License 2.0 | 6 votes |
private void updateUsername(String username, UserModel user, KeycloakSession session) { RealmModel realm = session.getContext().getRealm(); boolean usernameChanged = username == null || !user.getUsername().equals(username); if (realm.isEditUsernameAllowed() && !realm.isRegistrationEmailAsUsername()) { if (usernameChanged) { UserModel existing = session.users().getUserByUsername(username, realm); if (existing != null && !existing.getId().equals(user.getId())) { throw new ModelDuplicateException(Messages.USERNAME_EXISTS); } user.setUsername(username); } } else if (usernameChanged) { } }
Example 3
Source File: Validation.java From keycloak with Apache License 2.0 | 5 votes |
public static List<FormMessage> validateRegistrationForm(KeycloakSession session, RealmModel realm, MultivaluedMap<String, String> formData, List<String> requiredCredentialTypes, PasswordPolicy policy) { List<FormMessage> errors = new ArrayList<>(); if (!realm.isRegistrationEmailAsUsername() && isBlank(formData.getFirst(FIELD_USERNAME))) { addError(errors, FIELD_USERNAME, Messages.MISSING_USERNAME); } if (isBlank(formData.getFirst(FIELD_FIRST_NAME))) { addError(errors, FIELD_FIRST_NAME, Messages.MISSING_FIRST_NAME); } if (isBlank(formData.getFirst(FIELD_LAST_NAME))) { addError(errors, FIELD_LAST_NAME, Messages.MISSING_LAST_NAME); } if (isBlank(formData.getFirst(FIELD_EMAIL))) { addError(errors, FIELD_EMAIL, Messages.MISSING_EMAIL); } else if (!isEmailValid(formData.getFirst(FIELD_EMAIL))) { addError(errors, FIELD_EMAIL, Messages.INVALID_EMAIL); } if (requiredCredentialTypes.contains(CredentialRepresentation.PASSWORD)) { if (isBlank(formData.getFirst(FIELD_PASSWORD))) { addError(errors, FIELD_PASSWORD, Messages.MISSING_PASSWORD); } else if (!formData.getFirst(FIELD_PASSWORD).equals(formData.getFirst(FIELD_PASSWORD_CONFIRM))) { addError(errors, FIELD_PASSWORD_CONFIRM, Messages.INVALID_PASSWORD_CONFIRM); } } if (formData.getFirst(FIELD_PASSWORD) != null) { PolicyError err = session.getProvider(PasswordPolicyManagerProvider.class).validate(realm.isRegistrationEmailAsUsername() ? formData.getFirst(FIELD_EMAIL) : formData.getFirst(FIELD_USERNAME), formData.getFirst(FIELD_PASSWORD)); if (err != null) errors.add(new FormMessage(FIELD_PASSWORD, err.getMessage(), err.getParameters())); } return errors; }
Example 4
Source File: UsernameTemplateMapper.java From keycloak with Apache License 2.0 | 5 votes |
@Override public void updateBrokeredUser(KeycloakSession session, RealmModel realm, UserModel user, IdentityProviderMapperModel mapperModel, BrokeredIdentityContext context) { // preprocessFederatedIdentity gets called anyways, so we only need to set the username if necessary. // However, we don't want to set the username when the email is used as username if (!realm.isRegistrationEmailAsUsername()) { user.setUsername(context.getModelUsername()); } }
Example 5
Source File: UsernameTemplateMapper.java From keycloak with Apache License 2.0 | 5 votes |
@Override public void updateBrokeredUser(KeycloakSession session, RealmModel realm, UserModel user, IdentityProviderMapperModel mapperModel, BrokeredIdentityContext context) { // preprocessFederatedIdentity gets called anyways, so we only need to set the username if necessary. // However, we don't want to set the username when the email is used as username if (!realm.isRegistrationEmailAsUsername()) { user.setUsername(context.getModelUsername()); } }
Example 6
Source File: IdpCreateUserIfUniqueAuthenticator.java From keycloak with Apache License 2.0 | 4 votes |
protected String getUsername(AuthenticationFlowContext context, SerializedBrokeredIdentityContext serializedCtx, BrokeredIdentityContext brokerContext) { RealmModel realm = context.getRealm(); return realm.isRegistrationEmailAsUsername() ? brokerContext.getEmail() : brokerContext.getModelUsername(); }
Example 7
Source File: IdpReviewProfileAuthenticator.java From keycloak with Apache License 2.0 | 4 votes |
@Override protected void actionImpl(AuthenticationFlowContext context, SerializedBrokeredIdentityContext userCtx, BrokeredIdentityContext brokerContext) { EventBuilder event = context.getEvent(); event.event(EventType.UPDATE_PROFILE); MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters(); RealmModel realm = context.getRealm(); List<FormMessage> errors = Validation.validateUpdateProfileForm(realm, formData, userCtx.isEditUsernameAllowed()); if (errors != null && !errors.isEmpty()) { Response challenge = context.form() .setErrors(errors) .setAttribute(LoginFormsProvider.UPDATE_PROFILE_CONTEXT_ATTR, userCtx) .setFormData(formData) .createUpdateProfilePage(); context.challenge(challenge); return; } String username = realm.isRegistrationEmailAsUsername() ? formData.getFirst(UserModel.EMAIL) : formData.getFirst(UserModel.USERNAME); userCtx.setUsername(username); userCtx.setFirstName(formData.getFirst(UserModel.FIRST_NAME)); userCtx.setLastName(formData.getFirst(UserModel.LAST_NAME)); String email = formData.getFirst(UserModel.EMAIL); if (!ObjectUtil.isEqualOrBothNull(email, userCtx.getEmail())) { if (logger.isTraceEnabled()) { logger.tracef("Email updated on updateProfile page to '%s' ", email); } userCtx.setEmail(email); context.getAuthenticationSession().setAuthNote(UPDATE_PROFILE_EMAIL_CHANGED, "true"); } AttributeFormDataProcessor.process(formData, realm, userCtx); userCtx.saveToAuthenticationSession(context.getAuthenticationSession(), BROKERED_CONTEXT_NOTE); logger.debugf("Profile updated successfully after first authentication with identity provider '%s' for broker user '%s'.", brokerContext.getIdpConfig().getAlias(), userCtx.getUsername()); event.detail(Details.UPDATED_EMAIL, email); // Ensure page is always shown when user later returns to it - for example with form "back" button context.getAuthenticationSession().setAuthNote(ENFORCE_UPDATE_PROFILE, "true"); context.success(); }