|
| 1 | +# Authentication Quick Reference Card |
| 2 | + |
| 3 | +**Last Updated:** October 22, 2025 |
| 4 | +**Full Guide:** [AUTHENTICATION.md](AUTHENTICATION.md) |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Config Values (MUST BE EXACT!) |
| 9 | + |
| 10 | +```php |
| 11 | +// In config/api.php |
| 12 | + |
| 13 | +'auth_method' => 'apikey', // ✅ Correct (NOT 'api_key') |
| 14 | +'auth_method' => 'basic', // ✅ Correct |
| 15 | +'auth_method' => 'jwt', // ✅ Correct |
| 16 | +'auth_method' => 'oauth', // ✅ Correct (placeholder) |
| 17 | +``` |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## API Key Authentication |
| 22 | + |
| 23 | +**Config:** |
| 24 | +```php |
| 25 | +'auth_method' => 'apikey', |
| 26 | +'api_keys' => ['changeme123'], |
| 27 | +'api_key_role' => 'admin', // Role for all API key users |
| 28 | +``` |
| 29 | + |
| 30 | +**Usage:** |
| 31 | +```bash |
| 32 | +# Header (recommended) |
| 33 | +curl -H "X-API-Key: changeme123" http://localhost/api.php?action=tables |
| 34 | + |
| 35 | +# Query parameter |
| 36 | +curl "http://localhost/api.php?action=tables&api_key=changeme123" |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Basic Authentication |
| 42 | + |
| 43 | +**Config:** |
| 44 | +```php |
| 45 | +'auth_method' => 'basic', |
| 46 | +'basic_users' => [ |
| 47 | + 'admin' => 'secret', |
| 48 | +], |
| 49 | +'user_roles' => [ |
| 50 | + 'admin' => 'admin', |
| 51 | +], |
| 52 | +'use_database_auth' => true, // Check database too |
| 53 | +``` |
| 54 | + |
| 55 | +**Usage:** |
| 56 | +```bash |
| 57 | +# cURL |
| 58 | +curl -u admin:secret http://localhost/api.php?action=tables |
| 59 | + |
| 60 | +# JavaScript |
| 61 | +const credentials = btoa('admin:secret'); |
| 62 | +fetch('/api.php?action=tables', { |
| 63 | + headers: { 'Authorization': 'Basic ' + credentials } |
| 64 | +}); |
| 65 | +``` |
| 66 | +
|
| 67 | +**Create Database User:** |
| 68 | +```bash |
| 69 | +php scripts/create_user.php john john@email.com SecurePass123! readonly |
| 70 | +``` |
| 71 | +
|
| 72 | +--- |
| 73 | +
|
| 74 | +## JWT Authentication |
| 75 | +
|
| 76 | +**Config:** |
| 77 | +```php |
| 78 | +'auth_method' => 'jwt', |
| 79 | +'jwt_secret' => 'a7f92c8e4b6d1f3a9e8c7b5d2f1a6e9b...', // Change this! |
| 80 | +'jwt_expiration' => 3600, // 1 hour |
| 81 | +'use_database_auth' => true, |
| 82 | +``` |
| 83 | +
|
| 84 | +**Step 1 - Login:** |
| 85 | +```bash |
| 86 | +curl -X POST \ |
| 87 | + -d "username=john&password=SecurePass123!" \ |
| 88 | + http://localhost/api.php?action=login |
| 89 | + |
| 90 | +# Response: |
| 91 | +# {"success":true,"token":"eyJ0eXAi...","expires_in":3600,"user":"john","role":"readonly"} |
| 92 | +``` |
| 93 | +
|
| 94 | +**Step 2 - Use Token:** |
| 95 | +```bash |
| 96 | +curl -H "Authorization: Bearer eyJ0eXAi..." \ |
| 97 | + http://localhost/api.php?action=tables |
| 98 | +``` |
| 99 | +
|
| 100 | +**JavaScript Example:** |
| 101 | +```javascript |
| 102 | +// Login |
| 103 | +const loginRes = await fetch('/api.php?action=login', { |
| 104 | + method: 'POST', |
| 105 | + body: new URLSearchParams({ |
| 106 | + username: 'john', |
| 107 | + password: 'SecurePass123!' |
| 108 | + }) |
| 109 | +}); |
| 110 | +const { token } = await loginRes.json(); |
| 111 | + |
| 112 | +// Use token |
| 113 | +const dataRes = await fetch('/api.php?action=tables', { |
| 114 | + headers: { 'Authorization': 'Bearer ' + token } |
| 115 | +}); |
| 116 | +const data = await dataRes.json(); |
| 117 | +``` |
| 118 | +
|
| 119 | +--- |
| 120 | +
|
| 121 | +## RBAC Roles |
| 122 | +
|
| 123 | +**Predefined Roles:** |
| 124 | +
|
| 125 | +| Role | Tables | Actions | System Tables | |
| 126 | +|------|--------|---------|---------------| |
| 127 | +| `admin` | All (`*`) | All | ✅ Can access | |
| 128 | +| `readonly` | All (`*`) | list, read | ❌ Blocked | |
| 129 | +| `editor` | All (`*`) | All | ❌ Blocked | |
| 130 | +| `users_manager` | users, orders | Specific | ❌ No access | |
| 131 | +
|
| 132 | +**Config:** |
| 133 | +```php |
| 134 | +'roles' => [ |
| 135 | + 'admin' => [ |
| 136 | + '*' => ['list', 'read', 'create', 'update', 'delete'] |
| 137 | + ], |
| 138 | + 'readonly' => [ |
| 139 | + '*' => ['list', 'read'], |
| 140 | + 'api_users' => [], // Empty array = DENY |
| 141 | + 'api_key_usage' => [], |
| 142 | + ], |
| 143 | +], |
| 144 | +``` |
| 145 | +
|
| 146 | +**Actions:** |
| 147 | +- `list` - View list |
| 148 | +- `read` - View single record |
| 149 | +- `create` - Insert |
| 150 | +- `update` - Modify |
| 151 | +- `delete` - Remove |
| 152 | +
|
| 153 | +--- |
| 154 | +
|
| 155 | +## Role Assignment by Auth Method |
| 156 | +
|
| 157 | +| Auth Method | Role Source | |
| 158 | +|-------------|-------------| |
| 159 | +| **apikey** | `api_key_role` in config | |
| 160 | +| **basic** (config users) | `user_roles` mapping | |
| 161 | +| **basic** (DB users) | `api_users.role` column | |
| 162 | +| **jwt** | `role` claim in token | |
| 163 | +
|
| 164 | +--- |
| 165 | +
|
| 166 | +## Common Issues |
| 167 | +
|
| 168 | +### "401 Unauthorized" |
| 169 | +- Check `auth_method` matches your usage |
| 170 | +- Verify credentials/token |
| 171 | +- Ensure `auth_enabled = true` |
| 172 | +
|
| 173 | +### "403 Forbidden: No role assigned" |
| 174 | +- API Key: Add `'api_key_role' => 'admin'` to config |
| 175 | +- Basic Auth: Add user to `user_roles` mapping or check DB role |
| 176 | +- JWT: Role should be in token claims |
| 177 | +
|
| 178 | +### "403 Forbidden" (with role) |
| 179 | +- Check RBAC permissions for your role |
| 180 | +- System tables blocked for non-admin roles |
| 181 | +
|
| 182 | +### API Key doesn't work |
| 183 | +- Use `'apikey'` NOT `'api_key'` (no underscore!) |
| 184 | +
|
| 185 | +--- |
| 186 | +
|
| 187 | +## Performance Comparison |
| 188 | +
|
| 189 | +| Method | DB Queries per Request | Best For | |
| 190 | +|--------|------------------------|----------| |
| 191 | +| API Key | 0 | Webhooks | |
| 192 | +| Basic (config) | 0 | Development | |
| 193 | +| Basic (DB) | 1 | Small apps | |
| 194 | +| JWT | 0 | Production | |
| 195 | +
|
| 196 | +**JWT Performance:** |
| 197 | +- Before: 600,000 auth queries/hour |
| 198 | +- After: 1,000 auth queries/hour |
| 199 | +- **Reduction: 99.8%** 🚀 |
| 200 | +
|
| 201 | +--- |
| 202 | +
|
| 203 | +## Security Checklist |
| 204 | +
|
| 205 | +- [ ] Use HTTPS in production |
| 206 | +- [ ] Change `jwt_secret` to random 64+ char string |
| 207 | +- [ ] Rotate API keys every 90 days |
| 208 | +- [ ] Use strong passwords (8+ chars, mixed case, numbers, symbols) |
| 209 | +- [ ] Enable rate limiting (`'rate_limit' => ['enabled' => true]`) |
| 210 | +- [ ] Monitor authentication failures (dashboard) |
| 211 | +- [ ] Set appropriate JWT expiration (1-24 hours) |
| 212 | +- [ ] Block system tables for non-admin roles |
| 213 | +- [ ] Use database users (not config file) for production |
| 214 | +
|
| 215 | +--- |
| 216 | +
|
| 217 | +## Quick Commands |
| 218 | +
|
| 219 | +```bash |
| 220 | +# Generate JWT secret |
| 221 | +php -r "echo bin2hex(random_bytes(32));" |
| 222 | + |
| 223 | +# Create database user |
| 224 | +php scripts/create_user.php <username> <email> <password> <role> |
| 225 | + |
| 226 | +# Test authentication |
| 227 | +curl -H "X-API-Key: changeme123" http://localhost/api.php?action=tables |
| 228 | + |
| 229 | +# View monitoring dashboard |
| 230 | +# http://localhost/PHP-CRUD-API-Generator/dashboard.html |
| 231 | +``` |
| 232 | +
|
| 233 | +--- |
| 234 | +
|
| 235 | +## Documentation Links |
| 236 | +
|
| 237 | +- **[AUTHENTICATION.md](AUTHENTICATION.md)** - Complete guide (50+ pages) |
| 238 | +- **[USER_MANAGEMENT.md](USER_MANAGEMENT.md)** - User management system |
| 239 | +- **[QUICK_START_USERS.md](QUICK_START_USERS.md)** - 5-minute setup |
| 240 | +- **[SECURITY_RBAC_TESTS.md](SECURITY_RBAC_TESTS.md)** - RBAC testing |
| 241 | +- **[PERFORMANCE_AUTHENTICATION.md](PERFORMANCE_AUTHENTICATION.md)** - Performance optimization |
| 242 | +
|
| 243 | +--- |
| 244 | +
|
| 245 | +**Need help?** Read the full guide: [docs/AUTHENTICATION.md](AUTHENTICATION.md) |
0 commit comments