Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Airlock Laravel Package

laravel/airlock

Laravel Sanctum (formerly Airlock) offers lightweight authentication for Laravel SPAs and simple APIs. Use cookie-based session auth for first-party SPAs or issue API tokens for mobile apps and third-party clients, with minimal setup and seamless Laravel integration.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Lightweight & Purpose-Built: Laravel Sanctum is explicitly designed for SPAs (Single-Page Applications) and simple APIs, aligning perfectly with modern Laravel-based microservices, mobile backends, or decoupled frontend architectures.
  • Token-Based Auth: Leverages stateless HTTP tokens (via personal_access_tokens table) instead of sessions, reducing server-side storage overhead and enabling seamless cross-domain/authentication flows.
  • Laravel Ecosystem Integration: Built for Laravel’s authentication contract system, enabling seamless integration with Laravel’s built-in auth (e.g., Auth::user(), HasApiTokens trait) and middleware (auth:sanctum).
  • Stateless vs. Stateful: Supports both stateless (API tokens) and stateful (CSRF-protected cookies) modes, allowing flexibility for different use cases (e.g., mobile apps vs. web SPAs).

Integration Feasibility

  • Minimal Boilerplate: Installation via Composer (laravel/sanctum) + php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" + migrations (php artisan migrate).
  • Database Requirements: Adds a single personal_access_tokens table (or uses existing oauth_personal_access_tokens in Laravel 10+). No complex schema changes.
  • Middleware & Routes: Auto-registers auth:sanctum middleware and /sanctum/csrf-cookie, /sanctum/token endpoints. Can be customized via config (config/sanctum.php).
  • Customization Points:
    • Token generation (createToken() methods on models).
    • Token retrieval (getAccessTokenFromRequestUsing).
    • Guard configuration (multiple guards supported).
    • Token expiration and revocation logic.

Technical Risk

Risk Area Assessment Mitigation
Laravel Version Lock Tight coupling with Laravel (v11+ in v4.x). Downgrading/upgrading Laravel may require Sanctum updates. Monitor Laravel/Sanctum compatibility matrix. Use feature flags or modularize auth if version flexibility is critical.
Token Security Tokens are not encrypted by default (stored as plaintext in DB). Vulnerable to DB leaks if not using HTTPS or additional encryption (e.g., Laravel Encryption). Enable stateful mode for CSRF protection, use HTTPS, and consider encrypting tokens at rest (e.g., via Laravel’s encrypt()).
Performance Token lookup uses where('tokenable_id', $id)->where('token', $token) by default. Scaling to millions of tokens may require indexing (already added in v4.2.0). Ensure tokenable_id and token columns are indexed. For high-scale APIs, consider sharding tokens or using a dedicated cache (Redis) for token validation.
Stateful Mode Complexity Stateful mode (cookies) introduces CSRF and session-like behavior, which may conflict with stateless APIs or CDNs. Use stateless mode for APIs; reserve stateful for SPAs. Configure stateful domains explicitly in config/sanctum.php.
Token Revocation Manual revocation requires deleting tokens from DB. No built-in TTL for stateless tokens (unlike OAuth). Implement a token_pruner job (via Laravel Queues) to expire tokens after inactivity (e.g., last_used_at tracking in v4.3.0).
Custom User Providers Sanctum assumes Laravel’s default User model. Custom providers (e.g., API keys) require manual guard configuration. Extend Sanctum::guard() or create a custom guard implementation.

Key Questions for TPM

  1. Use Case Clarity:
    • Is Sanctum replacing Laravel Passport (OAuth2) or API Tokens (e.g., for internal services)?
    • Will it handle both SPAs and mobile apps (stateful/stateless) or just one?
  2. Scaling Needs:
    • What’s the expected token volume (e.g., 10K vs. 10M tokens)? Are indexes sufficient, or is Redis caching needed?
    • Are token revocation or expiration critical (e.g., for compliance)?
  3. Security Requirements:
    • Are tokens PII-sensitive? If so, is encryption at rest required?
    • Should tokens be scoped (e.g., role-based access)?
  4. Integration Complexity:
    • Does the existing system use custom auth guards or non-Laravel User models?
    • Are there legacy systems relying on sessions that Sanctum’s stateless approach might disrupt?
  5. Operational Overhead:
    • Who manages token rotation (e.g., for compromised tokens)?
    • Is there a backup plan if Sanctum’s DB table becomes a bottleneck?

Integration Approach

Stack Fit

  • Laravel-Centric: Ideal for Laravel 11+ applications using PHP 8.4+. Leverages Laravel’s:
    • Authentication contracts (Illuminate\Contracts\Auth\Authenticatable).
    • Middleware pipeline (auth:sanctum).
    • Eloquent models (HasApiTokens trait).
  • Frontend Compatibility:
    • SPAs: Works with React, Vue, Next.js (stateful mode).
    • Mobile: Stateless tokens for iOS/Android apps.
    • Third-Party APIs: Stateless tokens for internal services.
  • Database Support: MySQL, PostgreSQL, SQLite (via Laravel migrations).
  • Caching: Optional Redis integration for token validation (customizable via getAccessTokenFromRequestUsing).

Migration Path

Step Action Dependencies
1. Assessment Audit existing auth (e.g., sessions, API keys, Passport). Identify endpoints/models needing Sanctum. None.
2. Setup Install Sanctum: composer require laravel/sanctum. Publish config/migrations: php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider". Run migrations. Laravel 11+, PHP 8.4+.
3. Model Integration Add HasApiTokens trait to user models. Configure token namespaces (e.g., createToken('mobile-app')). Eloquent models.
4. Route Protection Apply auth:sanctum middleware to API routes. For SPAs, enable stateful mode in config/sanctum.php. Laravel routing.
5. Frontend Setup Configure frontend to send tokens (stateless: Authorization: Bearer <token>; stateful: cookies). SPA framework (e.g., Axios for React).
6. Testing Test token generation, revocation, and scope-based access. Validate CSRF protection for stateful requests. Postman/Newman or custom test suites.
7. Monitoring Set up logging for token usage (e.g., last_used_at). Monitor failed auth attempts. Laravel Horizon or custom logs.
8. Rollback Plan Document steps to revert to legacy auth if issues arise (e.g., token migration failures). Backup DB schema.

Compatibility

  • Laravel Versions: Officially supports 11+ (v4.x). Backward compatibility for 10+ in v3.x.
  • PHP Versions: 8.4+ (v4.x). PHP 8.1+ for v3.x.
  • Database: Works with Laravel-supported DBs (MySQL, PostgreSQL, SQLite). No schema changes if using oauth_personal_access_tokens.
  • Middleware: Plays well with Laravel’s api middleware group. Can coexist with Passport if needed.
  • Caching: No native Redis support, but can be extended via getAccessTokenFromRequestUsing.

Sequencing

  1. Phase 1: Pilot (Non-Prod)
    • Integrate Sanctum in a staging environment for a single SPA or API.
    • Test token generation, revocation, and frontend flows.
  2. Phase 2: Gradual Rollout
    • Migrate one service/API at a time, using feature flags to toggle Sanctum.
    • Monitor token performance and auth failures.
  3. Phase 3: Full Cutover
    • Deprecate legacy auth (e.g., sessions/API keys) post-validation.
    • Update documentation and frontend SDKs to use Sanctum tokens.

Operational Impact

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony