|
| 1 | +# GitHub OAuth Setup Guide |
| 2 | + |
| 3 | +This guide explains how to configure and use GitHub OAuth authentication in llms.py. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The llms.py application now supports: |
| 8 | +1. **OAuth Authentication** (default) - Sign in with GitHub |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- ✅ GitHub OAuth 2.0 integration |
| 13 | +- ✅ Secure session management |
| 14 | +- ✅ CSRF protection with state tokens |
| 15 | +- ✅ User profile display with avatar |
| 16 | +- ✅ Logout functionality |
| 17 | +- ✅ Configurable authentication type |
| 18 | +- ✅ Environment variable support for credentials |
| 19 | + |
| 20 | +## Setup Instructions |
| 21 | + |
| 22 | +### 1. Create a GitHub OAuth App |
| 23 | + |
| 24 | +1. Go to GitHub Settings → Developer settings → OAuth Apps |
| 25 | +2. Click "New OAuth App" |
| 26 | +3. Fill in the application details: |
| 27 | + - **Application name**: `llms.py` (or your preferred name) |
| 28 | + - **Homepage URL**: `http://localhost:8080` |
| 29 | + - **Authorization callback URL**: `http://localhost:8080/auth/github/callback` |
| 30 | +4. Click "Register application" |
| 31 | +5. Note down your **Client ID** |
| 32 | +6. Click "Generate a new client secret" and note down the **Client Secret** |
| 33 | + |
| 34 | +### 2. Configure Environment Variables |
| 35 | + |
| 36 | +Set the following environment variables with your GitHub OAuth credentials: |
| 37 | + |
| 38 | +```bash |
| 39 | +export GITHUB_CLIENT_ID="your_github_client_id_here" |
| 40 | +export GITHUB_CLIENT_SECRET="your_github_client_secret_here" |
| 41 | +# Optional - Comma or space separated list of allowed users (if set restricts access to these users) |
| 42 | +export GITHUB_USERS="octocat mythz" |
| 43 | +``` |
| 44 | + |
| 45 | +For permanent configuration, add these to your shell profile (`~/.bashrc`, `~/.zshrc`, etc.). |
| 46 | + |
| 47 | +### 3. Configuration in llms.json |
| 48 | + |
| 49 | +Enable the OAuth configuration that's included in `llms/llms.json`: |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "auth": { |
| 54 | + "enabled": true, |
| 55 | + "github": { |
| 56 | + "client_id": "$GITHUB_CLIENT_ID", |
| 57 | + "client_secret": "$GITHUB_CLIENT_SECRET", |
| 58 | + "redirect_uri": "http://localhost:8080/auth/github/callback" |
| 59 | + } |
| 60 | + }, |
| 61 | + ... |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +**Note**: The `$` prefix indicates environment variables. The values will be automatically expanded at runtime. |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +### Starting the Server |
| 70 | + |
| 71 | +```bash |
| 72 | +llms --serve 8000 |
| 73 | +``` |
| 74 | + |
| 75 | +Or if running from the repository: |
| 76 | + |
| 77 | +```bash |
| 78 | +./llms.sh --serve 8000 |
| 79 | +``` |
| 80 | + |
| 81 | +### Signing In |
| 82 | + |
| 83 | +1. Navigate to `http://localhost:8080` |
| 84 | +2. Click "Sign in with GitHub" |
| 85 | +3. Authorize the application on GitHub |
| 86 | +4. You'll be redirected back to the application, now authenticated |
| 87 | + |
| 88 | +### Signing Out |
| 89 | + |
| 90 | +1. Click on your avatar in the top-right corner |
| 91 | +2. A dropdown menu will appear showing your profile info |
| 92 | +3. Click "Sign Out" |
| 93 | + |
| 94 | +### To disable authentication: |
| 95 | + |
| 96 | +Disable authentication in `llms.json`: |
| 97 | + |
| 98 | +```json |
| 99 | +"auth": { |
| 100 | + "enabled": false, |
| 101 | + "github": { |
| 102 | + "client_id": "$GITHUB_CLIENT_ID", |
| 103 | + "client_secret": "$GITHUB_CLIENT_SECRET", |
| 104 | + "redirect_uri": "http://localhost:8080/auth/github/callback" |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Architecture |
| 110 | + |
| 111 | +### Server-Side (Python) |
| 112 | + |
| 113 | +**New Endpoints:** |
| 114 | +- `GET /auth/github` - Initiates GitHub OAuth flow |
| 115 | +- `GET /auth/github/callback` - Handles OAuth callback |
| 116 | +- `GET /auth/session` - Validates session token |
| 117 | +- `POST /auth/logout` - Ends user session |
| 118 | + |
| 119 | +**Session Management:** |
| 120 | +- Sessions stored in-memory (`g_sessions` dictionary) |
| 121 | +- Session tokens are 32-byte URL-safe random strings |
| 122 | +- Sessions expire after 24 hours |
| 123 | +- CSRF protection using state tokens (expire after 10 minutes) |
| 124 | + |
| 125 | +### Client-Side (JavaScript) |
| 126 | + |
| 127 | +**New Components:** |
| 128 | +- `OAuthSignIn.mjs` - OAuth sign-in UI component |
| 129 | +- Updated `Avatar.mjs` - Profile display with logout dropdown |
| 130 | +- Updated `ai.mjs` - OAuth methods and session handling |
| 131 | +- Updated `Main.mjs` - Conditional sign-in component rendering |
| 132 | + |
| 133 | +**Authentication Flow:** |
| 134 | +1. User clicks "Sign in with GitHub" |
| 135 | +2. Redirected to `/auth/github` |
| 136 | +3. Server redirects to GitHub OAuth authorization |
| 137 | +4. User authorizes on GitHub |
| 138 | +5. GitHub redirects to `/auth/github/callback` with code |
| 139 | +6. Server exchanges code for access token |
| 140 | +7. Server fetches user info from GitHub API |
| 141 | +8. Server creates session and redirects to `/?session=TOKEN` |
| 142 | +9. Client validates session and stores user info |
| 143 | +10. User is authenticated |
| 144 | + |
| 145 | +## Security Considerations |
| 146 | + |
| 147 | +### CSRF Protection |
| 148 | +- State tokens are generated for each OAuth flow |
| 149 | +- State tokens are validated on callback |
| 150 | +- Expired state tokens (>10 minutes) are automatically cleaned up |
| 151 | + |
| 152 | +### Session Security |
| 153 | +- Session tokens are cryptographically random (32 bytes) |
| 154 | +- Sessions expire after 24 hours |
| 155 | +- Expired sessions are automatically cleaned up |
| 156 | +- Session tokens are transmitted via URL parameter (initial) and HTTP header (subsequent requests) |
| 157 | + |
| 158 | +### Environment Variables |
| 159 | +- OAuth credentials are stored in environment variables |
| 160 | +- Never commit credentials to version control |
| 161 | +- Use `$VAR_NAME` syntax in config files for automatic expansion |
| 162 | + |
| 163 | +## Troubleshooting |
| 164 | + |
| 165 | +### "GitHub OAuth not configured" error |
| 166 | +- Ensure `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` environment variables are set |
| 167 | +- Restart the server after setting environment variables |
| 168 | + |
| 169 | +### "Invalid state parameter" error |
| 170 | +- This can happen if the OAuth flow takes longer than 10 minutes |
| 171 | +- Try the sign-in process again |
| 172 | + |
| 173 | +### "Invalid or expired session" error |
| 174 | +- Your session may have expired (24-hour limit) |
| 175 | +- Sign in again to create a new session |
| 176 | + |
| 177 | +### Callback URL mismatch |
| 178 | +- Ensure the redirect_uri in `llms.json` matches exactly what you configured in GitHub OAuth App settings |
| 179 | +- Default is `http://localhost:8080/auth/github/callback` |
| 180 | + |
| 181 | +## Production Deployment |
| 182 | + |
| 183 | +For production deployment, update the following: |
| 184 | + |
| 185 | +1. **Change redirect_uri** in `llms.json`: |
| 186 | + ```json |
| 187 | + "redirect_uri": "https://yourdomain.com/auth/github/callback" |
| 188 | + ``` |
| 189 | + |
| 190 | +2. **Update GitHub OAuth App** callback URL to match |
| 191 | + |
| 192 | +3. **Use HTTPS** for secure token transmission |
| 193 | + |
| 194 | +4. **Consider persistent session storage** (Redis, database) instead of in-memory storage |
| 195 | + |
| 196 | +5. **Set appropriate session expiration** based on your security requirements |
| 197 | + |
| 198 | +## Files Modified |
| 199 | + |
| 200 | +- `llms/llms.json` - Added auth configuration |
| 201 | +- `llms/main.py` - Added OAuth endpoints and session management |
| 202 | +- `llms/ui/ai.mjs` - Added OAuth methods and authType config |
| 203 | +- `llms/ui/OAuthSignIn.mjs` - New OAuth sign-in component |
| 204 | +- `llms/ui/Main.mjs` - Conditional sign-in component rendering |
| 205 | +- `llms/ui/Avatar.mjs` - Added logout functionality |
| 206 | + |
| 207 | +## API Reference |
| 208 | + |
| 209 | +### Session Data Structure |
| 210 | + |
| 211 | +```javascript |
| 212 | +{ |
| 213 | + "userId": "12345678", |
| 214 | + "userName": "octocat", |
| 215 | + "displayName": "The Octocat", |
| 216 | + "profileUrl": "https://avatars.githubusercontent.com/u/583231", |
| 217 | + "email": "octocat@github.com", |
| 218 | + "sessionToken": "abc123...", |
| 219 | + "created": 1234567890.123 |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +### HTTP Headers |
| 224 | + |
| 225 | +**For authenticated requests:** |
| 226 | +``` |
| 227 | +X-Session-Token: <session_token> |
| 228 | +``` |
0 commit comments