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
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
conf/defaults.ini:949-959
OAuth 2.0 Authentication
Grafana supports multiple OAuth providers through a unified OAuth strategy implementation.Supported Providers
- GitHub
- Azure AD
- Generic OAuth
- Okta
- GitLab
pkg/services/ssosettings/strategies/oauth_strategy.go
Generic OAuth Configuration
conf/defaults.ini:906-947
OAuth Strategy Implementation
The OAuth strategy loads settings from the configuration file and provides them to the authentication system: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
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 frompkg/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
pkg/services/ssosettings/strategies/saml_strategy.go:32-75
JWT Authentication
JWT authentication allows stateless authentication using JSON Web Tokens.Configuration
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 servicepkg/services/auth/jwt/validation.go- Token validationpkg/services/auth/jwt/key_sets.go- JWK set management
Auth Proxy
Auth proxy allows authentication through a reverse proxy that adds authentication headers.Configuration
conf/defaults.ini:961-971
Authentication Middleware
The authentication middleware enforces authentication requirements and handles redirects.Middleware Options
pkg/middleware/auth.go:26-30
Authentication Flow
- Token Lookup: The middleware checks for authentication tokens in cookies or headers
- User Validation: Validates the token and loads user information
- Authorization Check: Verifies user has required permissions
- Redirect Handling: Redirects to login if authentication fails
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
pkg/services/auth/auth.go:80-96
External Sessions
External sessions link OAuth/SAML sessions to Grafana user tokens:Security Best Practices
Token Security
- Token Rotation: Tokens are automatically rotated on use
- Token Revocation: Supports both soft and hard token revocation
- Concurrent Session Limits: Enforce maximum concurrent sessions per user
Configuration Security
-
Secret Management: Use environment variables for secrets
-
TLS Requirements: Always use TLS in production
-
Disable Anonymous Access: For production environments
OAuth/OIDC Security
-
Use PKCE: Enable PKCE for public clients
-
Validate ID Tokens: Verify token signatures
-
Restrict Domains: Limit authentication to specific domains
LDAP Security
-
Use TLS: Enable LDAPS or StartTLS
-
Certificate Validation: Verify LDAP server certificates
- Service Account: Use dedicated LDAP bind account with minimal permissions
Troubleshooting
Enable Debug Logging
Common Issues
- OAuth Redirect URI Mismatch: Ensure redirect URI in provider matches Grafana URL
- LDAP Connection Timeout: Check network connectivity and firewall rules
- SAML Clock Skew: Verify time synchronization between systems
- JWT Validation Failures: Check key configuration and claim mapping
Related Topics
- Authorization - Role-based access control
- TLS/SSL Configuration - Secure communication
- Security Best Practices - Comprehensive security guide