laravel/airlock
Laravel Sanctum (formerly Airlock) provides a lightweight authentication system for Laravel SPAs and simple APIs. Issue and manage API tokens or use cookie-based session auth for first-party SPAs, with minimal setup and seamless integration.
personal_access_tokens table) and session-based authentication, aligning with modern stateless API design.personal_access_tokens table (migrations provided). Minimal schema changes if using Laravel’s default auth structure.auth:sanctum middleware for API routes. No custom middleware required for basic use.createToken()) and automatic token assignment (e.g., after login).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Token Security | Basic tokens (no encryption by default). Vulnerable if exposed in logs/DB. | Use HTTPS, avoid logging tokens, and consider Laravel Encryption for sensitive data. |
| Token Revocation | Manual revocation required (no built-in API for bulk revocation). | Implement a TokenService to handle revocation or use Laravel’s Tokenable interface. |
| Rate Limiting | No built-in rate limiting for tokens. | Integrate with laravel/rate-limiting or spatie/rate-limiter. |
| Session Management | Session-based auth shares cookies with web routes (potential CSRF risk). | Isolate API and web routes; use sanctum:api middleware explicitly. |
| Performance | Token validation adds minor overhead (~1-2ms per request). | Cache token checks if high throughput is expected (e.g., Redis-based validation). |
| SPA Complexity | SPAs must manage token storage (localStorage/sessionStorage). | Provide frontend SDK or docs for secure token handling (e.g., HTTP-only cookies). |
Authentication Scope:
Token Storage:
Scaling Needs:
Integration with Existing Auth:
Security Hardening:
fetch/axios with Authorization: Bearer <token>.| Step | Action | Tools/Dependencies |
|---|---|---|
| 1. Prerequisites | Ensure Laravel 10.x+ and PHP 8.1+. | Composer, Laravel Installer |
| 2. Installation | composer require laravel/sanctum; publish config (php artisan vendor:publish). |
Sanctum Package |
| 3. Database Setup | Run php artisan sanctum:install to create personal_access_tokens table. |
Laravel Migrations |
| 4. Configuration | Configure config/sanctum.php (e.g., token TTL, guards). |
Laravel Config |
| 5. API Routes | Protect routes with auth:sanctum middleware. |
Laravel Routes |
| 6. Frontend Integration | Implement token retrieval (e.g., login → store token → attach to API requests). | Axios/Fetch, localStorage |
| 7. Testing | Test token generation, validation, and revocation. | PHPUnit, Postman/Paw |
| 8. Deployment | Deploy with HTTPS (tokens must be secure in transit). | Nginx/Apache, Laravel Forge/Envoyer |
HasApiTokens trait for user models.spatie/laravel-permission).Phase 1: Core Integration
Phase 2: Security Hardening
Phase 3: Scaling
Phase 4: Advanced Features
How can I help you explore Laravel packages today?