RealmDeployDocs
GuidesSingle sign-on for apps

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:

SettingValue
Client typeOpenID Connect
Client IDgrafana
Client authenticationOn
Authentication flowStandard flow only
Root URLhttps://grafana.example.com
Home URLhttps://grafana.example.com
Valid redirect URIshttps://grafana.example.com/login/generic_oauth
Valid post logout redirect URIshttps://grafana.example.com/login
Web originsLeave 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.

  1. On the grafana client, open the Client scopes tab and choose grafana-dedicated.
  2. Choose Configure a new mapper and pick Group Membership.
  3. Fill it in and choose Save:
FieldValue
Namegroups
Token Claim Namegroups
Full group pathOff
Add to ID tokenOn
Add to access tokenOff
Add to userinfoOn

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.

grafana.ini
[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%2Flogin

What the settings do:

SettingMeaning
auth_url, token_url, api_urlKeycloak's authorization, token and userinfo endpoints for your realm.
scopesoffline_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_pathKeycloak puts the username in preferred_username and the display name in name.
role_attribute_pathA JMESPath expression that returns Admin, Editor or Viewer. See Groups and roles.
use_pkceSends a PKCE S256 challenge with the login.
signout_redirect_urlKeycloak'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:

docker-compose.yml
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 set role_attribute_strict = true. Users outside both groups can no longer sign in.
  • Grafana server administrator. Return GrafanaAdmin for a group, for example contains(groups[*], 'grafana-server-admins') && 'GrafanaAdmin' || ..., and set allow_assign_grafana_admin = true.
  • Realm roles instead of groups. Keycloak's built-in roles scope puts realm roles in the access token under realm_access.roles, not in the ID token. Add a User Realm Role mapper to grafana-dedicated with Token Claim Name roles and Add to ID token on, then use contains(roles[*], 'admin') && 'Admin' || ....
  • Team Sync. groups_attribute_path = groups feeds 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

  1. Open https://grafana.example.com/login in a private window and choose Sign in with Keycloak.
  2. Sign in as a test user in grafana-editors. Grafana creates the user and gives them the Editor role.
  3. To see the claims Grafana receives, open the grafana client 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.
  4. 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

ErrorCauseFix
Invalid parameter: redirect_uri on the Keycloak pageGrafana 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 ViewerThe 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 loginWrong 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 logoutThe 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 minutesNo 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 = trueThe 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

On this page