Authentication Methods and SSO
PostHog supports multiple authentication methods including password-based login, social SSO, SAML, and two-factor authentication. This guide covers setup and configuration for each method.Authentication Methods
PostHog implements several authentication backends (posthog/auth.py):
- Password Authentication - Traditional email/password login
- Personal API Keys - For programmatic access
- OAuth/Social SSO - Google, GitHub, GitLab, etc.
- SAML - Enterprise SSO (domain-based)
- WebAuthn/Passkeys - Passwordless authentication
- Project Secret Keys - Server-side feature flag evaluation
Password Authentication
Standard Login Flow
Password authentication uses Django’s authentication backend with additional security layers:Password Requirements
PostHog uses thezxcvbn library for password strength validation (posthog/auth.py:111-128):
- Minimum score of 3 out of 4
- Rejects common passwords
- Provides feedback for weak passwords
- No hard character requirements (length, symbols, etc.)
Password validation happens client-side during signup and server-side on password changes.
Password Reset
Self-service password reset flow:
Implementation at
posthog/api/authentication.py:808-888.
Password reset is disabled for domains with SSO enforcement. Users see an error message directing them to SSO login.
Personal API Keys
Personal API keys provide programmatic access to the PostHog API.Creating API Keys
- Go to Personal settings → API keys
- Click Create personal API key
- Set label and optional scopes
- Copy the key (shown once)
Using API Keys
Three authentication methods (posthog/auth.py:159-196):
API Key Security
Keys are hashed using SHA-256 before storage (posthog/models/personal_api_key.py):
- Organization scoping (optional)
- Last used tracking
- Automatic expiration after 1 year of inactivity
Restricting API Key Usage
Organization admins can disable personal API keys:- Go to Organization settings → Security
- Toggle Members can use personal API keys
- Existing keys are immediately invalidated
ORGANIZATION_SECURITY_SETTINGS feature.
Two-Factor Authentication (2FA)
Enabling 2FA for Users
Users can enable 2FA via TOTP or passkeys:Complete Setup
- TOTP: Scan QR code with authenticator app
- Passkey: Follow browser prompts to register device
TOTP (Authenticator Apps)
Supported apps:- Google Authenticator
- Authy
- 1Password
- Any TOTP-compatible app
django-otp library with 30-second time windows.
Passkeys (WebAuthn)
Passkeys provide passwordless authentication:- Biometric authentication (Face ID, Touch ID, Windows Hello)
- Hardware security keys (YubiKey, etc.)
- Platform authenticators (built into devices)
posthog/auth.py:737-838 and posthog/passkey.py.
Passkeys can be used for both primary login and 2FA verification. Users control this via the
passkeys_enabled_for_2fa setting.Enforcing 2FA Organization-Wide
Organization admins can require 2FA for all members:- Go to Organization settings → Security
- Enable Enforce two-factor authentication
- Members without 2FA are prompted to set it up
TWO_FACTOR_ENFORCEMENT feature flag.
2FA Recovery
If users lose access to their 2FA device:- Use backup codes (generated during setup)
- Contact organization admin for reset
- Admins can reset 2FA at
/api/users/{id}/two_factor_reset/
Email MFA (Fallback)
For users without TOTP or passkeys, PostHog sends a verification link via email:- Valid for 15 minutes
- Single-use tokens
- Rate-limited to prevent abuse
Social SSO (OAuth)
PostHog supports OAuth providers:- GitHub
- GitLab
- Custom OAuth2 providers
Enabling SSO Providers
For self-hosted instances, configure via environment variables:SSO Login Flow
- User clicks “Continue with Google” (or other provider)
- Redirected to provider’s authorization page
- User approves access
- Redirected back to PostHog with auth code
- PostHog exchanges code for user info
- Account created or matched by email
- User logged in
posthog/api/authentication.py:141-153.
SSO Account Linking
Users can link multiple SSO providers to one account:- Email must match across providers
- First login with email creates the account
- Subsequent SSO logins link to existing account
- Users can unlink providers in settings
SAML Authentication
SAML provides enterprise SSO at the domain level.SAML Configuration
SAML is configured per organization domain (not instance-wide):- Go to Organization settings → Authentication domains
- Add your domain (e.g.,
company.com) - Upload IdP metadata XML or enter details manually:
- SSO URL
- Entity ID
- X.509 Certificate
- Configure attribute mapping
- Test the integration
SAML requires the enterprise license (
EE_AVAILABLE). The feature is domain-based, allowing different SAML configs for different email domains.SAML Enforcement
Organization domains can enforce SAML login:- Password login disabled for that domain
- OAuth login disabled
- Password reset disabled
- Users redirected to SAML login
SAML Attribute Mapping
Map SAML attributes to PostHog user fields:Session Management
Session Duration
Default session settings:Remember Device (2FA)
2FA can be skipped on trusted devices:Session Expiration
2FA sessions expire after inactivity:Security Features
Rate Limiting
Authentication endpoints are rate-limited:- Login: Handled by
django-axes(5 attempts per 30 minutes) - Password reset:
UserPasswordResetThrottle(3 per hour) - 2FA verification:
TwoFactorThrottle(10 per minute) - Email MFA:
EmailMFAThrottle(5 per hour)
Account Lockout
After failed login attempts:“Too many failed login attempts. Please try again in 30 minutes.”
Login Notifications
Users receive email notifications for:- New device login
- Backup code usage
- Password reset
- SSO account linking
posthog/tasks/email.py:login_from_new_device_notification.