Search in sources :

Example 11 with FormMessage

use of org.keycloak.models.utils.FormMessage in project keycloak by keycloak.

the class FreeMarkerAccountProvider method setMessage.

protected void setMessage(MessageType type, String message, Object... parameters) {
    messageType = type;
    messages = new ArrayList<>();
    messages.add(new FormMessage(null, message, parameters));
}
Also used : FormMessage(org.keycloak.models.utils.FormMessage)

Example 12 with FormMessage

use of org.keycloak.models.utils.FormMessage in project keycloak by keycloak.

the class FreeMarkerLoginFormsProvider method getMessage.

@Override
public String getMessage(String message) {
    Theme theme;
    try {
        theme = getTheme();
    } catch (IOException e) {
        logger.error("Failed to create theme", e);
        throw new RuntimeException("Failed to create theme");
    }
    Locale locale = session.getContext().resolveLocale(user);
    Properties messagesBundle = handleThemeResources(theme, locale);
    Map<String, String> localizationTexts = realm.getRealmLocalizationTextsByLocale(locale.getCountry());
    messagesBundle.putAll(localizationTexts);
    FormMessage msg = new FormMessage(null, message);
    return formatMessage(msg, messagesBundle, locale);
}
Also used : Locale(java.util.Locale) Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) Properties(java.util.Properties) FormMessage(org.keycloak.models.utils.FormMessage)

Example 13 with FormMessage

use of org.keycloak.models.utils.FormMessage in project keycloak by keycloak.

the class UpdatePassword method processAction.

@Override
public void processAction(RequiredActionContext context) {
    EventBuilder event = context.getEvent();
    AuthenticationSessionModel authSession = context.getAuthenticationSession();
    RealmModel realm = context.getRealm();
    UserModel user = context.getUser();
    KeycloakSession session = context.getSession();
    MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
    event.event(EventType.UPDATE_PASSWORD);
    String passwordNew = formData.getFirst("password-new");
    String passwordConfirm = formData.getFirst("password-confirm");
    EventBuilder errorEvent = event.clone().event(EventType.UPDATE_PASSWORD_ERROR).client(authSession.getClient()).user(authSession.getAuthenticatedUser());
    if (Validation.isBlank(passwordNew)) {
        Response challenge = context.form().setAttribute("username", authSession.getAuthenticatedUser().getUsername()).addError(new FormMessage(Validation.FIELD_PASSWORD, Messages.MISSING_PASSWORD)).createResponse(UserModel.RequiredAction.UPDATE_PASSWORD);
        context.challenge(challenge);
        errorEvent.error(Errors.PASSWORD_MISSING);
        return;
    } else if (!passwordNew.equals(passwordConfirm)) {
        Response challenge = context.form().setAttribute("username", authSession.getAuthenticatedUser().getUsername()).addError(new FormMessage(Validation.FIELD_PASSWORD_CONFIRM, Messages.NOTMATCH_PASSWORD)).createResponse(UserModel.RequiredAction.UPDATE_PASSWORD);
        context.challenge(challenge);
        errorEvent.error(Errors.PASSWORD_CONFIRM_ERROR);
        return;
    }
    if (getId().equals(authSession.getClientNote(Constants.KC_ACTION_EXECUTING)) && "on".equals(formData.getFirst("logout-sessions"))) {
        session.sessions().getUserSessionsStream(realm, user).filter(s -> !Objects.equals(s.getId(), authSession.getParentSession().getId())).collect(// collect to avoid concurrent modification as backchannelLogout removes the user sessions.
        Collectors.toList()).forEach(s -> AuthenticationManager.backchannelLogout(session, realm, s, session.getContext().getUri(), context.getConnection(), context.getHttpRequest().getHttpHeaders(), true));
    }
    try {
        session.userCredentialManager().updateCredential(realm, user, UserCredentialModel.password(passwordNew, false));
        context.success();
    } catch (ModelException me) {
        errorEvent.detail(Details.REASON, me.getMessage()).error(Errors.PASSWORD_REJECTED);
        Response challenge = context.form().setAttribute("username", authSession.getAuthenticatedUser().getUsername()).setError(me.getMessage(), me.getParameters()).createResponse(UserModel.RequiredAction.UPDATE_PASSWORD);
        context.challenge(challenge);
        return;
    } catch (Exception ape) {
        errorEvent.detail(Details.REASON, ape.getMessage()).error(Errors.PASSWORD_REJECTED);
        Response challenge = context.form().setAttribute("username", authSession.getAuthenticatedUser().getUsername()).setError(ape.getMessage()).createResponse(UserModel.RequiredAction.UPDATE_PASSWORD);
        context.challenge(challenge);
        return;
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) Response(javax.ws.rs.core.Response) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) EventBuilder(org.keycloak.events.EventBuilder) ModelException(org.keycloak.models.ModelException) KeycloakSession(org.keycloak.models.KeycloakSession) FormMessage(org.keycloak.models.utils.FormMessage) ModelException(org.keycloak.models.ModelException)

