Skip to content

Commit

Permalink
updated translations with client names. reformatted.
Browse files Browse the repository at this point in the history
  • Loading branch information
xgp committed Nov 29, 2023
1 parent 71fb3ca commit 23678fb
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 103 deletions.
32 changes: 16 additions & 16 deletions ext/main/java/io/phasetwo/portal/PortalEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package io.phasetwo.portal;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import jakarta.annotation.Generated;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"displayName",
"logoUrl",
"faviconUrl",
"appiconUrl",
"realm",
"locale",
"authServerUrl",
"baseUrl",
"resourceUrl",
"refererUrl",
"isRunningAsTheme",
"supportedLocales",
"features",
"styles"
"name",
"displayName",
"logoUrl",
"faviconUrl",
"appiconUrl",
"realm",
"locale",
"authServerUrl",
"baseUrl",
"resourceUrl",
"refererUrl",
"isRunningAsTheme",
"supportedLocales",
"features",
"styles"
})
@Generated("jsonschema2pojo")
public class PortalEnvironment {
Expand Down
194 changes: 107 additions & 87 deletions ext/main/java/io/phasetwo/portal/PortalResourceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ public PortalResourceProvider(KeycloakSession session, String authRealmOverride)
}

public void init() {
AuthenticationManager.AuthResult authResult = authManager.authenticateIdentityCookie(session,
session.getContext().getRealm());
AuthenticationManager.AuthResult authResult =
authManager.authenticateIdentityCookie(session, session.getContext().getRealm());
if (authResult != null) {
auth = new Auth(
session.getContext().getRealm(),
authResult.getToken(),
authResult.getUser(),
session.getContext().getClient(),
authResult.getSession(),
true);
auth =
new Auth(
session.getContext().getRealm(),
authResult.getToken(),
authResult.getUser(),
session.getContext().getClient(),
authResult.getSession(),
true);
}
}

Expand All @@ -88,7 +89,7 @@ private String getRealmName(RealmModel realm) {
}

public static final String[] METHODS = {
"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"
"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"
};

