Search in sources :

Example 11 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class AccountConsole method getMainPage.

@GET
@NoCache
public Response getMainPage() throws IOException, FreeMarkerException {
    UriInfo uriInfo = session.getContext().getUri(UrlType.FRONTEND);
    URI accountBaseUrl = uriInfo.getBaseUriBuilder().path(RealmsResource.class).path(realm.getName()).path(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).path("/").build(realm);
    if (!session.getContext().getUri().getRequestUri().getPath().endsWith("/")) {
        UriBuilder redirectUri = session.getContext().getUri().getRequestUriBuilder().uri(accountBaseUrl);
        return Response.status(302).location(redirectUri.build()).build();
    } else {
        Map<String, Object> map = new HashMap<>();
        URI adminBaseUri = session.getContext().getUri(UrlType.ADMIN).getBaseUri();
        URI authUrl = uriInfo.getBaseUri();
        map.put("authUrl", authUrl.getPath().endsWith("/") ? authUrl : authUrl + "/");
        map.put("baseUrl", accountBaseUrl);
        map.put("realm", realm);
        map.put("resourceUrl", Urls.themeRoot(authUrl).getPath() + "/" + Constants.ACCOUNT_MANAGEMENT_CLIENT_ID + "/" + theme.getName());
        map.put("resourceCommonUrl", Urls.themeRoot(adminBaseUri).getPath() + "/common/keycloak");
        map.put("resourceVersion", Version.RESOURCES_VERSION);
        String[] referrer = getReferrer();
        if (referrer != null) {
            map.put("referrer", referrer[0]);
            map.put("referrerName", referrer[1]);
            map.put("referrer_uri", referrer[2]);
        }
        UserModel user = null;
        if (auth != null)
            user = auth.getUser();
        Locale locale = session.getContext().resolveLocale(user);
        map.put("locale", locale.toLanguageTag());
        Properties messages = theme.getMessages(locale);
        messages.putAll(realm.getRealmLocalizationTextsByLocale(locale.toLanguageTag()));
        map.put("msg", new MessageFormatterMethod(locale, messages));
        map.put("msgJSON", messagesToJsonString(messages));
        map.put("supportedLocales", supportedLocales(messages));
        map.put("properties", theme.getProperties());
        map.put("theme", (Function<String, String>) file -> {
            try {
                final InputStream resource = theme.getResourceAsStream(file);
                return new Scanner(resource, "UTF-8").useDelimiter("\\A").next();
            } catch (IOException e) {
                throw new RuntimeException("could not load file", e);
            }
        });
        EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
        map.put("isEventsEnabled", eventStore != null && realm.isEventsEnabled());
        map.put("isAuthorizationEnabled", Profile.isFeatureEnabled(Profile.Feature.AUTHORIZATION));
        boolean isTotpConfigured = false;
        boolean deleteAccountAllowed = false;
        if (user != null) {
            isTotpConfigured = session.userCredentialManager().isConfiguredFor(realm, user, realm.getOTPPolicy().getType());
            RoleModel deleteAccountRole = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).getRole(AccountRoles.DELETE_ACCOUNT);
            deleteAccountAllowed = deleteAccountRole != null && user.hasRole(deleteAccountRole) && realm.getRequiredActionProviderByAlias(DeleteAccount.PROVIDER_ID).isEnabled();
        }
        map.put("isTotpConfigured", isTotpConfigured);
        map.put("deleteAccountAllowed", deleteAccountAllowed);
        FreeMarkerUtil freeMarkerUtil = new FreeMarkerUtil();
        String result = freeMarkerUtil.processTemplate(map, "index.ftl", theme);
        Response.ResponseBuilder builder = Response.status(Response.Status.OK).type(MediaType.TEXT_HTML_UTF_8).language(Locale.ENGLISH).entity(result);
        return builder.build();
    }
}
Also used : Locale(java.util.Locale) ClientModel(org.keycloak.models.ClientModel) Theme(org.keycloak.theme.Theme) RedirectUtils(org.keycloak.protocol.oidc.utils.RedirectUtils) RealmsResource(org.keycloak.services.resources.RealmsResource) Profile(org.keycloak.common.Profile) GET(javax.ws.rs.GET) Logger(org.jboss.logging.Logger) EventStoreProvider(org.keycloak.events.EventStoreProvider) Constants(org.keycloak.models.Constants) Path(javax.ws.rs.Path) Scanner(java.util.Scanner) HashMap(java.util.HashMap) Version(org.keycloak.common.Version) Function(java.util.function.Function) ResolveRelative(org.keycloak.services.util.ResolveRelative) UserModel(org.keycloak.models.UserModel) Matcher(java.util.regex.Matcher) AuthenticationManager(org.keycloak.services.managers.AuthenticationManager) Validation(org.keycloak.services.validation.Validation) Locale(java.util.Locale) AppAuthManager(org.keycloak.services.managers.AppAuthManager) Map(java.util.Map) Json(javax.json.Json) Auth(org.keycloak.services.managers.Auth) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) UrlType(org.keycloak.urls.UrlType) RealmModel(org.keycloak.models.RealmModel) Context(javax.ws.rs.core.Context) Properties(java.util.Properties) KeycloakSession(org.keycloak.models.KeycloakSession) RoleModel(org.keycloak.models.RoleModel) IOException(java.io.IOException) DeleteAccount(org.keycloak.authentication.requiredactions.DeleteAccount) FreeMarkerUtil(org.keycloak.theme.FreeMarkerUtil) Collectors(java.util.stream.Collectors) MessageFormatterMethod(org.keycloak.theme.beans.MessageFormatterMethod) AccountRoles(org.keycloak.models.AccountRoles) MediaType(org.keycloak.utils.MediaType) NoCache(org.jboss.resteasy.annotations.cache.NoCache) Response(javax.ws.rs.core.Response) Urls(org.keycloak.services.Urls) Pattern(java.util.regex.Pattern) UriInfo(javax.ws.rs.core.UriInfo) FreeMarkerException(org.keycloak.theme.FreeMarkerException) JsonObjectBuilder(javax.json.JsonObjectBuilder) InputStream(java.io.InputStream) Scanner(java.util.Scanner) HashMap(java.util.HashMap) InputStream(java.io.InputStream) RoleModel(org.keycloak.models.RoleModel) IOException(java.io.IOException) Properties(java.util.Properties) URI(java.net.URI) UserModel(org.keycloak.models.UserModel) Response(javax.ws.rs.core.Response) FreeMarkerUtil(org.keycloak.theme.FreeMarkerUtil) UriBuilder(javax.ws.rs.core.UriBuilder) MessageFormatterMethod(org.keycloak.theme.beans.MessageFormatterMethod) UriInfo(javax.ws.rs.core.UriInfo) EventStoreProvider(org.keycloak.events.EventStoreProvider) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 12 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class TestingResourceProvider method clearEventStore.

