Keycloak access cookie and/or url query params inside freemarker template

Viewed 5121

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

3 Answers

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

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

Related