Search in sources :

Example 1 with LocaleBean

use of org.keycloak.theme.beans.LocaleBean in project keycloak by keycloak.

the class FreeMarkerAccountProvider method createResponse.

@Override
public Response createResponse(AccountPages page) {
    Map<String, Object> attributes = new HashMap<>();
    if (this.attributes != null) {
        attributes.putAll(this.attributes);
    }
    Theme theme;
    try {
        theme = getTheme();
    } catch (IOException e) {
        logger.error("Failed to create theme", e);
        return Response.serverError().build();
    }
    Locale locale = session.getContext().resolveLocale(user);
    Properties messagesBundle = handleThemeResources(theme, locale, attributes);
    URI baseUri = uriInfo.getBaseUri();
    UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
    for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
        baseUriBuilder.queryParam(e.getKey(), e.getValue().toArray());
    }
    URI baseQueryUri = baseUriBuilder.build();
    if (stateChecker != null) {
        attributes.put("stateChecker", stateChecker);
    }
    handleMessages(locale, messagesBundle, attributes);
    if (referrer != null) {
        attributes.put("referrer", new ReferrerBean(referrer));
    }
    if (realm != null) {
        attributes.put("realm", new RealmBean(realm));
    }
    attributes.put("url", new UrlBean(realm, theme, baseUri, baseQueryUri, uriInfo.getRequestUri(), stateChecker));
    if (realm.isInternationalizationEnabled()) {
        UriBuilder b = UriBuilder.fromUri(baseQueryUri).path(uriInfo.getPath());
        attributes.put("locale", new LocaleBean(realm, locale, b, messagesBundle));
    }
    attributes.put("features", new FeaturesBean(identityProviderEnabled, eventsEnabled, passwordUpdateSupported, authorizationSupported));
    attributes.put("account", new AccountBean(user, profileFormData));
    switch(page) {
        case TOTP:
            attributes.put("totp", new TotpBean(session, realm, user, uriInfo.getRequestUriBuilder()));
            break;
        case FEDERATED_IDENTITY:
            attributes.put("federatedIdentity", new AccountFederatedIdentityBean(session, realm, user, uriInfo.getBaseUri(), stateChecker));
            break;
        case LOG:
            attributes.put("log", new LogBean(events));
            break;
        case SESSIONS:
            attributes.put("sessions", new SessionsBean(realm, sessions));
            break;
        case APPLICATIONS:
            attributes.put("applications", new ApplicationsBean(session, realm, user));
            attributes.put("advancedMsg", new AdvancedMessageFormatterMethod(locale, messagesBundle));
            break;
        case PASSWORD:
            attributes.put("password", new PasswordBean(passwordSet));
            break;
        case RESOURCES:
            if (!realm.isUserManagedAccessAllowed()) {
                return Response.status(Status.FORBIDDEN).build();
            }
            attributes.put("authorization", new AuthorizationBean(session, user, uriInfo));
        case RESOURCE_DETAIL:
            if (!realm.isUserManagedAccessAllowed()) {
                return Response.status(Status.FORBIDDEN).build();
            }
            attributes.put("authorization", new AuthorizationBean(session, user, uriInfo));
    }
    return processTemplate(theme, page, attributes, locale);
}
Also used : Locale(java.util.Locale) ApplicationsBean(org.keycloak.forms.account.freemarker.model.ApplicationsBean) AccountFederatedIdentityBean(org.keycloak.forms.account.freemarker.model.AccountFederatedIdentityBean) PasswordBean(org.keycloak.forms.account.freemarker.model.PasswordBean) HashMap(java.util.HashMap) Properties(java.util.Properties) FeaturesBean(org.keycloak.forms.account.freemarker.model.FeaturesBean) URI(java.net.URI) LocaleBean(org.keycloak.theme.beans.LocaleBean) UrlBean(org.keycloak.forms.account.freemarker.model.UrlBean) TotpBean(org.keycloak.forms.account.freemarker.model.TotpBean) RealmBean(org.keycloak.forms.account.freemarker.model.RealmBean) ArrayList(java.util.ArrayList) List(java.util.List) UriBuilder(javax.ws.rs.core.UriBuilder) AccountBean(org.keycloak.forms.account.freemarker.model.AccountBean) AdvancedMessageFormatterMethod(org.keycloak.theme.beans.AdvancedMessageFormatterMethod) SessionsBean(org.keycloak.forms.account.freemarker.model.SessionsBean) IOException(java.io.IOException) AuthorizationBean(org.keycloak.forms.account.freemarker.model.AuthorizationBean) Theme(org.keycloak.theme.Theme) LogBean(org.keycloak.forms.account.freemarker.model.LogBean) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ReferrerBean(org.keycloak.forms.account.freemarker.model.ReferrerBean)

Example 2 with LocaleBean

use of org.keycloak.theme.beans.LocaleBean in project keycloak by keycloak.

the class FreeMarkerLoginFormsProvider method createCommonAttributes.

/**
 * Create common attributes used in all templates.
 *
 * @param theme actual Theme used (provided by <code>getTheme()</code>)
 * @param locale actual locale
 * @param messagesBundle actual message bundle (provided by <code>handleThemeResources()</code>)
 * @param baseUriBuilder actual base uri builder (provided by <code>prepareBaseUriBuilder()</code>)
 * @param page in case if common page is rendered, is null if called from <code>createForm()</code>
 */