Example 14 with FormMessage

use of org.keycloak.models.utils.FormMessage in project keycloak by keycloak.

the class AuthenticationProcessor method handleBrowserExceptionList.

public Response handleBrowserExceptionList(AuthenticationFlowException e) {
    LoginFormsProvider forms = session.getProvider(LoginFormsProvider.class).setAuthenticationSession(authenticationSession);
    ServicesLogger.LOGGER.failedAuthentication(e);
    forms.addError(new FormMessage(Messages.UNEXPECTED_ERROR_HANDLING_REQUEST));
    for (AuthenticationFlowException afe : e.getAfeList()) {
        ServicesLogger.LOGGER.failedAuthentication(afe);
        switch(afe.getError()) {
            case INVALID_USER:
                event.error(Errors.USER_NOT_FOUND);
                forms.addError(new FormMessage(Messages.INVALID_USER));
                break;
            case USER_DISABLED:
                event.error(Errors.USER_DISABLED);
                forms.addError(new FormMessage(Messages.ACCOUNT_DISABLED));
                break;
            case USER_TEMPORARILY_DISABLED:
                event.error(Errors.USER_TEMPORARILY_DISABLED);
                forms.addError(new FormMessage(Messages.INVALID_USER));
                break;
            case INVALID_CLIENT_SESSION:
                event.error(Errors.INVALID_CODE);
                forms.addError(new FormMessage(Messages.INVALID_CODE));
                break;
            case EXPIRED_CODE:
                event.error(Errors.EXPIRED_CODE);
                forms.addError(new FormMessage(Messages.EXPIRED_CODE));
                break;
            case DISPLAY_NOT_SUPPORTED:
                event.error(Errors.DISPLAY_UNSUPPORTED);
                forms.addError(new FormMessage(Messages.DISPLAY_UNSUPPORTED));
                break;
            case CREDENTIAL_SETUP_REQUIRED:
                event.error(Errors.INVALID_USER_CREDENTIALS);
                forms.addError(new FormMessage(Messages.CREDENTIAL_SETUP_REQUIRED));
                break;
        }
    }
    return forms.createErrorPage(Response.Status.BAD_REQUEST);
}
Also used : LoginFormsProvider(org.keycloak.forms.login.LoginFormsProvider) FormMessage(org.keycloak.models.utils.FormMessage)

Example 15 with FormMessage

use of org.keycloak.models.utils.FormMessage in project keycloak by keycloak.

the class FormAuthenticationFlow method processFlow.

@Override
public Response processFlow() {
    // KEYCLOAK-16143: Propagate forwarded error messages if present
    FormMessage forwardedErrorMessage = processor.getAndRemoveForwardedErrorMessage();
    List<FormMessage> errors = forwardedErrorMessage != null ? Collections.singletonList(forwardedErrorMessage) : null;
    return renderForm(null, errors);
}
Also used : FormMessage(org.keycloak.models.utils.FormMessage)

Aggregations

FormMessage (org.keycloak.models.utils.FormMessage)20 Response (javax.ws.rs.core.Response)5 IOException (java.io.IOException)4 EventBuilder (org.keycloak.events.EventBuilder)4 LoginFormsProvider (org.keycloak.forms.login.LoginFormsProvider)3 UserModel (org.keycloak.models.UserModel)3 AuthenticationSessionModel (org.keycloak.sessions.AuthenticationSessionModel)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 AuthenticatorConfigModel (org.keycloak.models.AuthenticatorConfigModel)2 RealmModel (org.keycloak.models.RealmModel)2 MessageBean (org.keycloak.theme.beans.MessageBean)2 MessagesPerFieldBean (org.keycloak.theme.beans.MessagesPerFieldBean)2 URI (java.net.URI)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 Map (java.util.Map)1