<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Identity-and-Access-Management on Janik von Rotz</title>
    <link>https://janikvonrotz.ch/tags/identity-and-access-management/</link>
    <description>Recent content in Identity-and-Access-Management on Janik von Rotz</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Wed, 12 Aug 2026 17:32:22 +0000</lastBuildDate>
    <atom:link href="https://janikvonrotz.ch/tags/identity-and-access-management/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Remark42 Authenticate with Codeberg</title>
      <link>https://janikvonrotz.ch/2026/08/12/remark42-authenticate-with-codeberg/</link>
      <pubDate>Wed, 12 Aug 2026 17:32:22 +0000</pubDate>
      <guid>https://janikvonrotz.ch/2026/08/12/remark42-authenticate-with-codeberg/</guid>
      <description>&lt;p&gt;At then end of each post you can leave a comment. This comment section is powered by &lt;a href=&#34;https://remark42.com/&#34;&gt;Remark42&lt;/a&gt;. A highly recommended comment engine. But be aware, there is no SaaS and you have to host it yourself (maybe that is way it is so good). For quite some time the only option to authenticate on my site was using &lt;em&gt;GitHub&lt;/em&gt;. As a proud member for Codeberg this had to change.&lt;/p&gt;&#xA;&lt;p&gt;Before we start I wanna thank my brother. I already &lt;a href=&#34;https://joelvonrotz.ch/writing/codeberg-as-a-remark42-authenticator/&#34;&gt;made the integration&lt;/a&gt; and so it was easier for me to do this as well.&lt;/p&gt;&#xA;&lt;h2 id=&#34;setup-codeberg-oauth2&#34;&gt;Setup Codeberg OAuth2&lt;/h2&gt;&#xA;&lt;p&gt;First we create the OAuth2 application in Coderberg. Open &lt;a href=&#34;https://codeberg.org/user/settings/applications&#34;&gt;https://codeberg.org/user/settings/applications&lt;/a&gt; and log in. Underneath &lt;em&gt;Manage OAuth2 applications&lt;/em&gt; enter &lt;code&gt;Remark42&lt;/code&gt; as name, &lt;code&gt;&amp;lt;url-of-your-remark42-instance&amp;gt;/auth/codeberg/callback&lt;/code&gt; as &lt;em&gt;Redirect URI&lt;/em&gt; and click &lt;em&gt;Create application&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;In the new dialog copy the &lt;em&gt;Client ID&lt;/em&gt; and the &lt;em&gt;Client Secret&lt;/em&gt;. We will need this in the second step.&lt;/p&gt;&#xA;&lt;h2 id=&#34;deploy-remark42&#34;&gt;Deploy Remark42&lt;/h2&gt;&#xA;&lt;p&gt;The PR that supports authentication with OAuth provider was merged only recently: &lt;a href=&#34;https://github.com/umputun/remark42/pull/2006&#34;&gt;https://github.com/umputun/remark42/pull/2006&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;The documentation shows the new environment variables that need to be configured: &lt;a href=&#34;https://remark42.com/docs/configuration/authorization/#custom-oauth2-provider&#34;&gt;https://remark42.com/docs/configuration/authorization/#custom-oauth2-provider&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;I am deploying Remark42 using this &lt;a href=&#34;https://ansible.build/roles/remark42/&#34;&gt;https://ansible.build/roles/remark42/&lt;/a&gt; Ansible role. Adding support for new env vars is fairly simple for Ansible roles. You only need to map the env vars to Ansible vars.&lt;/p&gt;&#xA;&lt;p&gt;I updated the role and in my Ansible inventory I made this configuration:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_image&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;umputun/remark42:v1.16.4&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_auth_custom_name&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;codeberg&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_auth_custom_cid&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;lt;client id goes here&amp;gt;&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_auth_custom_auth_url&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;https://codeberg.org/login/oauth/authorize&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_auth_custom_token_url&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;https://codeberg.org/login/oauth/access_token&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_auth_custom_info_url&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;https://codeberg.org/login/oauth/userinfo&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_auth_custom_scopes&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;read:user&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;em&gt;Client Secret&lt;/em&gt; was encrypted with Ansible vault and stored as &lt;code&gt;vault_remark42_auth_custom_csec&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;After deploying the container with Ansible I checked the log:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-txt&#34; data-lang=&#34;txt&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;2026/08/12 12:31:24.692 [INFO]  init oauth2 service github&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;2026/08/12 12:31:24.693 [INFO]  init oauth2 service codeberg&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Everthing looked fine.&lt;/p&gt;&#xA;&lt;h2 id=&#34;testing&#34;&gt;Testing&lt;/h2&gt;&#xA;&lt;p&gt;After the deployment I opened my site in private tab and clicked &lt;em&gt;Sign In&lt;/em&gt; on a random post:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://janikvonrotz.ch/images/remark42-sign-in.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;Then I clicked on &lt;em&gt;Codeberg&lt;/em&gt; and was redirected. After login I got this error:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Authorization failed&lt;br&gt;&#xA;Unregistered Redirect URI&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I double checked the redirect uri, but it was definitely correct. I took me a while to figure this out. Basically I messed up the client id and secret and then I forgot to remove the placeholder in the host inventory:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;remark42_auth_custom_csec: # default&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{{ vault_remark42_auth_custom_csec }}&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once fixed this I got greeted by this message:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Authorize &amp;ldquo;Remark42&amp;rdquo; to access your account?&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;Clicked &lt;em&gt;Accept&lt;/em&gt; and was redirected and welcomed by a new error message:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{&lt;span style=&#34;color:#f92672&#34;&gt;&amp;#34;error&amp;#34;&lt;/span&gt;:&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;exchange failed&amp;#34;&lt;/span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In Ansible I quoted the &lt;code&gt;AUTH_CUSTOM_CSEC&lt;/code&gt; env var. This was a mistake. After removing the quotes it worked! Yay!&lt;/p&gt;&#xA;&lt;h2 id=&#34;cleanup-step&#34;&gt;Cleanup step&lt;/h2&gt;&#xA;&lt;p&gt;On last thing that bothered me was that my Codeberg user did not have admin rights. To grant admin access you set the &lt;code&gt;ADMIN_SHARED_EMAIL&lt;/code&gt; and &lt;code&gt;ADMIN_SHARED_ID&lt;/code&gt;. The email stayed the same, but the id changes. To retrieve the id you click on the username in the login section. Then I added this is to the env var. After refresh I saw the admin settings in the comment section.&lt;/p&gt;&#xA;&lt;p&gt;Read more at &lt;a href=&#34;https://remark42.com/docs/configuration/parameters/#admin-users&#34;&gt;https://remark42.com/docs/configuration/parameters/#admin-users&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title> Nextcloud Social Login with Gitlab</title>
      <link>https://janikvonrotz.ch/2021/05/17/nextcloud-social-login-with-gitlab/</link>
      <pubDate>Mon, 17 May 2021 13:18:01 +0200</pubDate>
      <guid>https://janikvonrotz.ch/2021/05/17/nextcloud-social-login-with-gitlab/</guid>
      <description>&lt;p&gt;This example shows one way to configure &lt;a href=&#34;https://docs.gitlab.com/ee/integration/openid_connect_provider.html&#34;&gt;GitLab as an OpenID Connect (OIDC) identity provider&lt;/a&gt;, so that only members a specific GitLab group are allowed to login.&lt;/p&gt;&#xA;&lt;p&gt;In the example, the GitLab group is named &lt;code&gt;hfmts&lt;/code&gt; and the Nextcloud server is &lt;code&gt;https://nextcloud.example.com&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;setup-gitlab&#34;&gt;Setup GitLab&lt;/h2&gt;&#xA;&lt;p&gt;To make the login work with GitLab we need a GitLab application and a group. The GitLab application represents our Nextcloud application.&lt;/p&gt;&#xA;&lt;p&gt;Open &lt;a href=&#34;https://gitlab.com/oauth/applications&#34;&gt;https://gitlab.com/oauth/applications&lt;/a&gt; and create a new application with these values:&lt;/p&gt;&#xA;&lt;p&gt;Name: &lt;code&gt;Name of Nextcloud&lt;/code&gt;&lt;br&gt;&#xA;Redirect URI: &lt;code&gt;https://cloud.example.com/apps/sociallogin/custom_oidc/gitlab&lt;/code&gt;&lt;br&gt;&#xA;Condential: [ ]&lt;br&gt;&#xA;Scopes: &lt;code&gt;openid&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;Save the &lt;em&gt;Application ID&lt;/em&gt; and the &lt;em&gt;Secret&lt;/em&gt; to a notepad. We need it later.&lt;/p&gt;&#xA;&lt;p&gt;Next create the GitLab group at &lt;a href=&#34;https://gitlab.com/groups/new&#34;&gt;https://gitlab.com/groups/new&lt;/a&gt;. Enter a valid name and also not the generated url path. This will be the group name that is referenced in the Nextcloud integration.&lt;/p&gt;&#xA;&lt;h2 id=&#34;setup-social-login&#34;&gt;Setup Social Login&lt;/h2&gt;&#xA;&lt;p&gt;As we have our GitLab application and group, we are ready to setup the login integration in Nextcloud.&lt;/p&gt;&#xA;&lt;p&gt;Open social login settings at &lt;a href=&#34;https://nextcloud.example.com/settings/admin/sociallogin&#34;&gt;https://nextcloud.example.com/settings/admin/sociallogin&lt;/a&gt;. Ensure the options are checked as follow:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;input disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Disable auto create new users&lt;/li&gt;&#xA;&lt;li&gt;&lt;input disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Create users with disabled account&lt;/li&gt;&#xA;&lt;li&gt;&lt;input disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Allow users to connect social logins with their account&lt;/li&gt;&#xA;&lt;li&gt;&lt;input disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Prevent creating an account if the email address exists in another account&lt;/li&gt;&#xA;&lt;li&gt;&lt;input checked=&#34;&#34; disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Update user profile every login&lt;/li&gt;&#xA;&lt;li&gt;&lt;input disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Do not prune not available user groups on login&lt;/li&gt;&#xA;&lt;li&gt;&lt;input disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Automatically create groups if they do not exists&lt;/li&gt;&#xA;&lt;li&gt;&lt;input checked=&#34;&#34; disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Restrict login for users without mapped groups&lt;/li&gt;&#xA;&lt;li&gt;&lt;input disabled=&#34;&#34; type=&#34;checkbox&#34;&gt; Disable notify admins about new users&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;This will ensure that user data is always up-to-date and only members of the GitLab group are allowed to login.&lt;/p&gt;&#xA;&lt;p&gt;Next create a new &lt;em&gt;Custom OpenID Connect&lt;/em&gt; entry by clicking on the &lt;code&gt;+&lt;/code&gt; next to it. Set these values&lt;/p&gt;&#xA;&lt;p&gt;Internal name: &lt;code&gt;gitlab&lt;/code&gt;&lt;br&gt;&#xA;Title: &lt;code&gt;GitLab&lt;/code&gt;&lt;br&gt;&#xA;Authorize url: &lt;code&gt;https://gitlab.com/oauth/authorize&lt;/code&gt;&lt;br&gt;&#xA;Token url: &lt;code&gt;https://gitlab.com/oauth/token&lt;/code&gt;&lt;br&gt;&#xA;User info URL: &lt;code&gt;https://gitlab.com/oauth/userinfo&lt;/code&gt;&lt;br&gt;&#xA;Client Id: &lt;code&gt;Copy from notepad&lt;/code&gt;&lt;br&gt;&#xA;Client Secret:  &lt;code&gt;Copy from notepad&lt;/code&gt;&lt;br&gt;&#xA;Scope: &lt;code&gt;openid&lt;/code&gt;&lt;br&gt;&#xA;Groups claim: &lt;code&gt;groups&lt;/code&gt;&lt;br&gt;&#xA;Button style: &lt;code&gt;Gitlab&lt;/code&gt;&lt;br&gt;&#xA;Default group: &lt;code&gt;None&lt;/code&gt;&lt;br&gt;&#xA;Group mapping: &lt;code&gt;hfmts&lt;/code&gt; &amp;lt;&amp;ndash;&amp;gt; &lt;code&gt;hfmts&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;On the left of the &lt;em&gt;Group mapping&lt;/em&gt; option is the displayname of the GitLab group and on the right is the selected Nextcloud group. Create a separate Nextcloud group if not already done.&lt;/p&gt;&#xA;&lt;p&gt;Click &lt;em&gt;Save&lt;/em&gt; on the bottom of the settings page.&lt;/p&gt;&#xA;&lt;h2 id=&#34;test-the-login&#34;&gt;Test the login&lt;/h2&gt;&#xA;&lt;p&gt;To test the login integration proceed as followed:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Log out of Nextcloud&lt;/li&gt;&#xA;&lt;li&gt;Click &lt;em&gt;Log in with GitLab&lt;/em&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Your are redirectred to GitLab.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Sign into GitLab&lt;/li&gt;&#xA;&lt;li&gt;Authorize the application&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;You are redirected back to Nextcloud and are logged in.&lt;/p&gt;&#xA;&lt;p&gt;Any group shares can be accessed immediately. This is a great way to work with external users which do not want have another account.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Upgrade Keycloak Docker container</title>
      <link>https://janikvonrotz.ch/2021/03/08/08-upgrade-keycloak-docker-container/</link>
      <pubDate>Mon, 08 Mar 2021 10:46:43 +0100</pubDate>
      <guid>https://janikvonrotz.ch/2021/03/08/08-upgrade-keycloak-docker-container/</guid>
      <description>&lt;p&gt;Upgrading Keycloak is not so difficult as supports automatically migrating the database to a new version.&lt;/p&gt;&#xA;&lt;p&gt;Nonetheless, Keycloak is a critical piece of software in every infrastructure. Under no circumstances you want the upgrade to fail. I will show you some upgrade preparations for a docker-based setup that ensure you can restore the service in the worst case scenario.&lt;/p&gt;&#xA;&lt;p&gt;Before upgrading the docker image, ensure you have:&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Created a database backup&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Use &lt;code&gt;pg_dump&lt;/code&gt; to dump the Keycloak database.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Exported realms including groups, roles and clients&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Open the realm in the Keycloak backend navigate to &lt;em&gt;Mange &amp;gt; Export&lt;/em&gt;. Tick on all options and export the realm. Repeat this process for all realms.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Stopped and renamed the old container&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Stop the existing container and rename it.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;docker stop $KEYCLOAKD_CONTAINER &amp;amp;&amp;amp; docker rename $KEYCLOAKD_CONTAINER $NEW_NAME&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;In the worst case scenario we can restore the database and restore to old container and state.&lt;/p&gt;&#xA;&lt;h2 id=&#34;upgrade&#34;&gt;Upgrade&lt;/h2&gt;&#xA;&lt;p&gt;Executing the upgrade is simple. In my case I simply had to change the image tag to a newer version.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;jboss/keycloak:9.0.2 -&amp;gt; jboss/keycloak:12.0.4&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;Avoid using the &lt;code&gt;latest&lt;/code&gt; tag!&lt;/p&gt;&#xA;&lt;h2 id=&#34;troubleshooting&#34;&gt;Troubleshooting&lt;/h2&gt;&#xA;&lt;p&gt;After upgrade the connected OAuth clients must be tested. While doing so errors might occur due to new restrictions from the Keycloak side.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Invalid scope parameter&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;After the upgrade I could not longer login with one of the OAuth providers. Whenever I tried to initiate the login flow it was immediately aborted. The Keycloak log threw this error: &lt;code&gt;KC-SERVICES0093: Invalid parameter value for: scope&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;When I checked the login url on the client, I saw that the OAuth provider set &lt;code&gt;scope=False&lt;/code&gt;. Explicitly setting the scope param of the OAuth provider resolved the issue.&lt;/p&gt;</description>
    </item>
    <item>
      <title>OpenID Connect with Nextcloud and Keycloak</title>
      <link>https://janikvonrotz.ch/2020/10/20/openid-connect-with-nextcloud-and-keycloak/</link>
      <pubDate>Tue, 20 Oct 2020 08:43:32 +0200</pubDate>
      <guid>https://janikvonrotz.ch/2020/10/20/openid-connect-with-nextcloud-and-keycloak/</guid>
      <description>&lt;p&gt;Keycloak and Nextcloud are both popular open source software. The first is an identity provider and broker, the second one is collaboration platform. Instead of having multiple accounts on several online platforms, you want to have one identity and log into multiple platforms. This is the job of the &lt;a href=&#34;https://openid.net/connect/&#34;&gt;OpenID Connect protocol&lt;/a&gt; and is supported by Keycloak. In this guide I walk you through the steps of connecting Nextcloud with Keycloak to support a seamless single sign-on experience.&lt;/p&gt;&#xA;&lt;p&gt;In our scenario we have these hosts and configurations:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Keycloak server (OpenID Provider)&#xA;&lt;ul&gt;&#xA;&lt;li&gt;host: &lt;code&gt;login.example.com&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;realm: &lt;code&gt;example.com&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;Nextcloud server (Relying Party)&#xA;&lt;ul&gt;&#xA;&lt;li&gt;host: &lt;code&gt;nextcloud.example.com&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;I assume both services are configured and running without issues.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-authentication-flow&#34;&gt;The authentication flow&lt;/h2&gt;&#xA;&lt;p&gt;What we want to achieve is quite common.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://janikvonrotz.ch/images/OpenID%20Connect%20flow.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;The authentication flow goes like this:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;Nextcloud redirects the user to Keylcoak&lt;/li&gt;&#xA;&lt;li&gt;The user authenticates with the Keycloak credentials&lt;/li&gt;&#xA;&lt;li&gt;User is redirected back to Nextcloud with a token&lt;/li&gt;&#xA;&lt;li&gt;Nextcloud retrieves an access token&lt;/li&gt;&#xA;&lt;li&gt;Nextcloud uses the access token to retrieve user information&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;h2 id=&#34;setup-client-in-keycloak&#34;&gt;Setup client in Keycloak&lt;/h2&gt;&#xA;&lt;p&gt;Lets get started by setting up the Keycloak client.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Open the Keycloak Admin Console&lt;/li&gt;&#xA;&lt;li&gt;Select your realm and create a new client &lt;em&gt;Configure &amp;gt; Clients &amp;gt; Create&lt;/em&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Client ID: &lt;code&gt;nextcloud.example.com&lt;/code&gt;&lt;br&gt;&#xA;Client Protocol: &lt;code&gt;openid-connect&lt;/code&gt;&lt;br&gt;&#xA;Root URL : &lt;code&gt;https://nextcloud.example.com&lt;/code&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Click &lt;em&gt;Save&lt;/em&gt; and update these settings for the new client:&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Access Type: &lt;code&gt;confidential&lt;/code&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Click &lt;em&gt;Save&lt;/em&gt; and open the &lt;em&gt;Roles&lt;/em&gt; tab and add a new role:&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Role Name: &lt;code&gt;admin&lt;/code&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Return to the clients view and open the &lt;em&gt;Mappers&lt;/em&gt; tab&lt;/li&gt;&#xA;&lt;li&gt;Click &lt;em&gt;Add Builtin&lt;/em&gt;, search for &lt;code&gt;client roles&lt;/code&gt; and add it&lt;/li&gt;&#xA;&lt;li&gt;Open the new mapper and make these configurations:&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Token Claim Name: &lt;code&gt;roles&lt;/code&gt;&lt;br&gt;&#xA;Add to userinfo: &lt;code&gt;ON&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;This mapper will ensure that the JWT-Token contains the user roles at the right location.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Click &lt;em&gt;Save&lt;/em&gt; and open the &lt;em&gt;Credentials&lt;/em&gt; tab&lt;/li&gt;&#xA;&lt;li&gt;Copy the &lt;em&gt;Secret&lt;/em&gt; to an ephermal note&lt;/li&gt;&#xA;&lt;li&gt;Open the &lt;em&gt;Scope&lt;/em&gt; tab and disable &lt;em&gt;Full Scope Allowed&lt;/em&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;This will ensure that only the client roles are stored in the token.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Navigate to &lt;em&gt;Configure &amp;gt; Realm Settings &amp;gt; General tab&lt;/em&gt; and click on &lt;em&gt;OpenID Endpoint Configuration&lt;/em&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Keep this browser tab open. We need it for the Nextcloud configuration.&lt;/p&gt;&#xA;&lt;h2 id=&#34;configure-social-login-in-nextcloud&#34;&gt;Configure social login in Nextcloud&lt;/h2&gt;&#xA;&lt;p&gt;Now it is time to set up the Nextcloud social login.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Open the &lt;em&gt;App&lt;/em&gt; dashboard&lt;/li&gt;&#xA;&lt;li&gt;Install the &lt;em&gt;Social Login&lt;/em&gt; app&lt;/li&gt;&#xA;&lt;li&gt;Navigate to &lt;em&gt;Settings &amp;gt; Administration &amp;gt; Social login&lt;/em&gt;&lt;/li&gt;&#xA;&lt;li&gt;Set the options according your requirements or check at least these options:&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;[x] Prevent creating an account if the email address exists in another account&lt;br&gt;&#xA;[x] Restrict login for users without mapped groups&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Save the settings&lt;/li&gt;&#xA;&lt;li&gt;Click on the &lt;em&gt;Custom OpenID Connect&lt;/em&gt; plus button&lt;/li&gt;&#xA;&lt;li&gt;Enter the information from the open Keycloak browser tab:&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Internal name: &lt;code&gt;keycloak&lt;/code&gt;&lt;br&gt;&#xA;Title: &lt;code&gt;Example&lt;/code&gt;&lt;br&gt;&#xA;Authorize url: &#x9;&lt;code&gt;https://login.example.com/auth/realms/example.com/protocol/openid-connect/auth&lt;/code&gt;&lt;br&gt;&#xA;Token url: &lt;code&gt;https://login.example.com/auth/realms/example.com/protocol/openid-connect/token&lt;/code&gt;&lt;br&gt;&#xA;User info URL (optional): &lt;code&gt;https://login.example.com/auth/realms/example.com/protocol/openid-connect/userinfo&lt;/code&gt;&lt;br&gt;&#xA;Logout URL (optional): &lt;code&gt;https://login.example.com/auth/realms/example.com/protocol/openid-connect/logout?redirect_uri=https%3A%2F%2Fnextcloud.example.com&lt;/code&gt;&lt;br&gt;&#xA;Client Id: &lt;code&gt;nextcloud.example.com&lt;/code&gt;&lt;br&gt;&#xA;Client Secret: Copy from ephermal note&lt;br&gt;&#xA;Scope: &lt;code&gt;openid&lt;/code&gt;&lt;br&gt;&#xA;Groups claim (optional): &lt;code&gt;roles&lt;/code&gt;&lt;br&gt;&#xA;Button style: &lt;code&gt;Keycloak&lt;/code&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Add a mapper with &lt;em&gt;Add group mapping&lt;/em&gt;&lt;/li&gt;&#xA;&lt;li&gt;Enter in both fields: &lt;code&gt;admin&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;If you would choose to let Nextcloud create the required groups from the userinfo, it would prefix all group names with &lt;code&gt;keycloak-&lt;/code&gt;. We want to avoid this and therefore have to map every single group in Nextcloud.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Click on &lt;em&gt;Save&lt;/em&gt; at the end of the page&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;If you get a success message from Nextcloud we are ready to test the login.&lt;/p&gt;&#xA;&lt;h2 id=&#34;test-the-login&#34;&gt;Test the login&lt;/h2&gt;&#xA;&lt;p&gt;First we are going to verify the token and userinfo.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Apply the client admin role to your personal Keycloak account&lt;/li&gt;&#xA;&lt;li&gt;Open the command line, update the script below and run it.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Setttings&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_HOST&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;login.example.com&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_USERNAME&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak username&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_PASSWORD&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak password&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_REALM&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak realm name&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_CLIENT_SECRET&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak client secret&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;CLIENT_ID&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;nextcloud.example.com&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Get token&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;TOKEN&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;$(&lt;/span&gt;curl -s &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;client_id=&lt;/span&gt;$CLIENT_ID&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;client_secret=&lt;/span&gt;$KEYCLOAK_CLIENT_SECRET&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;username=&lt;/span&gt;$KEYCLOAK_USERNAME&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;password=&lt;/span&gt;$KEYCLOAK_PASSWORD&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grant_type=password&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://&lt;/span&gt;$KEYCLOAK_HOST&lt;span style=&#34;color:#e6db74&#34;&gt;/auth/realms/&lt;/span&gt;$KEYCLOAK_REALM&lt;span style=&#34;color:#e6db74&#34;&gt;/protocol/openid-connect/token&amp;#34;&lt;/span&gt; | jq -r &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;.access_token&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;)&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Use token to get userinfo&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;curl &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  -H &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Authorization: bearer &lt;/span&gt;$TOKEN&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  https://$KEYCLOAK_HOST/auth/realms/$KEYCLOAK_REALM/protocol/openid-connect/userinfo&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you get a JSON formatted response from the userinfo endpoint everything looks good. Otherwise let me know in the comments.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Open a private browser window and open Nextcloud&lt;/li&gt;&#xA;&lt;li&gt;Click on &lt;em&gt;Login with Example&lt;/em&gt;&lt;/li&gt;&#xA;&lt;li&gt;Login with the Keycloak account&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;If you see a the Nextcloud intro, congratulation! You have successfully used Keycloak to log into Nextcloud with OpenID Connect.&lt;/p&gt;&#xA;&lt;p&gt;Final note: The great benefit of using OpenID Connect over SAML is that you can still set a password for the user account and login using the email.&lt;/p&gt;&#xA;&lt;h2 id=&#34;issues&#34;&gt;Issues&lt;/h2&gt;&#xA;&lt;p&gt;&lt;strong&gt;Cryptic Username&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;By default the username is generated using the &lt;code&gt;keycloak-&lt;/code&gt; prefix, the Keycloak user id and the actual Keycloak username. To avoid this schema you need to override the &lt;em&gt;sub&lt;/em&gt; property of the userinfo. Create a mapper for the client:&lt;/p&gt;&#xA;&lt;p&gt;Name: &lt;code&gt;sub&lt;/code&gt;&lt;br&gt;&#xA;Mapper Type: &lt;code&gt;User Prooperty&lt;/code&gt;&lt;br&gt;&#xA;Property: &lt;code&gt;username&lt;/code&gt;&lt;br&gt;&#xA;Token Claim Name: &lt;code&gt;sub&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;If a user logs into Nextcloud the username will be &lt;code&gt;keycloak-$KEYCLOAK_USENRAME&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;further-readings&#34;&gt;Further Readings&lt;/h2&gt;&#xA;&lt;p&gt;Learn more about Keycloak:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://janikvonrotz.ch/2020/04/30/role-based-access-control-for-multiple-keycloak-clients&#34;&gt;Role based access control for multiple Keycloak clients&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
    <item>
      <title>Grafana OAuth with Keycloak and how to validate a JWT token</title>
      <link>https://janikvonrotz.ch/2020/08/27/grafana-oauth-with-keycloak-and-how-to-validate-a-jwt-token/</link>
      <pubDate>Thu, 27 Aug 2020 17:18:30 +0200</pubDate>
      <guid>https://janikvonrotz.ch/2020/08/27/grafana-oauth-with-keycloak-and-how-to-validate-a-jwt-token/</guid>
      <description>&lt;p&gt;In this tutorial I am going to show how you can connect a &lt;a href=&#34;https://grafana.com/&#34;&gt;Garafana&lt;/a&gt; container that is hidden behind proxy with Keycloak. We want to log into Grafana with a Keycloak user and experience a seamless SSO-flow. Therefore we are going to configure an OAuth client for Grafana.&lt;/p&gt;&#xA;&lt;p&gt;For this tutorial I assume that our two services are reachable from a public domain.&lt;/p&gt;&#xA;&lt;p&gt;Keycloak: &lt;code&gt;login.example.com&lt;/code&gt;&lt;br&gt;&#xA;Grafana: &lt;code&gt;monitor.example.com&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;Replace these domains with your case.&lt;/p&gt;&#xA;&lt;h1 id=&#34;setup-keycloak&#34;&gt;Setup Keycloak&lt;/h1&gt;&#xA;&lt;p&gt;First we are going to create a new Keycloak client. I assume Keycloak is already running and a realm has been configured.&lt;/p&gt;&#xA;&lt;p&gt;Login into Keycloak and select &lt;em&gt;Configure &amp;gt; Clients &amp;gt; Create&lt;/em&gt;.&lt;br&gt;&#xA;Create a new client with these configurations:&lt;/p&gt;&#xA;&lt;p&gt;Client ID: &lt;code&gt;monitor.example.com&lt;/code&gt;&lt;br&gt;&#xA;Client Protocol: &lt;code&gt;openid-connect&lt;/code&gt;&lt;br&gt;&#xA;Root URL: &lt;code&gt;https://monitor.example.com&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;Further make these configurations:&lt;/p&gt;&#xA;&lt;p&gt;Access Type: &lt;code&gt;confidentials&lt;/code&gt; &lt;em&gt;// The OAuth client must use a client id and secret.&lt;/em&gt;&lt;br&gt;&#xA;Root URL: &lt;code&gt;${authBaseUrl}&lt;/code&gt;&lt;br&gt;&#xA;Valid Redirect URIs: &lt;code&gt;https://monitor.example.com/login/generic_oauth&lt;/code&gt;&lt;br&gt;&#xA;Base URL: &lt;code&gt;/login/generic_oauth&lt;/code&gt;&lt;br&gt;&#xA;Clear &lt;em&gt;Admin URL&lt;/em&gt; and &lt;em&gt;Web Origins&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Click save and open the &lt;em&gt;Credentials&lt;/em&gt; tab. Copy the Secret into a separate note, we will need it in the second and third part of this tutorial.&lt;/p&gt;&#xA;&lt;p&gt;Open the tab &lt;em&gt;Roles&lt;/em&gt; and click &lt;em&gt;Add Role&lt;/em&gt;. Create a new role with name &lt;code&gt;admin&lt;/code&gt;. This role defines the access level for Grafana.&lt;/p&gt;&#xA;&lt;p&gt;Assign the client role to your Keycloak user.&lt;/p&gt;&#xA;&lt;p&gt;Header over to &lt;em&gt;Scope&lt;/em&gt; tab and set &lt;em&gt;Full Scope Allowed&lt;/em&gt; to &lt;code&gt;OFF&lt;/code&gt;. We do not want to share any other details about the realm in the client token.&lt;/p&gt;&#xA;&lt;p&gt;Finally, we are going to configure a client mapper for the roles property. We must ensure that Grafana can extract the access role from the JWT token. Open the &lt;em&gt;Mappers&lt;/em&gt; tab and click on &lt;em&gt;Create&lt;/em&gt;. Create an entry with these options:&lt;/p&gt;&#xA;&lt;p&gt;Name: &lt;code&gt;Roles&lt;/code&gt;&lt;br&gt;&#xA;Mapper Type: &lt;code&gt;User Client Role&lt;/code&gt;&lt;br&gt;&#xA;Client ID: &lt;code&gt;monitor.example.com&lt;/code&gt;&lt;br&gt;&#xA;Token Claim Name: &lt;code&gt;roles&lt;/code&gt;&lt;br&gt;&#xA;Claim JSON type: &lt;code&gt;string&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;In the next step we are going to verify that Grafana can retrieve a valid access token.&lt;/p&gt;&#xA;&lt;h1 id=&#34;verify-jwt-token&#34;&gt;Verify JWT token&lt;/h1&gt;&#xA;&lt;p&gt;Open your shell, enter the command below and populate the &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;-fields. Copy the client secret from the note.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_USERNAME&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak username&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_PASSWORD&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak password&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_REALM&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak realm name&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;KEYCLOAK_CLIENT_SECRET&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&amp;lt;Keycloak client secret&amp;gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;curl -s &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;client_id=monitor.example.com&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;client_secret=&lt;/span&gt;$KEYCLOAK_CLIENT_SECRET&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;username=&lt;/span&gt;$KEYCLOAK_USERNAME&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;password=&lt;/span&gt;$KEYCLOAK_PASSWORD&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;-d &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;grant_type=password&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://login.example.com/auth/realms/&lt;/span&gt;$KEYCLOAK_REALM&lt;span style=&#34;color:#e6db74&#34;&gt;/protocol/openid-connect/token&amp;#34;&lt;/span&gt; | jq -r &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;.access_token&amp;#39;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then copy the encoded output, open &lt;a href=&#34;https://jwt.io#debugger-io&#34;&gt;https://jwt.io#debugger-io&lt;/a&gt; and paste it into the left box. On the rights side you should find the decoded JSON output with this property:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;roles&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;:&lt;/span&gt; [&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;admin&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;,&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This means the client role has been added to the JWT token and mapped correctly .&lt;/p&gt;&#xA;&lt;p&gt;Grafana&amp;rsquo;s generic OAuth can be configured to look for this property using a &lt;a href=&#34;https://jmespath.org/&#34;&gt;JMESPath&lt;/a&gt;. Open this &lt;a href=&#34;https://jmespath.org/&#34;&gt;site&lt;/a&gt;, paste the decoded output of the JWT token and enter this filter:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-txt&#34; data-lang=&#34;txt&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;contains(roles[*], &amp;#39;admin&amp;#39;) &amp;amp;&amp;amp; &amp;#39;Admin&amp;#39; || contains(roles[*], &amp;#39;editor&amp;#39;) &amp;amp;&amp;amp; &amp;#39;Editor&amp;#39; || &amp;#39;Viewer&amp;#39;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The results box should say &lt;code&gt;Admin&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h1 id=&#34;grafana&#34;&gt;Grafana&lt;/h1&gt;&#xA;&lt;p&gt;We assume that the Grafana container is running and needs to be configured for OAuth access.&lt;/p&gt;&#xA;&lt;p&gt;My first choice of configuring any container is using environment variables. Luckily all Grafana settings can be set using environment variables.&lt;/p&gt;&#xA;&lt;p&gt;Make the following configurations for the Grafana container:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yml&#34; data-lang=&#34;yml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_SERVER_DOMAIN&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;monitor.example.com&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_SERVER_ROOT_URL&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://monitor.exmpale.com&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_ENABLED&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;true&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_NAME&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Login Keycloak&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_ALLOW_SIGN_UP&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;true&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_CLIENT_ID&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;monitor.example.com&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;$KEYCLOAK_CLIENT_SECRET&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_SCOPES&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;profile&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_AUTH_URL&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://login.example.com/auth/realms/example.com/protocol/openid-connect/auth&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_TOKEN_URL&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://login.example.com/auth/realms/example.com/protocol/openid-connect/token&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_API_URL&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://login.example.com/auth/realms/example.com/protocol/openid-connect/userinfo&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;GF_AUTH_GENERIC_OAUTH_ROLE_ATTRIBUTE_PATH&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;contains(roles[*], &amp;#39;admin&amp;#39;) &amp;amp;&amp;amp; &amp;#39;Admin&amp;#39; || contains(roles[*], &amp;#39;editor&amp;#39;) &amp;amp;&amp;amp; &amp;#39;Editor&amp;#39; || &amp;#39;Viewer&amp;#39;&amp;#34;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Test&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Once everything is deployed logout of Grafana and click on the &lt;code&gt;Login Keycloak&lt;/code&gt; button below the login form. You will be forwarded to Keycloak. Keycloak will check the redirect url and client key of the request. If everything looks good to go, you should see the Keycloak login form. Login using the Keycloak user and password and you should be redirected back to Grafana on a successful login. Grafana will create a user if it does not already exist.&lt;/p&gt;&#xA;&lt;h1 id=&#34;further-readings&#34;&gt;Further readings&lt;/h1&gt;&#xA;&lt;p&gt;You want to access restricitions for the Grafana client? See my post &lt;a href=&#34;https://janikvonrotz.ch/2020/04/30/role-based-access-control-for-multiple-keycloak-clients/&#34;&gt;role based access control for multiple Keycloak clients&lt;/a&gt; for details.&lt;/p&gt;&#xA;&lt;p&gt;The Grafana and Prometheus documentation is one of the best documentation I have seen so far. Make sure to check it out.&lt;/p&gt;&#xA;&lt;h1 id=&#34;source&#34;&gt;Source&lt;/h1&gt;&#xA;&lt;p&gt;Here are the articles that helped me creating this tutorial:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://www.janua.fr/keycloak-access-token-verification-example/&#34;&gt;Janua - Keycloak Access Token verification example&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://grafana.com/docs/grafana/latest/auth/generic-oauth/&#34;&gt;Grafana Labs - Generic OAuth Authentication&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://www.techrunnr.com/how-to-setup-oauth-for-grafana-using-keycloak/&#34;&gt;Techrunnr - How to setup OAuth for Grafana using Keycloak&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
    <item>
      <title>Role based access control for multiple Keycloak clients</title>
      <link>https://janikvonrotz.ch/2020/04/30/role-based-access-control-for-multiple-keycloak-clients/</link>
      <pubDate>Thu, 30 Apr 2020 22:38:57 +0200</pubDate>
      <guid>https://janikvonrotz.ch/2020/04/30/role-based-access-control-for-multiple-keycloak-clients/</guid>
      <description>&lt;p&gt;Role based access control (RBAC) is a common feature in identity and access management (IAM) systems. Granting access to applications by assigning roles to a selection of users is the proper way to manage access permissions.&lt;/p&gt;&#xA;&lt;p&gt;In this guide I will show you how this can be implemented with Keycloak. We will create a authentication flow that checks if a user is eligible to access the client. This authentication flow can be applied to any Keycloak client.&lt;/p&gt;&#xA;&lt;p&gt;Lets assume that we have set up the following Keycloak resources:&lt;/p&gt;&#xA;&lt;p&gt;Keycloak realm: example.com&lt;br&gt;&#xA;Openid-connect client: app.example.com&lt;/p&gt;&#xA;&lt;p&gt;Now are going implement a custom authentication flow script. Therefore we have to enable the script feature for Keycloak.&lt;/p&gt;&#xA;&lt;h1 id=&#34;enable-scripts-for-keycloak&#34;&gt;Enable scripts for Keycloak&lt;/h1&gt;&#xA;&lt;p&gt;Run the Keycloak server with the following option:&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;-Dkeycloak.profile.feature.upload_scripts=enabled&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;If you run Keycloak with Docker you can pass the option as command argument.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;docker run -p 8080:8080 jboss/keycloak -Dkeycloak.profile.feature.upload_scripts=enabled&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;In the log you should see these entries:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-txt&#34; data-lang=&#34;txt&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;11:33:23,596 WARN  [org.keycloak.common.Profile] (ServerService Thread Pool -- 62) Deprecated feature enabled: upload_scripts&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;11:33:23,598 WARN  [org.keycloak.common.Profile] (ServerService Thread Pool -- 62) Preview feature enabled: scripts&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next we are going to create the client role.&lt;/p&gt;&#xA;&lt;h1 id=&#34;create-client-role&#34;&gt;Create client role&lt;/h1&gt;&#xA;&lt;p&gt;In your realm navigate to &lt;em&gt;Configure &amp;gt; Clients &amp;gt; app.example.com &amp;gt; Roles &amp;gt; Add Role&lt;/em&gt;. Define &lt;code&gt;access&lt;/code&gt; as the role name.&lt;/p&gt;&#xA;&lt;p&gt;Assign this role to users which should be allowed to access the client.&lt;/p&gt;&#xA;&lt;p&gt;Now it is time to update the authentication flow.&lt;/p&gt;&#xA;&lt;h1 id=&#34;keycloak-flow&#34;&gt;Keycloak flow&lt;/h1&gt;&#xA;&lt;p&gt;Navigate to &lt;em&gt;Configure &amp;gt; Authentication &amp;gt; Flow&lt;/em&gt; and select the &lt;em&gt;Browser&lt;/em&gt; flow. Make a copy of the flow and set as &lt;em&gt;New Name&lt;/em&gt; &lt;code&gt;browser role access control&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;At the end of the &lt;em&gt;Browser Role Access Control Forms&lt;/em&gt; row click on &lt;em&gt;Actions &amp;gt; Add execution&lt;/em&gt;. Select &lt;em&gt;Script&lt;/em&gt; from the provider list and hit save. Move the new entry with the arrow button up until it is below the &lt;em&gt;Username Password Form&lt;/em&gt; row. Then set the requirement to &lt;em&gt;REQUIRED&lt;/em&gt; for the Script row.&lt;/p&gt;&#xA;&lt;p&gt;Now we will add the script that checks if a user has a client role. Click on &lt;em&gt;Actions &amp;gt; Config&lt;/em&gt; at the end of the Script row. Update the script as showed below.&lt;/p&gt;&#xA;&lt;p&gt;Alias: &lt;code&gt;hasClientRole&lt;/code&gt;&lt;br&gt;&#xA;Script Name: &lt;code&gt;hasClientRole&lt;/code&gt;&lt;br&gt;&#xA;Script Source:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;/*&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * Template for JavaScript based authenticator&amp;#39;s.&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * See org.keycloak.authentication.authenticators.browser.ScriptBasedAuthenticatorFactory&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; */&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;FormMessage&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Java&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;type&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;org.keycloak.models.utils.FormMessage&amp;#39;&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;/**&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * An example authenticate function.&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; *&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * The following variables are available for convenience:&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * user - current user {@see org.keycloak.models.UserModel}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * realm - current realm {@see org.keycloak.models.RealmModel}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * session - current KeycloakSession {@see org.keycloak.models.KeycloakSession}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * httpRequest - current HttpRequest {@see org.jboss.resteasy.spi.HttpRequest}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * script - current script {@see org.keycloak.models.ScriptModel}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * authenticationSession - current authentication session {@see org.keycloak.sessions.AuthenticationSessionModel}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * LOG - current logger {@see org.jboss.logging.Logger}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; *&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * You one can extract current http request headers via:&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * httpRequest.getHttpHeaders().getHeaderString(&amp;#34;Forwarded&amp;#34;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; *&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; * @param context {@see org.keycloak.authentication.AuthenticationFlowContext}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt; */&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;authenticate&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;context&lt;/span&gt;) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;username&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;user&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;?&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;user&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;username&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;anonymous&amp;#34;&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;LOG&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;info&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;script&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34; trace auth for: &amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;username&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;client&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;session&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;getContext&lt;/span&gt;().&lt;span style=&#34;color:#a6e22e&#34;&gt;getClient&lt;/span&gt;();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;//var rolesClient = user.getClientRoleMappings(client);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;var&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;roleModel&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;client&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;getRole&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;access&amp;#34;&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// if (rolesClient.isEmpty()) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#f92672&#34;&gt;!&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;user&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;hasRole&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;roleModel&lt;/span&gt;)) {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#a6e22e&#34;&gt;context&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;forkWithErrorMessage&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;FormMessage&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;label&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;User is not allowed to access this client.&amp;#39;&lt;/span&gt;));&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;context&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;success&lt;/span&gt;();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Update 1:&lt;/strong&gt; The script has been updated to check if user actually has the role &lt;em&gt;access&lt;/em&gt; and not only any role of the client. This change was proposed in the comments by @rogier.&lt;/p&gt;&#xA;&lt;p&gt;The script retrieves the roles mappings of the current client from the session context and if the user has not assigned any roles it will deny access.&lt;/p&gt;&#xA;&lt;p&gt;Just to make sure that the flow is setup correctly have a look here:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://janikvonrotz.ch/images/keycloak-authentication-flow.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;As the final step navigate to &lt;em&gt;Configure &amp;gt; Clients &amp;gt; app.example.com &amp;gt; Settings &amp;gt; Authentication Flow Overrides&lt;/em&gt; and select for &lt;em&gt;Browser Flow&lt;/em&gt; the new authentication flow.&lt;/p&gt;&#xA;&lt;h1 id=&#34;test-the-login&#34;&gt;Test the login&lt;/h1&gt;&#xA;&lt;p&gt;Testing is quite simple. Create two users. One user has the client role &lt;em&gt;access&lt;/em&gt; assigned and the other user doesn&amp;rsquo;t. Open your client an initiate the auth process. Log in with the first user and the auth flow should work without interruption. Log in with the second user and you should see the error message &lt;em&gt;User is not allowed to access this client.&lt;/em&gt;.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Odoo OAuth authentication with Keycloak</title>
      <link>https://janikvonrotz.ch/2020/04/24/odoo-oauth-authentication-with-keycloak/</link>
      <pubDate>Fri, 24 Apr 2020 17:08:49 +0200</pubDate>
      <guid>https://janikvonrotz.ch/2020/04/24/odoo-oauth-authentication-with-keycloak/</guid>
      <description>&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;&#xA;&lt;p&gt;OAuth is an authorization framework that enables applications to obtain limited access to user accounts on an HTTP service. It works by delegating user authentication to the service that hosts the user account, and authorizing third-party applications to access the user account.&lt;/p&gt;&#xA;&lt;p&gt;In our scenario Keycloak acts as the OAuth service and Odoo as the application that delegates the user authentication. In this guide you learn how to configure Odoo and Keycloak to handle an implicit OAuth flow.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://janikvonrotz.ch/images/oauth%20implicit%20flow.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;This image depicts what we want to achieve. The user accesses Odoo and then decides to authenticate with Keycloak. He gets forwarded to the login page and authorizes the Odoo application to access his account informations. He then gets redirected back to the application. Trust is enabled by only allowing selected applications to be redirected. If you want to know more about OAuth authentication head down to the source chapter.&lt;/p&gt;&#xA;&lt;p&gt;We assume that we have the following service up and running:&lt;br&gt;&#xA;Keycloak Auth Server: &lt;code&gt;login.example.com&lt;/code&gt;&lt;br&gt;&#xA;Odoo Application: &lt;code&gt;odoo.example.com&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s get started!&lt;/p&gt;&#xA;&lt;h1 id=&#34;setup-keycloak-client&#34;&gt;Setup Keycloak client&lt;/h1&gt;&#xA;&lt;p&gt;Open the Keycloak management console, select your realm, navigate to &lt;em&gt;Configure &amp;gt; Clients&lt;/em&gt; and create a new client.&lt;/p&gt;&#xA;&lt;p&gt;For Client ID use &lt;code&gt;odoo&lt;/code&gt;, for Client Protocol &lt;em&gt;openid-connect&lt;/em&gt; and as Root URL enter &lt;code&gt;${authBaseUrl}&lt;/code&gt;. Click save.&lt;/p&gt;&#xA;&lt;p&gt;In the client edit view make the following configurations.&lt;/p&gt;&#xA;&lt;p&gt;Access type: &lt;em&gt;confidential&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Odoo OAuth will pass a secret to intiate the login protocol.&lt;/p&gt;&#xA;&lt;p&gt;Implicit Flow Enabled: &lt;em&gt;On&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Odoo OAuth requires the implicit flow.&lt;/p&gt;&#xA;&lt;p&gt;Valid Redirect URIs:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;/realms/example.com/account/*&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;http://odoo.example.com/auth_oauth/signin&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;http://localhost:8069/auth_oauth/signin&lt;/code&gt; &lt;em&gt;for development purposes&lt;/em&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Allow redirection to the odoo login page.&lt;/p&gt;&#xA;&lt;p&gt;Base URL: &lt;code&gt;/realms/example.com/account/&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;Leave the &lt;em&gt;Admin URL&lt;/em&gt; and &lt;em&gt;Web Origins&lt;/em&gt; empty.&lt;/p&gt;&#xA;&lt;p&gt;Save the settings and open the &lt;em&gt;Mappers&lt;/em&gt; tab. Click on &lt;em&gt;Add Builtin&lt;/em&gt;. Select and add the &lt;code&gt;email&lt;/code&gt; entry. Open the &lt;em&gt;email&lt;/em&gt; mapper and set as &lt;em&gt;Token Claim Name&lt;/em&gt; the value &lt;code&gt;user_id&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;This will ensure that the token has the email address set as user id.&lt;/p&gt;&#xA;&lt;h1 id=&#34;update-oauth-module&#34;&gt;Update OAuth module&lt;/h1&gt;&#xA;&lt;p&gt;In order to authenticate with Keycloak the Odoo OAuth module requires some changes. I have created a Odoo module that applies the necessary changes.&lt;/p&gt;&#xA;&lt;p&gt;Download the module zip from &lt;a href=&#34;https://github.com/Mint-System/Odoo-App-Auth-OAuth-Keycloak&#34;&gt;https://github.com/Mint-System/Odoo-App-Auth-OAuth-Keycloak&lt;/a&gt; and install the module.&lt;/p&gt;&#xA;&lt;p&gt;This module makes the following changes:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Add new field &lt;code&gt;x_keycloak&lt;/code&gt; to the &lt;code&gt;auth.oauth.provider&lt;/code&gt; model.&lt;/li&gt;&#xA;&lt;li&gt;Update the &lt;code&gt;auth_oauth.view_oauth_provider_form&lt;/code&gt; view  with the field &lt;em&gt;Keycloak&lt;/em&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Override the &lt;code&gt;_auth_oauth_rpc&lt;/code&gt; and &lt;code&gt;_auth_oauth_validate&lt;/code&gt; methods of the &lt;code&gt;res.users&lt;/code&gt; class.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;The new methods support the bearer access token format and therefore make the authentication with Keycloak possible.&lt;/p&gt;&#xA;&lt;h1 id=&#34;add-keycloak-provider-in-odoo&#34;&gt;Add Keycloak provider in Odoo&lt;/h1&gt;&#xA;&lt;p&gt;Open the Odoo dashboard and navigate to &lt;em&gt;Settings &amp;gt; General Settings &amp;gt; Integrations&lt;/em&gt;. If necessary enable &lt;em&gt;OAuth Authentication&lt;/em&gt; and then click on &lt;em&gt;OAuth Providers&lt;/em&gt;. Create a new provider with the following settings:&lt;/p&gt;&#xA;&lt;p&gt;Provider Name: &lt;code&gt;Login Example&lt;/code&gt;&lt;br&gt;&#xA;Client ID: &lt;code&gt;odoo&lt;/code&gt;&lt;br&gt;&#xA;Allowed: [ x ]&lt;br&gt;&#xA;Keycloak: [ x ]&lt;br&gt;&#xA;Body: &lt;code&gt;Login Example&lt;/code&gt;&lt;br&gt;&#xA;Authentication URL: &lt;code&gt;https://login.example.com/auth/realms/example.com/protocol/openid-connect/auth&lt;/code&gt;&lt;br&gt;&#xA;Scope: &lt;code&gt;profile&lt;/code&gt;&lt;br&gt;&#xA;Validation URL: &lt;code&gt;https://login.example.com/auth/realms/example.com/protocol/openid-connect/userinfo&lt;/code&gt;&lt;/p&gt;&#xA;&lt;h1 id=&#34;update-portal-user-template&#34;&gt;Update portal user template&lt;/h1&gt;&#xA;&lt;p&gt;By default Odoo creates a portal user for users that sign in via OAuth. We want Odoo to create a internal user instead.&lt;/p&gt;&#xA;&lt;p&gt;Enable the developer mode and navigate to &lt;em&gt;Settings &amp;gt; General Settings &amp;gt; Users &amp;gt; Customer Account &amp;gt; Default Access Rights&lt;/em&gt;. Edit the template and select for &lt;em&gt;User types&lt;/em&gt;: &lt;em&gt;Internal User&lt;/em&gt;. Save the portal user template.&lt;/p&gt;&#xA;&lt;h1 id=&#34;test-the-login&#34;&gt;Test the login&lt;/h1&gt;&#xA;&lt;p&gt;In Keycloak ensure that a user has been added to the realm. If necessary create one with password credentials.&lt;/p&gt;&#xA;&lt;p&gt;Logout out of Odoo. You should now see a button &lt;em&gt;Login Example&lt;/em&gt;. Click on it and wait to be forwarded. Log in an await to be redirected. If everything has been configured properly you should now be logged in and see the Odoo activity page.&lt;/p&gt;&#xA;&lt;h1 id=&#34;source&#34;&gt;Source&lt;/h1&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://www.odoo.com/de_DE/forum/hilfe-1/question/keycloak-authentication-on-v12-146506&#34;&gt;Odoo Help - Keycloak Authentication on v12&lt;/a&gt;&lt;br&gt;&#xA;&lt;a href=&#34;https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2&#34;&gt;Digital OCean Tutorials - An Introduction to OAuth 2&lt;/a&gt;&lt;/p&gt;&#xA;&lt;h1 id=&#34;edits&#34;&gt;Edits&lt;/h1&gt;&#xA;&lt;p&gt;&lt;strong&gt;2021-03-08&lt;/strong&gt;: Set value for scope parameter when configuring the Odoo OAuth provider.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Configure SAML Authentication for Nextcloud with Keycloack</title>
      <link>https://janikvonrotz.ch/2020/04/21/configure-saml-authentication-for-nextcloud-with-keycloack/</link>
      <pubDate>Tue, 21 Apr 2020 12:05:28 +0200</pubDate>
      <guid>https://janikvonrotz.ch/2020/04/21/configure-saml-authentication-for-nextcloud-with-keycloack/</guid>
      <description>&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;&#xA;&lt;p&gt;The complex problems of identity and access management (IAM) have challenged big companies and in result we got powerful protocols, technologies and concepts such as SAML, oAuth, Keycloack, tokens and much more.&lt;/p&gt;&#xA;&lt;p&gt;The goal of IAM is simple. Centralize all identities, policies and get rid of application identity stores. Not only is more secure to manage logins in one place, but you can also offer a better user experience. As the title says we want to connect our centralized identity management software Keycloack with our application Nextcloud.&lt;/p&gt;&#xA;&lt;p&gt;Nextcloud supports multiple modules and protocols for authentication. The one that is around for quite some time is SAML. It is complicated to configure, but enojoys a broad support.&lt;/p&gt;&#xA;&lt;p&gt;The SAML authentication process step by step:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://janikvonrotz.ch/images/SAML%20Authentication.png&#34; alt=&#34;SAML Authentication&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;The service provider is Nextcloud and the identity provider is Keycloack.&lt;/p&gt;&#xA;&lt;p&gt;In this guide the keycloack service is running as &lt;code&gt;login.example.com&lt;/code&gt; and nextcloud as &lt;code&gt;cloud.example.com&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;prepare-keycloack-realm-and-key-material&#34;&gt;Prepare Keycloack realm and key material&lt;/h2&gt;&#xA;&lt;p&gt;Navigate to the Keycloack console &lt;a href=&#34;https://login.example.com/auth/admin/console&#34;&gt;https://login.example.com/auth/admin/console&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;Create a new realm &lt;em&gt;example.com&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Navigate to the &lt;em&gt;keys&lt;/em&gt; tab and copy the &lt;em&gt;Certificate&lt;/em&gt; content of the RSA entry to an empty texteditor. We require this certificate later on.&lt;/p&gt;&#xA;&lt;p&gt;Open a shell and run the following command to generate a certificate.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;openssl req  -nodes -new -x509  -keyout private.key -out public.cert&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This certificate will be used to identify the Nextcloud SP.&lt;/p&gt;&#xA;&lt;h2 id=&#34;setup-nextcloud&#34;&gt;Setup Nextcloud&lt;/h2&gt;&#xA;&lt;p&gt;Open the Nextcloud app page &lt;a href=&#34;https://cloud.example.com/index.php/settings/apps&#34;&gt;https://cloud.example.com/index.php/settings/apps&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Install the &lt;em&gt;SSO &amp;amp; SAML authentication&lt;/em&gt; app.&lt;/p&gt;&#xA;&lt;p&gt;Navigate to &lt;em&gt;Settings &amp;gt; Administration &amp;gt; SSO &amp;amp; SAML authentication&lt;/em&gt; and select &lt;em&gt;Use built-in SAML authentication&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Then walk through the configuration sections below.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Global Settings&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Select these options:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;[ - ] Only allow authentication if an account exists on some other backend. (e.g. LDAP)&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;[ - ] Use SAML auth for the Nextcloud desktop clients (requires user re-authentication)&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;[ x ] Allow the use of multiple user back-ends (e.g. LDAP)&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Allow use of multible user back-ends will allow to select the login method. You can disable this setting once Keycloak is connected successfuly.&lt;/p&gt;&#xA;&lt;p&gt;For the IDP &lt;em&gt;Provider 1&lt;/em&gt; set these configurations:&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;General&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Attribute to map the UID to: &lt;code&gt;username&lt;/code&gt;&lt;br&gt;&#xA;Optional display name: &lt;code&gt;Login Example&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;The user id will be mapped from the &lt;code&gt;username&lt;/code&gt; attribute in the SAML assertion.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Service Provider Data&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;x.509 certificate of the Service Provider: Copy the content of the &lt;code&gt;public.cert&lt;/code&gt; file.&lt;br&gt;&#xA;Private key of the Service Provider: Copy the content of the &lt;code&gt;private.key&lt;/code&gt; file.&lt;/p&gt;&#xA;&lt;p&gt;This certificate is used to sign the SAML request.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Identity Proivder Data&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Identifier of the IdP: &lt;a href=&#34;https://login.example.com/auth/realms/example.com&#34;&gt;https://login.example.com/auth/realms/example.com&lt;/a&gt;&lt;br&gt;&#xA;URL Target of the IdP where the SP will send the Authentication Request Message: &lt;a href=&#34;https://login.example.com/auth/realms/example.com/protocol/saml&#34;&gt;https://login.example.com/auth/realms/example.com/protocol/saml&lt;/a&gt;&lt;br&gt;&#xA;URL Location of IdP where the SP will send the SLO Request: &lt;a href=&#34;https://login.example.com/auth/realms/example.com/protocol/saml&#34;&gt;https://login.example.com/auth/realms/example.com/protocol/saml&lt;/a&gt;&lt;br&gt;&#xA;Public X.509 certificate of the IdP: Copy the certificate from the texteditor. You will need to add &lt;code&gt;-----BEGIN CERTIFICATE-----&lt;/code&gt; in front of the key and &lt;code&gt;-----END CERTIFICATE-----&lt;/code&gt; to the end of it.&lt;/p&gt;&#xA;&lt;p&gt;This certificate is used to sign the SAML assertion.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Attribute mapping&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Attribute to map the email address to.: &lt;code&gt;email&lt;/code&gt;&lt;br&gt;&#xA;Attribute to map the user groups to.: &lt;code&gt;Role&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;The email address and role assignment are managed in Keycloack, therefor we need to map this attributes from the SAML assertion.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Security Settings&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Check the following options:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://janikvonrotz.ch/images/Nextcloud%20SAML%20security%20settings.png&#34; alt=&#34;Nextcloud SAML security settings&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;Validate the metadata and download the &lt;code&gt;metadata.xml&lt;/code&gt; file.&lt;/p&gt;&#xA;&lt;p&gt;We are ready to register the SP in Keycloack.&lt;/p&gt;&#xA;&lt;h1 id=&#34;register-service-provider&#34;&gt;Register service provider&lt;/h1&gt;&#xA;&lt;p&gt;Open the Keycloack console again and select your realm. Navigate to &lt;em&gt;Clients&lt;/em&gt; and click on the &lt;em&gt;Create&lt;/em&gt; button.&lt;/p&gt;&#xA;&lt;p&gt;Use the import function to upload the &lt;code&gt;metadata.xml&lt;/code&gt; file.&lt;/p&gt;&#xA;&lt;p&gt;Update the &lt;em&gt;Client SAML Endpoint&lt;/em&gt; field with: &lt;a href=&#34;https://login.example.com/auth/realms/example.com&#34;&gt;https://login.example.com/auth/realms/example.com&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Then open the &lt;em&gt;mappers&lt;/em&gt; tab.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Mappers&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Create these mappers:&lt;/p&gt;&#xA;&lt;p&gt;Name: &lt;code&gt;username&lt;/code&gt;&lt;br&gt;&#xA;Mapper Type: &lt;em&gt;User Property&lt;/em&gt;&lt;br&gt;&#xA;Property: &lt;code&gt;username&lt;/code&gt;&lt;br&gt;&#xA;Friendly Name: &lt;code&gt;username&lt;/code&gt;&lt;br&gt;&#xA;SAML Attribute Name: &lt;code&gt;username&lt;/code&gt;&lt;br&gt;&#xA;SAML Attribute NameFormat: &lt;em&gt;Basic&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Name: &lt;code&gt;email&lt;/code&gt;&lt;br&gt;&#xA;Mapper Type: &lt;em&gt;User Property&lt;/em&gt;&lt;br&gt;&#xA;Property: &lt;code&gt;email&lt;/code&gt;&lt;br&gt;&#xA;Friendly Name: &lt;code&gt;email&lt;/code&gt;&lt;br&gt;&#xA;SAML Attribute Name: &lt;code&gt;email&lt;/code&gt;&lt;br&gt;&#xA;SAML Attribute NameFormat: &lt;em&gt;Basic&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;Name: &lt;code&gt;roles&lt;/code&gt;&lt;br&gt;&#xA;Mapper Type: &lt;em&gt;Role List&lt;/em&gt;&lt;br&gt;&#xA;Role attribute name: &lt;code&gt;Roles&lt;/code&gt;&lt;br&gt;&#xA;Friendly Name: &lt;code&gt;Roles&lt;/code&gt;&lt;br&gt;&#xA;SAML Attribute NameFormat: &lt;em&gt;Basic&lt;/em&gt;&lt;br&gt;&#xA;Single Role Attribute: &lt;em&gt;On&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;In addition the &lt;em&gt;Single Role Attribute&lt;/em&gt; option needs to be enabled in a different section. Navigate to &lt;em&gt;Configure &amp;gt; Client scopes &amp;gt; role_list &amp;gt; Mappers &amp;gt; role_list&lt;/em&gt; and toggle the &lt;em&gt;Single Role Attribute&lt;/em&gt; to &lt;em&gt;On&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;If these mappers have been created, we are ready to log in.&lt;/p&gt;&#xA;&lt;h1 id=&#34;test-the-login&#34;&gt;Test the login&lt;/h1&gt;&#xA;&lt;p&gt;First ensure that there is a Keycloack user in the realm to login with. Navigate to &lt;em&gt;Manage &amp;gt; Users&lt;/em&gt; and create a user if needed. The following attributes must be set:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Username&lt;/li&gt;&#xA;&lt;li&gt;Email&lt;/li&gt;&#xA;&lt;li&gt;Credentials&lt;/li&gt;&#xA;&lt;li&gt;Role Mappings&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;The role can be managed under &lt;em&gt;Configure &amp;gt; Roles&lt;/em&gt; and then set in the user view under the &lt;em&gt;Role Mappings&lt;/em&gt; tab. Or you can set a role per client under &lt;em&gt;Configure &amp;gt; Clients &amp;gt; &lt;em&gt;select client&lt;/em&gt; &amp;gt; Tab Roles&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;In your browser open &lt;a href=&#34;https://cloud.example.com&#34;&gt;https://cloud.example.com&lt;/a&gt; and choose &lt;em&gt;login.example.com&lt;/em&gt;. You will now be redirected to the Keycloack login page. Enter your credentials and on a successfull login you should see the Nextcloud home page.&lt;/p&gt;&#xA;&lt;p&gt;Nextcloud will create the user if it is not available.&lt;/p&gt;&#xA;&lt;p&gt;If you see the Nextcloud welcome page everything worked!&lt;/p&gt;&#xA;&lt;h1 id=&#34;troubleshooting&#34;&gt;Troubleshooting&lt;/h1&gt;&#xA;&lt;p&gt;&lt;strong&gt;Debugging&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Error logging is very restict in the auth process. Set &lt;code&gt;&#39;debug&#39; =&amp;gt; true,&lt;/code&gt; in the Nextcloud &lt;code&gt;config.php&lt;/code&gt; to get more details.&lt;/p&gt;&#xA;&lt;h1 id=&#34;source&#34;&gt;Source&lt;/h1&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://www.muehlencord.de/wordpress/2019/12/14/nextcloud-sso-using-keycloak/&#34;&gt;Jörns Blog - Nextcloud SSO using Keycloak&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://stackoverflow.com/questions/48400812/sso-with-saml-keycloak-and-nextcloud&#34;&gt;stack overflow - SSO with SAML, Keycloak and Nextcloud&lt;/a&gt;&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
