Search in sources :

Example 1 with AuthorizationBean

use of org.keycloak.forms.account.freemarker.model.AuthorizationBean 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 AuthorizationBean

use of org.keycloak.forms.account.freemarker.model.AuthorizationBean in project keycloak by keycloak.

the class UmaRepresentationTest method testCanRepresentResourceBeanOfResourceOwnedByUser.

public static void testCanRepresentResourceBeanOfResourceOwnedByUser(KeycloakSession session) {
    session.getContext().setRealm(session.realms().getRealmByName("authz-test"));
    AuthorizationProvider authorization = session.getProvider(AuthorizationProvider.class);
    AuthorizationBean authorizationBean = new AuthorizationBean(session, null, session.getContext().getUri());
    ClientModel client = session.getContext().getRealm().getClientByClientId("resource-server-test");
    UserModel user = session.userStorageManager().getUserByUsername(session.getContext().getRealm(), "marta");
    ResourceBean resourceBean = authorizationBean.new ResourceBean(authorization.getStoreFactory().getResourceStore().findByName("Resource A", user.getId(), client.getId()));
    Assert.assertEquals("Resource A", resourceBean.getName());
    Assert.assertEquals("marta", resourceBean.getOwnerName());
    Assert.assertNotNull(resourceBean.getUserOwner());
    Assert.assertEquals("marta", resourceBean.getUserOwner().getUsername());
    Assert.assertNull(resourceBean.getClientOwner());
}
Also used : UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) ResourceBean(org.keycloak.forms.account.freemarker.model.AuthorizationBean.ResourceBean) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) AuthorizationBean(org.keycloak.forms.account.freemarker.model.AuthorizationBean)

Example 3 with AuthorizationBean

use of org.keycloak.forms.account.freemarker.model.AuthorizationBean in project keycloak by keycloak.

the class UmaRepresentationTest method testCanRepresentResourceBeanOfResourceOwnedByClient.

public static void testCanRepresentResourceBeanOfResourceOwnedByClient(KeycloakSession session) {
    session.getContext().setRealm(session.realms().getRealmByName("authz-test"));
    AuthorizationProvider authorization = session.getProvider(AuthorizationProvider.class);
    AuthorizationBean authorizationBean = new AuthorizationBean(session, null, session.getContext().getUri());
    ClientModel client = session.getContext().getRealm().getClientByClientId("resource-server-test");
    ResourceBean resourceBean = authorizationBean.new ResourceBean(authorization.getStoreFactory().getResourceStore().findByName("Resource A", client.getId(), client.getId()));
    Assert.assertEquals("Resource A", resourceBean.getName());
    Assert.assertEquals("resource-server-test", resourceBean.getOwnerName());
    Assert.assertNotNull(resourceBean.getClientOwner());
    Assert.assertEquals("resource-server-test", resourceBean.getClientOwner().getClientId());
    Assert.assertNull(resourceBean.getUserOwner());
}
Also used : ClientModel(org.keycloak.models.ClientModel) ResourceBean(org.keycloak.forms.account.freemarker.model.AuthorizationBean.ResourceBean) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) AuthorizationBean(org.keycloak.forms.account.freemarker.model.AuthorizationBean)

Aggregations

AuthorizationBean (org.keycloak.forms.account.freemarker.model.AuthorizationBean)3 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)2 ResourceBean (org.keycloak.forms.account.freemarker.model.AuthorizationBean.ResourceBean)2 ClientModel (org.keycloak.models.ClientModel)2 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Properties (java.util.Properties)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 UriBuilder (javax.ws.rs.core.UriBuilder)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 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