Authentication #
This section provides detailed information about the authentication system in NewsFeed.
Overview #
NewsFeed uses Casdoor for authentication and user management. Casdoor is an open-source Identity and Access Management (IAM) solution that provides:
- User authentication
- Single Sign-On (SSO)
- OAuth 2.0 support
- User management
- Role-based access control
Authentication Flow #
The authentication flow in NewsFeed works as follows:
- User clicks “Login” in the NewsFeed interface
- User is redirected to the Casdoor login page
- After successful authentication, Casdoor redirects back to NewsFeed with an authorization code
- NewsFeed exchanges the code for an access token
- The access token is stored in the browser and used for subsequent API requests
Backend Authentication #
The backend authenticates API requests using:
- JWT token validation
- Public key verification
- Role-based access control
JWT Verification #
The backend verifies JWT tokens by:
- Extracting the token from the Authorization header
- Verifying the token signature using Casdoor’s public key
- Validating token claims (expiration, issuer, etc.)
- Extracting user information from the token
Configuration #
Casdoor Configuration #
Casdoor is configured through environment variables:
# Casdoor Configuration
CASDOOR_ENDPOINT=http://casdoor:8000
CASDOOR_CLIENT_ID=your-client-id
CASDOOR_CLIENT_SECRET=your-client-secret
CASDOOR_ORG=newsfeed
CASDOOR_APP_NAME=newsfeed
CASDOOR_CERT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----"
Frontend Configuration #
The frontend is configured through environment variables:
# Casdoor configuration
NEXT_PUBLIC_CASDOOR_SERVER_URL=http://localhost:8000
NEXT_PUBLIC_CASDOOR_CLIENT_ID=your-client-id
NEXT_PUBLIC_CASDOOR_APP_NAME=newsfeed
NEXT_PUBLIC_CASDOOR_ORG_NAME=newsfeed
NEXT_PUBLIC_CASDOOR_REDIRECT_URI=http://localhost:8880/callback
User Roles #
NewsFeed supports the following user roles:
- Anonymous - Unauthenticated users with limited access
- User - Standard authenticated users
- Admin - Users with administrative privileges
Role-Based Access #
Different API endpoints have different access requirements:
- Public endpoints (no authentication required)
- User endpoints (authentication required)
- Admin endpoints (admin role required)
Custom Authentication #
If you need to use a different authentication system:
- Modify the
auth.py
file in the backend - Update the
AuthContext.tsx
file in the frontend - Configure your authentication provider
- Update the authentication flow
Troubleshooting #
Common authentication issues:
Invalid Token #
Symptoms: API requests fail with 401 Unauthorized
Solutions:
- Check that the token is being sent correctly
- Verify token expiration
- Ensure the public key is configured correctly
CORS Issues #
Symptoms: Authentication fails due to CORS errors
Solutions:
- Configure CORS settings in the backend
- Ensure the redirect URI is configured correctly
- Check browser console for specific CORS errors
Casdoor Connection Issues #
Symptoms: Unable to connect to Casdoor
Solutions:
- Verify Casdoor is running
- Check network connectivity between services
- Validate Casdoor configuration