@GET
@Path("/clear-event-store")
@Produces(MediaType.APPLICATION_JSON)
public Response clearEventStore() {
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    eventStore.clear();
    return Response.noContent().build();
}
Also used : EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 13 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class AdminEventQueryTest method testClear.

@Test
public void testClear() {
    inRolledBackTransaction(null, (session, t) -> {
        EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
        eventStore.clear();
    });
}
Also used : EventStoreProvider(org.keycloak.events.EventStoreProvider) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) Test(org.junit.Test)

Example 14 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class TestingResourceProvider method onAdminEvent.

@POST
@Path("/on-admin-event")
@Consumes(MediaType.APPLICATION_JSON)
public void onAdminEvent(final AdminEventRepresentation rep, @QueryParam("includeRepresentation") boolean includeRepresentation) {
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    eventStore.onEvent(repToModel(rep), includeRepresentation);
}
Also used : EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 15 with EventStoreProvider

use of org.keycloak.events.EventStoreProvider in project keycloak by keycloak.

the class TestingResourceProvider method onEvent.

@PUT
@Path("/on-event")
@Consumes(MediaType.APPLICATION_JSON)
public void onEvent(final EventRepresentation rep) {
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    eventStore.onEvent(repToModel(rep));
}
Also used : EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

EventStoreProvider (org.keycloak.events.EventStoreProvider)17 Path (javax.ws.rs.Path)14 GET (javax.ws.rs.GET)10 Produces (javax.ws.rs.Produces)9 NoCache (org.jboss.resteasy.annotations.cache.NoCache)4 Date (java.util.Date)3 AdminEventQuery (org.keycloak.events.admin.AdminEventQuery)3 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2 Test (org.junit.Test)2 OperationType (org.keycloak.events.admin.OperationType)2 ModelToRepresentation (org.keycloak.models.utils.ModelToRepresentation)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1