5.8 KiB
5.8 KiB
Authentication Setup
This document outlines how to configure authentication for Spotizerr, including both traditional username/password authentication and SSO (Single Sign-On) with Google and GitHub.
Environment Variables
Basic Authentication
ENABLE_AUTH: Enable/disable authentication system (default: false)DISABLE_REGISTRATION: Disable public registration (default: false)JWT_SECRET: Secret key for JWT token signing (required in production)JWT_ALGORITHM: JWT algorithm (default: HS256)JWT_EXPIRATION_HOURS: JWT token expiration time in hours (default: 24)DEFAULT_ADMIN_USERNAME: Default admin username (default: admin)DEFAULT_ADMIN_PASSWORD: Default admin password (default: admin123)
SSO Configuration
SSO_ENABLED: Enable/disable SSO functionality (default: true)SSO_BASE_REDIRECT_URI: Base redirect URI for SSO callbacks (default: http://localhost:8000/api/auth/sso/callback)FRONTEND_URL: Frontend URL for post-authentication redirects (default: http://localhost:3000)
Google SSO
GOOGLE_CLIENT_ID: Google OAuth2 client IDGOOGLE_CLIENT_SECRET: Google OAuth2 client secret
GitHub SSO
GITHUB_CLIENT_ID: GitHub OAuth2 client IDGITHUB_CLIENT_SECRET: GitHub OAuth2 client secret
Setup Instructions
1. Traditional Authentication Only
- Set environment variables:
ENABLE_AUTH=true
JWT_SECRET=your-super-secret-jwt-key-change-in-production
DEFAULT_ADMIN_USERNAME=admin
DEFAULT_ADMIN_PASSWORD=your-secure-password
- Start the application - a default admin user will be created automatically.
2. Google SSO Setup
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable Google+ API
- Go to "Credentials" → "Create Credentials" → "OAuth 2.0 Client IDs"
- Configure OAuth consent screen with your application details
- Set authorized redirect URIs:
http://localhost:8000/api/auth/sso/callback/google(development)https://yourdomain.com/api/auth/sso/callback/google(production)
- Copy Client ID and Client Secret to environment variables:
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
3. GitHub SSO Setup
- Go to GitHub Settings → Developer settings → OAuth Apps
- Click "New OAuth App"
- Fill in application details:
- Application name: Your app name
- Homepage URL: Your app URL
- Authorization callback URL:
http://localhost:8000/api/auth/sso/callback/github(development)https://yourdomain.com/api/auth/sso/callback/github(production)
- Copy Client ID and Client Secret to environment variables:
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
4. Complete Environment Configuration
Create a .env file with all required variables:
# Basic Authentication
ENABLE_AUTH=true
JWT_SECRET=your-super-secret-jwt-key-change-in-production
JWT_EXPIRATION_HOURS=24
DEFAULT_ADMIN_USERNAME=admin
DEFAULT_ADMIN_PASSWORD=your-secure-password
# SSO Configuration
SSO_ENABLED=true
SSO_BASE_REDIRECT_URI=http://localhost:8000/api/auth/sso/callback
FRONTEND_URL=http://localhost:3000
# Google SSO (optional)
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
# GitHub SSO (optional)
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
API Endpoints
Authentication Status
GET /api/auth/status- Get authentication status and available SSO providers
Traditional Authentication
POST /api/auth/login- Login with username/passwordPOST /api/auth/register- Register new user (if enabled)POST /api/auth/logout- Logout current user
SSO Authentication
GET /api/auth/sso/status- Get SSO status and providersGET /api/auth/sso/login/google- Initiate Google SSO loginGET /api/auth/sso/login/github- Initiate GitHub SSO loginGET /api/auth/sso/callback/google- Google SSO callback (automatic)GET /api/auth/sso/callback/github- GitHub SSO callback (automatic)POST /api/auth/sso/unlink/{provider}- Unlink SSO provider from account
User Management (Admin only)
GET /api/auth/users- List all usersPOST /api/auth/users/create- Create new userDELETE /api/auth/users/{username}- Delete userPUT /api/auth/users/{username}/role- Update user role
Security Considerations
- HTTPS in Production: Always use HTTPS in production and set
allow_insecure_http=False - Secure JWT Secret: Use a strong, randomly generated JWT secret
- Environment Variables: Never commit sensitive credentials to version control
- CORS Configuration: Configure CORS appropriately for your frontend domain
- Cookie Security: Ensure secure cookie settings in production
User Types
The system supports two types of users:
- Traditional Users: Created via username/password registration or admin creation
- SSO Users: Created automatically when users authenticate via Google or GitHub
SSO users:
- Have
sso_providerandsso_idfields populated - Cannot use password-based authentication
- Can be unlinked from SSO providers by admins
- Get
userrole by default (first user getsadminrole)
Troubleshooting
Common Issues
-
SSO Login Fails
- Check OAuth app configuration in Google/GitHub
- Verify redirect URIs match exactly
- Ensure client ID and secret are correct
-
CORS Errors
- Configure CORS middleware to allow your frontend domain
- Check if frontend and backend URLs match configuration
-
JWT Token Issues
- Verify JWT_SECRET is set and consistent
- Check token expiration time
- Ensure clock synchronization between services
-
SSO Module Not Available
- Install fastapi-sso:
pip install fastapi-sso==0.18.0 - Restart the application after installation
- Install fastapi-sso: