Keycloak SSO for Grafana
Sign in to Grafana with Keycloak through Generic OAuth, map Keycloak groups to Grafana roles with JMESPath, and log out of both at once.
Grafana signs users in with Keycloak through its Generic OAuth provider. The Grafana server exchanges the authorization code with Keycloak, so the client is confidential and has a secret.
Checked against Grafana 13.3 docs and Keycloak 26.8 on 25 November 2026.
Replace auth.example.com with your RealmDeploy hostname and acme with your realm. The examples use https://grafana.example.com for Grafana.
In Keycloak
Create the client
Open Clients, choose Create client, and use these values:
| Setting | Value |
|---|---|
| Client type | OpenID Connect |
| Client ID | grafana |
| Client authentication | On |
| Authentication flow | Standard flow only |
| Root URL | https://grafana.example.com |
| Home URL | https://grafana.example.com |
| Valid redirect URIs | https://grafana.example.com/login/generic_oauth |
| Valid post logout redirect URIs | https://grafana.example.com/login |
| Web origins | Leave empty. Grafana talks to Keycloak from its server, not from the browser. |
Choose Save, then copy the secret from the Credentials tab.
Grafana's own Keycloak page also sets Admin URL. Leave it empty. Keycloak uses that URL to call the app from your tenant, and outgoing connections from your tenant are closed by default. Grafana does not need it: login and logout here both run through the browser.
Add a groups claim
Grafana reads roles from the ID token first, so the groups have to be in the ID token.
- On the
grafanaclient, open the Client scopes tab and choosegrafana-dedicated. - Choose Configure a new mapper and pick Group Membership.
- Fill it in and choose Save:
| Field | Value |
|---|---|
| Name | groups |
| Token Claim Name | groups |
| Full group path | Off |
| Add to ID token | On |
| Add to access token | Off |
| Add to userinfo | On |
Then create the groups under Groups, for example grafana-admins and grafana-editors, and add users to them.
In Grafana
Put the settings in the [auth.generic_oauth] section of grafana.ini. root_url must be the address users open in the browser, because Grafana builds the redirect URI from it.
[server]
root_url = https://grafana.example.com
[auth.generic_oauth]
enabled = true
name = Keycloak
allow_sign_up = true
client_id = grafana
client_secret = CLIENT_SECRET_FROM_KEYCLOAK
scopes = openid email profile offline_access
auth_url = https://auth.example.com/realms/acme/protocol/openid-connect/auth
token_url = https://auth.example.com/realms/acme/protocol/openid-connect/token
api_url = https://auth.example.com/realms/acme/protocol/openid-connect/userinfo
email_attribute_path = email
login_attribute_path = preferred_username
name_attribute_path = name
groups_attribute_path = groups
role_attribute_path = contains(groups[*], 'grafana-admins') && 'Admin' || contains(groups[*], 'grafana-editors') && 'Editor' || 'Viewer'
use_pkce = true
use_refresh_token = true
signout_redirect_url = https://auth.example.com/realms/acme/protocol/openid-connect/logout?post_logout_redirect_uri=https%3A%2F%2Fgrafana.example.com%2FloginWhat the settings do:
| Setting | Meaning |
|---|---|
auth_url, token_url, api_url | Keycloak's authorization, token and userinfo endpoints for your realm. |
scopes | offline_access gives Grafana a refresh token, which use_refresh_token = true uses to renew the session. Grafana's Keycloak page asks for both. |
login_attribute_path, name_attribute_path | Keycloak puts the username in preferred_username and the display name in name. |
role_attribute_path | A JMESPath expression that returns Admin, Editor or Viewer. See Groups and roles. |
use_pkce | Sends a PKCE S256 challenge with the login. |
signout_redirect_url | Keycloak's logout endpoint. Grafana adds id_token_hint itself, and Keycloak sends the user back to the post_logout_redirect_uri, which must be listed in Valid post logout redirect URIs. |
Restart Grafana. The login page now shows Sign in with Keycloak.
The same settings as environment variables
Every option can also be set as GF_<SECTION>_<KEY>, in capitals, with dots replaced by underscores. For Docker Compose:
services:
grafana:
image: grafana/grafana:13.3.2
environment:
GF_SERVER_ROOT_URL: "https://grafana.example.com"
GF_AUTH_GENERIC_OAUTH_ENABLED: "true"
GF_AUTH_GENERIC_OAUTH_NAME: "Keycloak"
GF_AUTH_GENERIC_OAUTH_ALLOW_SIGN_UP: "true"
GF_AUTH_GENERIC_OAUTH_CLIENT_ID: "grafana"
GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET: "${GRAFANA_OAUTH_CLIENT_SECRET}"
GF_AUTH_GENERIC_OAUTH_SCOPES: "openid email profile offline_access"
GF_AUTH_GENERIC_OAUTH_AUTH_URL: "https://auth.example.com/realms/acme/protocol/openid-connect/auth"
GF_AUTH_GENERIC_OAUTH_TOKEN_URL: "https://auth.example.com/realms/acme/protocol/openid-connect/token"
GF_AUTH_GENERIC_OAUTH_API_URL: "https://auth.example.com/realms/acme/protocol/openid-connect/userinfo"
GF_AUTH_GENERIC_OAUTH_EMAIL_ATTRIBUTE_PATH: "email"
GF_AUTH_GENERIC_OAUTH_LOGIN_ATTRIBUTE_PATH: "preferred_username"
GF_AUTH_GENERIC_OAUTH_NAME_ATTRIBUTE_PATH: "name"
GF_AUTH_GENERIC_OAUTH_GROUPS_ATTRIBUTE_PATH: "groups"
GF_AUTH_GENERIC_OAUTH_ROLE_ATTRIBUTE_PATH: "contains(groups[*], 'grafana-admins') && 'Admin' || contains(groups[*], 'grafana-editors') && 'Editor' || 'Viewer'"
GF_AUTH_GENERIC_OAUTH_USE_PKCE: "true"
GF_AUTH_GENERIC_OAUTH_USE_REFRESH_TOKEN: "true"
GF_AUTH_GENERIC_OAUTH_SIGNOUT_REDIRECT_URL: "https://auth.example.com/realms/acme/protocol/openid-connect/logout?post_logout_redirect_uri=https%3A%2F%2Fgrafana.example.com%2Flogin"Grafana also has a form for the same settings under Administration → Authentication → Generic OAuth. Values saved there take over from the file.
Groups and roles
role_attribute_path is evaluated against the ID token first, then the userinfo response, then the access token. The first source that returns a role wins. Because the expression above always ends in 'Viewer', it returns a role from the ID token every time, so the groups claim must be in the ID token. That is why the mapper above has Add to ID token on.
With the mapper above, a member of grafana-admins gets an ID token like this, and Grafana gives them the Admin role:
{
"iss": "https://auth.example.com/realms/acme",
"aud": "grafana",
"preferred_username": "jane",
"groups": ["grafana-admins"]
}Other choices:
- Deny users with no matching group. End the expression without the fallback, for example
contains(groups[*], 'grafana-admins') && 'Admin' || contains(groups[*], 'grafana-editors') && 'Editor', and setrole_attribute_strict = true. Users outside both groups can no longer sign in. - Grafana server administrator. Return
GrafanaAdminfor a group, for examplecontains(groups[*], 'grafana-server-admins') && 'GrafanaAdmin' || ..., and setallow_assign_grafana_admin = true. - Realm roles instead of groups. Keycloak's built-in
rolesscope puts realm roles in the access token underrealm_access.roles, not in the ID token. Add a User Realm Role mapper tografana-dedicatedwith Token Claim Namerolesand Add to ID token on, then usecontains(roles[*], 'admin') && 'Admin' || .... - Team Sync.
groups_attribute_path = groupsfeeds Grafana Team Sync, which maps groups to Grafana teams. Team Sync is a Grafana Enterprise and Grafana Cloud feature.
If a group name contains a colon or quotes, Grafana's docs ask you to quote the whole JMESPath expression.
Test it
- Open
https://grafana.example.com/loginin a private window and choose Sign in with Keycloak. - Sign in as a test user in
grafana-editors. Grafana creates the user and gives them theEditorrole. - To see the claims Grafana receives, open the
grafanaclient in Keycloak, go to Client scopes → Evaluate, pick the user and look at Generated ID token. Paste a token into the JWT decoder to read it. - Sign out of Grafana. You land on Keycloak's logout and then back on Grafana's login page. Signing in again asks for the password.
Common errors
| Error | Cause | Fix |
|---|---|---|
Invalid parameter: redirect_uri on the Keycloak page | Grafana sent a redirect URI that is not in Valid redirect URIs, often http:// or the wrong host because root_url is not set. | Set root_url to the public URL and list https://grafana.example.com/login/generic_oauth exactly. Compare both with the redirect URI matcher. |
Every user gets Viewer | The groups claim is missing from the ID token, or Full group path is on, so the claim says /grafana-admins. | Turn on Add to ID token and turn off Full group path on the mapper, then check with Client scopes → Evaluate. |
unauthorized_client or invalid_client after login | Wrong client_secret, or Client authentication is off. | Copy the secret again from the Credentials tab. Look up other codes in the OAuth error lookup. |
Invalid redirect uri on logout | The post_logout_redirect_uri is not in Valid post logout redirect URIs, or it is not URL-encoded. | Add https://grafana.example.com/login to the client. Build the URL with the logout URL builder. |
| Users are logged out of Grafana after a few minutes | No refresh token, so Grafana ends the session when the access token expires. | Keep offline_access in scopes and use_refresh_token = true. |
Login fails with no role when role_attribute_strict = true | The expression returned nothing for this user. | Add the user to one of the groups, or drop role_attribute_strict. |
FAQ
Do I need api_url?
No, if the ID token already carries the email, username, name and groups. Grafana's docs note that the userinfo call adds a little latency to each login. It helps as a fallback, and when a user is in more than 150 groups.
Can I skip the Grafana login page?
Yes. Set auto_login = true in [auth.generic_oauth] and Grafana sends users straight to Keycloak.
Can users pick an outside identity provider directly?
Yes. Add kc_idp_hint to auth_url, for example .../protocol/openid-connect/auth?kc_idp_hint=google, and Keycloak skips its own login page for that provider. Keycloak calls an outside provider's token endpoint from your tenant, so tell us the provider first through How to reach us.
Sources
- Grafana, Configure Keycloak OAuth2 authentication: https://grafana.com/docs/grafana/latest/setup-grafana/configure-access/configure-authentication/keycloak/
- Grafana, Configure Generic OAuth authentication: https://grafana.com/docs/grafana/latest/setup-grafana/configure-access/configure-authentication/generic-oauth/
- Grafana, Override configuration with environment variables: https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/#override-configuration-with-environment-variables
- Grafana 13.3.2 release: https://github.com/grafana/grafana/releases/tag/v13.3.2
- Keycloak Server Administration Guide, client settings, protocol mappers and evaluating client scopes: https://www.keycloak.org/docs/latest/server_admin/
Add Keycloak login to a Laravel app
Sign users in to a Laravel 13 app with Keycloak through Socialite and the SocialiteProviders driver, protect API routes with firebase/php-jwt, and log out.
Keycloak SSO for GitLab
Sign in to GitLab Self-Managed with Keycloak through the OmniAuth OpenID Connect provider, and use Keycloak groups for admin and external users.