@OPTIONS
Expand All @@ -114,14 +115,16 @@ public Response portal() {
Theme theme = getTheme("portal");
UriInfo uriInfo = session.getContext().getUri();
URI authUrl = uriInfo.getBaseUri();
URI portalBaseUrl = uriInfo
.getBaseUriBuilder()
.path(RealmsResource.class)
.path(realm.getName())
.path("portal")
.path("/")
.build(realm);
String portalResources = Urls.themeRoot(authUrl).getPath() + "/" + "admin-portal" + "/" + theme.getName();
URI portalBaseUrl =
uriInfo
.getBaseUriBuilder()
.path(RealmsResource.class)
.path(realm.getName())
.path("portal")
.path("/")
.build(realm);
String portalResources =
Urls.themeRoot(authUrl).getPath() + "/" + "admin-portal" + "/" + theme.getName();

Locale locale = new Locale("en");
try {
Expand All @@ -135,23 +138,25 @@ public Response portal() {
}

try {
String referer = session
.getContext()
.getHttpRequest()
.getHttpHeaders()
.getRequestHeaders()
.getFirst("Referer");
PortalEnvironment env = new PortalEnvironment()
.realm(realm.getName())
.locale(locale.toLanguageTag())
.authServerUrl(
authUrl.getPath().endsWith("/") ? authUrl.toString() : authUrl.toString() + "/")
.baseUrl(portalBaseUrl.toString())
.resourceUrl(portalResources)
.refererUrl(referer)
.isRunningAsTheme(true)
.supportedLocales(getSupportedLocales(realm, locale))
.styles(new PortalEnvironment.CustomTheme());
String referer =
session
.getContext()
.getHttpRequest()
.getHttpHeaders()
.getRequestHeaders()
.getFirst("Referer");
PortalEnvironment env =
new PortalEnvironment()
.realm(realm.getName())
.locale(locale.toLanguageTag())
.authServerUrl(
authUrl.getPath().endsWith("/") ? authUrl.toString() : authUrl.toString() + "/")
.baseUrl(portalBaseUrl.toString())
.resourceUrl(portalResources)
.refererUrl(referer)
.isRunningAsTheme(true)
.supportedLocales(getSupportedLocales(realm, locale))
.styles(new PortalEnvironment.CustomTheme());
Optional.ofNullable(realm.getName()).ifPresent(a -> env.name(a));
Optional.ofNullable(realm.getDisplayName()).ifPresent(a -> env.displayName(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.logo.url")))
Expand All @@ -160,58 +165,73 @@ public Response portal() {
.ifPresent(a -> env.faviconUrl(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.appicon.url")))
.ifPresent(a -> env.appiconUrl(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor100")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor100")))
.ifPresent(a -> env.getStyles().setPrimary100(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor200")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor200")))
.ifPresent(a -> env.getStyles().setPrimary200(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor400")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor400")))
.ifPresent(a -> env.getStyles().setPrimary400(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor500")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor500")))
.ifPresent(a -> env.getStyles().setPrimary500(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor600")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor600")))
.ifPresent(a -> env.getStyles().setPrimary600(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor700")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor700")))
.ifPresent(a -> env.getStyles().setPrimary700(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor900")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.primaryColor900")))
.ifPresent(a -> env.getStyles().setPrimary900(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.secondaryColor800")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.secondaryColor800")))
.ifPresent(a -> env.getStyles().setSecondary800(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.secondaryColor900")))
Optional.ofNullable(
realm.getAttribute(String.format("_providerConfig.assets.portal.secondaryColor900")))
.ifPresent(a -> env.getStyles().setSecondary900(a));
Optional.ofNullable(realm.getAttribute(String.format("_providerConfig.assets.portal.css")))
.ifPresent(a -> env.getStyles().setCustomCSS(a));
env.setFeatures(PortalFeatures.fromSession(session, auth));

String envStr = new String(JsonStringEncoder.getInstance().quoteAsString(mapper.writeValueAsString(env)));
String envStr =
new String(JsonStringEncoder.getInstance().quoteAsString(mapper.writeValueAsString(env)));
log.infof("set environment string to %s", envStr);
String authUrlAttribute = authUrl.getPath().endsWith("/")
? authUrl.toString().substring(0, authUrl.toString().length() - 1)
: authUrl.toString();
String authUrlAttribute =
authUrl.getPath().endsWith("/")
? authUrl.toString().substring(0, authUrl.toString().length() - 1)
: authUrl.toString();
String realmName = getRealmName(realm);
LoginFormsProvider form = session
.getProvider(LoginFormsProvider.class)
.setAttribute("environment", envStr)
.setAttribute("authUrl", authUrlAttribute)
.setAttribute(
"faviconUrl",
Optional.ofNullable(realm.getAttribute("_providerConfig.assets.favicon.url"))
.orElse(
String.format(
"%s/realms/%s/portal/favicon.ico", authUrlAttribute, realmName)))
.setAttribute(
"appiconUrl",
Optional.ofNullable(realm.getAttribute("_providerConfig.assets.appicon.url"))
.orElse(
String.format(
"%s/realms/%s/portal/logo192.png", authUrlAttribute, realmName)))
.setAttribute("displayName", realmName)
.setAttribute("realmName", realm.getName());
LoginFormsProvider form =
session
.getProvider(LoginFormsProvider.class)
.setAttribute("environment", envStr)
.setAttribute("authUrl", authUrlAttribute)
.setAttribute(
"faviconUrl",
Optional.ofNullable(realm.getAttribute("_providerConfig.assets.favicon.url"))
.orElse(
String.format(
"%s/realms/%s/portal/favicon.ico", authUrlAttribute, realmName)))
.setAttribute(
"appiconUrl",
Optional.ofNullable(realm.getAttribute("_providerConfig.assets.appicon.url"))
.orElse(
String.format(
"%s/realms/%s/portal/logo192.png", authUrlAttribute, realmName)))
.setAttribute("displayName", realmName)
.setAttribute("realmName", realm.getName());
FreeMarkerLoginFormsProvider fm = (FreeMarkerLoginFormsProvider) form;
Method processTemplateMethod = fm.getClass()
.getDeclaredMethod("processTemplate", Theme.class, String.class, Locale.class);
Method processTemplateMethod =
fm.getClass()
.getDeclaredMethod("processTemplate", Theme.class, String.class, Locale.class);
processTemplateMethod.setAccessible(true);
Response resp = (Response) processTemplateMethod.invoke(
fm, theme, "portal.ftl", session.getContext().resolveLocale(null));
Response resp =
(Response)
processTemplateMethod.invoke(
fm, theme, "portal.ftl", session.getContext().resolveLocale(null));
return resp;
} catch (Exception e) {
log.warn("Could not call processTemplate on FreeMarkerLoginFormsProvider", e);
Expand Down Expand Up @@ -253,20 +273,20 @@ public Response keycloakJson() {
setupCors();
RealmModel realm = session.getContext().getRealm();
UriInfo uriInfo = session.getContext().getUri();
Map json = ImmutableMap.of(
"realm",
authRealmOverride,
"auth-server-url",
getBaseUrl(uriInfo),
"resource",
"admin-portal");
Map json =
ImmutableMap.of(
"realm",
authRealmOverride,
"auth-server-url",
getBaseUrl(uriInfo),
"resource",
"admin-portal");
return Response.ok(json).build();
}

private static String getBaseUrl(UriInfo uriInfo) {
String u = uriInfo.getBaseUri().toString();
if (u != null && u.endsWith("/"))
u = u.substring(0, u.length() - 1);
if (u != null && u.endsWith("/")) u = u.substring(0, u.length() - 1);
return u;
}

Expand All @@ -290,12 +310,13 @@ private String getLastPathSegment(UriInfo uriInfo) {
return null;
}

private static final String MIME_TYPES = "text/javascript js\n"
+ "text/css css\n"
+ "font/woff woff\n"
+ "font/woff2 woff2\n"
+ "application/json json webmanifest map\n"
+ "image/svg+xml svg\n";
private static final String MIME_TYPES =
"text/javascript js\n"
+ "text/css css\n"
+ "font/woff woff\n"
+ "font/woff2 woff2\n"
+ "application/json json webmanifest map\n"
+ "image/svg+xml svg\n";

private String getMimeType(String fileName) {
MimetypesFileTypeMap map = new MimetypesFileTypeMap();
Expand All @@ -304,6 +325,5 @@ private String getMimeType(String fileName) {
}

@Override
public void close() {
}
public void close() {}
}
7 changes: 7 additions & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
"cancel": "Cancel",
"choose": "Choose...",
"client": "Client",
"clientNotFoundMessage": "*Client not found.",
"client_account": "*Account",
"client_account-console": "*Account Console",
"client_admin-cli": "*Admin CLI",
"client_broker": "*Broker",
"client_realm-management": "*Realm Management",
"client_security-admin-console": "*security admin console",
"clients": "Clients",
"close": "Close",
"configureWaysToSignIn": "Configure ways to sign in",
Expand Down

0 comments on commit 23678fb

Please sign in to comment.