protected void createCommonAttributes(Theme theme, Locale locale, Properties messagesBundle, UriBuilder baseUriBuilder, LoginFormsPages page) {
    URI baseUri = baseUriBuilder.build();
    if (accessCode != null) {
        baseUriBuilder.queryParam(LoginActionsService.SESSION_CODE, accessCode);
    }
    URI baseUriWithCodeAndClientId = baseUriBuilder.build();
    if (client != null) {
        attributes.put("client", new ClientBean(session, client));
    }
    if (realm != null) {
        attributes.put("realm", new RealmBean(realm));
        List<IdentityProviderModel> identityProviders = LoginFormsUtil.filterIdentityProviders(realm.getIdentityProvidersStream(), session, context);
        attributes.put("social", new IdentityProviderBean(realm, session, identityProviders, baseUriWithCodeAndClientId));
        attributes.put("url", new UrlBean(realm, theme, baseUri, this.actionUri));
        attributes.put("requiredActionUrl", new RequiredActionUrlFormatterMethod(realm, baseUri));
        attributes.put("auth", new AuthenticationContextBean(context, page));
        attributes.put(Constants.EXECUTION, execution);
        if (realm.isInternationalizationEnabled()) {
            UriBuilder b;
            if (page != null) {
                switch(page) {
                    case LOGIN:
                    case LOGIN_USERNAME:
                    case X509_CONFIRM:
                        b = UriBuilder.fromUri(Urls.realmLoginPage(baseUri, realm.getName()));
                        break;
                    case REGISTER:
                        b = UriBuilder.fromUri(Urls.realmRegisterPage(baseUri, realm.getName()));
                        break;
                    default:
                        b = UriBuilder.fromUri(baseUri).path(uriInfo.getPath());
                        break;
                }
            } else {
                b = UriBuilder.fromUri(baseUri).path(uriInfo.getPath());
            }
            if (execution != null) {
                b.queryParam(Constants.EXECUTION, execution);
            }
            if (authenticationSession != null && authenticationSession.getAuthNote(Constants.KEY) != null) {
                b.queryParam(Constants.KEY, authenticationSession.getAuthNote(Constants.KEY));
            }
            attributes.put("locale", new LocaleBean(realm, locale, b, messagesBundle));
        }
    }
    if (realm != null && user != null && session != null) {
        attributes.put("authenticatorConfigured", new AuthenticatorConfiguredMethod(realm, user, session));
    }
    if (authenticationSession != null && authenticationSession.getClientNote(Constants.KC_ACTION_EXECUTING) != null) {
        attributes.put("isAppInitiatedAction", true);
    }
}
Also used : RequiredActionUrlFormatterMethod(org.keycloak.forms.login.freemarker.model.RequiredActionUrlFormatterMethod) IdentityProviderBean(org.keycloak.forms.login.freemarker.model.IdentityProviderBean) ClientBean(org.keycloak.forms.login.freemarker.model.ClientBean) UrlBean(org.keycloak.forms.login.freemarker.model.UrlBean) AuthenticationContextBean(org.keycloak.forms.login.freemarker.model.AuthenticationContextBean) RealmBean(org.keycloak.forms.login.freemarker.model.RealmBean) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) LocaleBean(org.keycloak.theme.beans.LocaleBean)

Example 3 with LocaleBean

use of org.keycloak.theme.beans.LocaleBean in project keycloak by keycloak.

the class KeycloakErrorHandler method initAttributes.

private Map<String, Object> initAttributes(KeycloakSession session, RealmModel realm, Theme theme, Locale locale, int statusCode) throws IOException {
    Map<String, Object> attributes = new HashMap<>();
    Properties messagesBundle = theme.getMessages(locale);
    attributes.put("statusCode", statusCode);
    attributes.put("realm", realm);
    attributes.put("url", new UrlBean(realm, theme, session.getContext().getUri().getBaseUri(), null));
    attributes.put("locale", new LocaleBean(realm, locale, session.getContext().getUri().getBaseUriBuilder(), messagesBundle));
    String errorKey = statusCode == 404 ? Messages.PAGE_NOT_FOUND : Messages.INTERNAL_SERVER_ERROR;
    String errorMessage = messagesBundle.getProperty(errorKey);
    attributes.put("message", new MessageBean(errorMessage, MessageType.ERROR));
    try {
        attributes.put("msg", new MessageFormatterMethod(locale, theme.getMessages(locale)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        attributes.put("properties", theme.getProperties());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return attributes;
}
Also used : MessageBean(org.keycloak.theme.beans.MessageBean) UrlBean(org.keycloak.forms.login.freemarker.model.UrlBean) HashMap(java.util.HashMap) IOException(java.io.IOException) Properties(java.util.Properties) MessageFormatterMethod(org.keycloak.theme.beans.MessageFormatterMethod) LocaleBean(org.keycloak.theme.beans.LocaleBean)

Aggregations

LocaleBean (org.keycloak.theme.beans.LocaleBean)3 IOException (java.io.IOException)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 UrlBean (org.keycloak.forms.login.freemarker.model.UrlBean)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 AccountBean (org.keycloak.forms.account.freemarker.model.AccountBean)1 AccountFederatedIdentityBean (org.keycloak.forms.account.freemarker.model.AccountFederatedIdentityBean)1 ApplicationsBean (org.keycloak.forms.account.freemarker.model.ApplicationsBean)1 AuthorizationBean (org.keycloak.forms.account.freemarker.model.AuthorizationBean)1 FeaturesBean (org.keycloak.forms.account.freemarker.model.FeaturesBean)1 LogBean (org.keycloak.forms.account.freemarker.model.LogBean)1 PasswordBean (org.keycloak.forms.account.freemarker.model.PasswordBean)1 RealmBean (org.keycloak.forms.account.freemarker.model.RealmBean)1