Search in sources :

Example 6 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class ThemeResourceProviderTest method getResourceAsStream.

@Test
public void getResourceAsStream() {
    testingClient.server().run(session -> {
        try {
            Theme theme = session.theme().getTheme("base", Theme.Type.LOGIN);
            Assert.assertNotNull(theme.getResourceAsStream("test.js"));
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 7 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class ThemeResourceProviderTest method getMessagesLocaleResolving.

/**
 * See KEYCLOAK-12926
 */
@Test
public void getMessagesLocaleResolving() {
    testingClient.server().run(session -> {
        try {
            Theme theme = session.theme().getTheme("base", Theme.Type.LOGIN);
            assertEquals("Test en_US_variant", theme.getMessages("messages", new Locale("en", "US", "variant")).get("test.keycloak-12926"));
            assertEquals("Test en_US", theme.getMessages("messages", new Locale("en", "US")).get("test.keycloak-12926"));
            assertEquals("Test en", theme.getMessages("messages", Locale.ENGLISH).get("test.keycloak-12926"));
            assertEquals("Test en_US", theme.getMessages("messages", new Locale("en", "US")).get("test.keycloak-12926"));
            assertEquals("Test en", theme.getMessages("messages", Locale.ENGLISH).get("test.keycloak-12926"));
            assertEquals("only de_AT_variant", theme.getMessages("messages", new Locale("de", "AT", "variant")).get("test.keycloak-12926-resolving1"));
            assertNull(theme.getMessages("messages", new Locale("de", "AT")).get("test.keycloak-12926-resolving1"));
            assertEquals("only de_AT", theme.getMessages("messages", new Locale("de", "AT", "variant")).get("test.keycloak-12926-resolving2"));
            assertNull(theme.getMessages("messages", new Locale("de")).get("test.keycloak-12926-resolving2"));
            assertEquals("only de", theme.getMessages("messages", new Locale("de", "AT", "variant")).get("test.keycloak-12926-only_de"));
            assertNull(theme.getMessages("messages", Locale.ENGLISH).get("test.keycloak-12926-only_de"));
            assertEquals("fallback en", theme.getMessages("messages", new Locale("de", "AT", "variant")).get("test.keycloak-12926-resolving3"));
            assertEquals("fallback en", theme.getMessages("messages", new Locale("de", "AT")).get("test.keycloak-12926-resolving3"));
            assertEquals("fallback en", theme.getMessages("messages", new Locale("de")).get("test.keycloak-12926-resolving3"));
            assertNull(theme.getMessages("messages", Locale.ENGLISH).get("fallback en"));
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Locale(java.util.Locale) Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 8 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class ThemeResourceProviderTest method getTheme.

@Test
public void getTheme() {
    testingClient.server().run(session -> {
        try {
            Theme theme = session.theme().getTheme("base", Theme.Type.LOGIN);
            Assert.assertNotNull(theme.getTemplate("test.ftl"));
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 9 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class DefaultThemeManagerTest method environmentVariablesSubstitutionInThemeProperties.

// KEYCLOAK-6698
@Test
public void environmentVariablesSubstitutionInThemeProperties() {
    testingClient.server().run(session -> {
        try {
            Theme theme = session.theme().getTheme(THEME_NAME, Theme.Type.LOGIN);
            Assert.assertEquals("getTheme(...) returns default theme when no matching theme found, but we need " + THEME_NAME + " theme deployed.", THEME_NAME, theme.getName());
            Assert.assertEquals("${env.MISSING_ENVIRONMENT_VARIABLE}", theme.getProperties().getProperty("env.missing"));
            Assert.assertEquals("defaultValue", theme.getProperties().getProperty("env.missingWithDefault"));
            if (System.getenv().containsKey("HOMEPATH")) {
                // Windows
                Assert.assertEquals(System.getenv().get("HOMEPATH"), theme.getProperties().getProperty("env.windowsHome"));
            } else if (System.getenv().containsKey("HOME")) {
                // Unix
                Assert.assertEquals(System.getenv().get("HOME"), theme.getProperties().getProperty("env.unixHome"));
            } else {
                Assert.fail("No default env variable found, can't verify");
            }
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 10 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class FreeMarkerLoginFormsProvider method createResponse.

@SuppressWarnings("incomplete-switch")
protected Response createResponse(LoginFormsPages page) {
    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);
    handleMessages(locale, messagesBundle);
    // for some reason Resteasy 2.3.7 doesn't like query params and form params with the same name and will null out the code form param
    UriBuilder uriBuilder = prepareBaseUriBuilder(page == LoginFormsPages.OAUTH_GRANT);
    createCommonAttributes(theme, locale, messagesBundle, uriBuilder, page);
    attributes.put("login", new LoginBean(formData));
    if (status != null) {
        attributes.put("statusCode", status.getStatusCode());
    }
    switch(page) {
        case LOGIN_CONFIG_TOTP:
            attributes.put("totp", new TotpBean(session, realm, user, uriInfo.getRequestUriBuilder()));
            break;
        case LOGIN_UPDATE_PROFILE:
            UpdateProfileContext userCtx = (UpdateProfileContext) attributes.get(LoginFormsProvider.UPDATE_PROFILE_CONTEXT_ATTR);
            attributes.put("user", new ProfileBean(userCtx, formData));
            break;
        case LOGIN_IDP_LINK_CONFIRM:
        case LOGIN_IDP_LINK_EMAIL:
            BrokeredIdentityContext brokerContext = (BrokeredIdentityContext) this.attributes.get(IDENTITY_PROVIDER_BROKER_CONTEXT);
            String idpAlias = brokerContext.getIdpConfig().getAlias();
            idpAlias = ObjectUtil.capitalize(idpAlias);
            String displayName = idpAlias;
            if (!ObjectUtil.isBlank(brokerContext.getIdpConfig().getDisplayName())) {
                displayName = brokerContext.getIdpConfig().getDisplayName();
            }
            attributes.put("brokerContext", brokerContext);
            attributes.put("idpAlias", idpAlias);
            attributes.put("idpDisplayName", displayName);
            break;
        case LOGIN_TOTP:
            attributes.put("otpLogin", new TotpLoginBean(session, realm, user, (String) this.attributes.get(OTPFormAuthenticator.SELECTED_OTP_CREDENTIAL_ID)));
            break;
        case REGISTER:
            if (isDynamicUserProfile()) {
                page = LoginFormsPages.REGISTER_USER_PROFILE;
            }
            RegisterBean rb = new RegisterBean(formData, session);
            // legacy bean for static template
            attributes.put("register", rb);
            // bean for dynamic template
            attributes.put("profile", rb);
            break;
        case OAUTH_GRANT:
            attributes.put("oauth", new OAuthGrantBean(accessCode, client, clientScopesRequested));
            break;
        case CODE:
            attributes.put(OAuth2Constants.CODE, new CodeBean(accessCode, messageType == MessageType.ERROR ? getFirstMessageUnformatted() : null));
            break;
        case X509_CONFIRM:
            attributes.put("x509", new X509ConfirmBean(formData));
            break;
        case SAML_POST_FORM:
            attributes.put("samlPost", new SAMLPostFormBean(formData));
            break;
        case UPDATE_USER_PROFILE:
            attributes.put("profile", new VerifyProfileBean(user, formData, session));
            break;
        case IDP_REVIEW_USER_PROFILE:
            UpdateProfileContext idpCtx = (UpdateProfileContext) attributes.get(LoginFormsProvider.UPDATE_PROFILE_CONTEXT_ATTR);
            attributes.put("profile", new IdpReviewProfileBean(idpCtx, formData, session));
            break;
        case FRONTCHANNEL_LOGOUT:
            attributes.put("logout", new FrontChannelLogoutBean(session));
            break;
    }
    return processTemplate(theme, Templates.getTemplate(page), locale);
}
Also used : Locale(java.util.Locale) ProfileBean(org.keycloak.forms.login.freemarker.model.ProfileBean) IdpReviewProfileBean(org.keycloak.forms.login.freemarker.model.IdpReviewProfileBean) VerifyProfileBean(org.keycloak.forms.login.freemarker.model.VerifyProfileBean) OAuthGrantBean(org.keycloak.forms.login.freemarker.model.OAuthGrantBean) CodeBean(org.keycloak.forms.login.freemarker.model.CodeBean) RegisterBean(org.keycloak.forms.login.freemarker.model.RegisterBean) IOException(java.io.IOException) FrontChannelLogoutBean(org.keycloak.forms.login.freemarker.model.FrontChannelLogoutBean) Properties(java.util.Properties) BrokeredIdentityContext(org.keycloak.broker.provider.BrokeredIdentityContext) VerifyProfileBean(org.keycloak.forms.login.freemarker.model.VerifyProfileBean) UpdateProfileContext(org.keycloak.authentication.requiredactions.util.UpdateProfileContext) UserUpdateProfileContext(org.keycloak.authentication.requiredactions.util.UserUpdateProfileContext) TotpBean(org.keycloak.forms.login.freemarker.model.TotpBean) TotpLoginBean(org.keycloak.forms.login.freemarker.model.TotpLoginBean) X509ConfirmBean(org.keycloak.forms.login.freemarker.model.X509ConfirmBean) LoginBean(org.keycloak.forms.login.freemarker.model.LoginBean) TotpLoginBean(org.keycloak.forms.login.freemarker.model.TotpLoginBean) SAMLPostFormBean(org.keycloak.forms.login.freemarker.model.SAMLPostFormBean) Theme(org.keycloak.theme.Theme) IdpReviewProfileBean(org.keycloak.forms.login.freemarker.model.IdpReviewProfileBean) UriBuilder(javax.ws.rs.core.UriBuilder)

Aggregations

Theme (org.keycloak.theme.Theme)21 IOException (java.io.IOException)17 Locale (java.util.Locale)9 Properties (java.util.Properties)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)5 FreeMarkerUtil (org.keycloak.theme.FreeMarkerUtil)5 UriBuilder (javax.ws.rs.core.UriBuilder)4 URI (java.net.URI)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 Response (javax.ws.rs.core.Response)3 RealmModel (org.keycloak.models.RealmModel)3 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 Map (java.util.Map)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2