Skip to main content
Grafana supports multiple authentication methods to integrate with your organization’s identity infrastructure. This guide covers the configuration and implementation details of each authentication method.

Overview

Authentication in Grafana is handled through the authentication service (pkg/services/auth/) and middleware layer (pkg/middleware/auth.go). The system supports:
  • Basic authentication (username/password)
  • OAuth 2.0 providers (GitHub, Google, Azure AD, Generic OAuth)
  • LDAP/Active Directory
  • SAML 2.0
  • JWT (JSON Web Tokens)
  • Auth proxy

Basic Authentication

Basic authentication is enabled by default and uses Grafana’s internal user database.

Configuration

When password_policy is enabled, passwords must:
  • Have a minimum of 12 characters
  • Contain at least 1 uppercase character
  • Contain at least 1 lowercase character
  • Contain at least 1 digit
  • Contain at least 1 symbol
Reference: conf/defaults.ini:949-959

OAuth 2.0 Authentication

Grafana supports multiple OAuth providers through a unified OAuth strategy implementation.

Supported Providers

  • GitHub
  • Google
  • Azure AD
  • Generic OAuth
  • Okta
  • GitLab
Implementation: pkg/services/ssosettings/strategies/oauth_strategy.go

Generic OAuth Configuration

Reference: conf/defaults.ini:906-947

OAuth Strategy Implementation

The OAuth strategy loads settings from the configuration file and provides them to the authentication system:
Reference: pkg/services/ssosettings/strategies/oauth_strategy.go:69-114

Azure AD OAuth Configuration

Azure AD includes additional settings for managed identities and workload identities:

LDAP Authentication

LDAP authentication integrates with LDAP directories including Active Directory.

Configuration

Reference: conf/defaults.ini:1001-1011

LDAP Server Configuration

The LDAP configuration file (ldap.toml) defines server connections and attribute mappings:

LDAP Configuration Structure

Implementation details from pkg/services/ldap/settings.go:17-87:

SAML 2.0 Authentication

SAML authentication enables single sign-on with SAML 2.0 identity providers.

Configuration

Implementation: pkg/services/ssosettings/strategies/saml_strategy.go
Reference: pkg/services/ssosettings/strategies/saml_strategy.go:32-75

JWT Authentication

JWT authentication allows stateless authentication using JSON Web Tokens.

Configuration

Reference: conf/defaults.ini:973-999

JWT Service Implementation

The JWT service handles token validation and key management:
  • pkg/services/auth/jwt/jwt.go - Main JWT service
  • pkg/services/auth/jwt/validation.go - Token validation
  • pkg/services/auth/jwt/key_sets.go - JWK set management

Auth Proxy

Auth proxy allows authentication through a reverse proxy that adds authentication headers.

Configuration

Reference: conf/defaults.ini:961-971

Authentication Middleware

The authentication middleware enforces authentication requirements and handles redirects.

Middleware Options

Reference: pkg/middleware/auth.go:26-30

Authentication Flow

  1. Token Lookup: The middleware checks for authentication tokens in cookies or headers
  2. User Validation: Validates the token and loads user information
  3. Authorization Check: Verifies user has required permissions
  4. Redirect Handling: Redirects to login if authentication fails
Key functions in pkg/middleware/auth.go:
  • Auth() - Main authentication middleware (line 202)
  • notAuthorized() - Handles unauthorized requests (line 41)
  • accessForbidden() - Handles forbidden access (line 32)
  • tokenRevoked() - Handles revoked tokens (line 70)

Token Management

Grafana manages user authentication tokens through the UserTokenService.

Token Operations

Reference: pkg/services/auth/auth.go:80-96

External Sessions

External sessions link OAuth/SAML sessions to Grafana user tokens:

Security Best Practices

Token Security

  1. Token Rotation: Tokens are automatically rotated on use
  2. Token Revocation: Supports both soft and hard token revocation
  3. Concurrent Session Limits: Enforce maximum concurrent sessions per user

Configuration Security

  1. Secret Management: Use environment variables for secrets
  2. TLS Requirements: Always use TLS in production
  3. Disable Anonymous Access: For production environments

OAuth/OIDC Security

  1. Use PKCE: Enable PKCE for public clients
  2. Validate ID Tokens: Verify token signatures
  3. Restrict Domains: Limit authentication to specific domains

LDAP Security

  1. Use TLS: Enable LDAPS or StartTLS
  2. Certificate Validation: Verify LDAP server certificates
  3. Service Account: Use dedicated LDAP bind account with minimal permissions

Troubleshooting

Enable Debug Logging

Common Issues

  1. OAuth Redirect URI Mismatch: Ensure redirect URI in provider matches Grafana URL
  2. LDAP Connection Timeout: Check network connectivity and firewall rules
  3. SAML Clock Skew: Verify time synchronization between systems
  4. JWT Validation Failures: Check key configuration and claim mapping