Reputation: 321
I want to access to some info's inside keycloak freemarker template. There are two approaches I am thinking about. To set url parameters and read them inside freemarker template or to set this info to a cookie and access them inside the template.
I tried already to access url parameters as described here or here. But both are not working. Keycloak data model doesn't provide any getters for current Url.
Is one of those approaches possible? How can I achieve my goal
Upvotes: 6
Views: 9835
Reputation: 1894
For keycloak v11, also should be suitable for other versions:
This piece of code will allow you to get all the hash keys (eg. of url
object)
<#list url?keys as k>
${k}<br>
</#list>
next to it - for me it's totpUrl
key with the current URL value
<#if url.totpUrl?last_index_of("from=login") gte 0>
bla bla
</#if>
Hope this can help
Upvotes: 2
Reputation: 289
To get query parameters, I used Javascript, because I struggled way too much with free marker
<script type="text/javascript">
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
</script>
Hope it'll help someone
Upvotes: 1
Reputation: 873
I ran into a similar problem, where I had to access query parameters in my custom keycloak login theme. (using Keycloak version 2.1.0.Final)
There was no query param passed to my login site. But I found out that inside
org.keycloak.forms.login.freemarker.FreeMarkerLoginFormsProvider
you can access the following query parameters:
That is possible by using
UriInfo uriInfo = session.getContext().getUri();
MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
You can read the query param redirect_uri
like this
String redirectUri = queryParameters.getFirst("redirect_uri")
To pass certain data to your template you must use
attributes.put("keyToData", dataForTemplate);
before your process the template by
freeMarker.processTemplate(attributes, Templates.getTemplate(page), theme);
Then in your template (e.g. template.ftl) you can access the provided data, for instance:
<#if keyToData!false == true>
...
</#if>
Unfortunately I was not able to access custom query parameters passed to my keycloak /auth
endpoint inside FreeMarkerLoginFormsProvider
.
Upvotes: 5