Download the PHP package agednerd/laravel-masquerade without Composer
On this page you can find all versions of the php package agednerd/laravel-masquerade. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download agednerd/laravel-masquerade
More information about agednerd/laravel-masquerade
Files in agednerd/laravel-masquerade
Package laravel-masquerade
Short Description Securely assume another user's identity in modern Laravel applications.
License MIT
Homepage https://github.com/agednerd/laravel-masquerade
Informations about the package laravel-masquerade
Laravel Masquerade
Securely assume another user's identity.
Laravel Masquerade provides secure, nested, remembered, and multi-guard user switching for Laravel 13. It uses Laravel's normal session guards and does not replace the framework's authentication driver.
Features
- Deny-by-default authorization with checks on both users.
- POST and DELETE routes protected by Laravel's
webmiddleware and CSRF handling. - Nested masquerades that unwind one level at a time.
- Same-guard and cross-guard switching with restoration of the source guard.
- Optional remember-me behavior with an encrypted, HTTP-only stack cookie.
- Octane-safe request-scoped services.
- Safe relative or named-route redirects; external redirects are disabled by default.
- Blade conditions, helper functions, lifecycle events, and sensitive-route middleware.
Requirements
- PHP 8.3 or newer.
- Laravel 13.
- At least one session-backed guard implementing
StatefulGuard.
Sanctum SPA authentication is supported through its underlying web guard. Personal access token masquerades are not synthesized: bearer-token exchange, revocation, and audit behavior should be implemented by an application-specific token broker.
Installation
Install the package after its first stable release is published:
Laravel discovers the service provider and Masquerade facade automatically. Publishing the configuration is optional:
Before a stable tag exists, the development branch can be installed explicitly:
Model setup and authorization
Add Masqueradable to every authenticatable model that can initiate or become the subject of a masquerade:
canMasquerade() returns false by default. canBeMasqueraded() returns true by default. The built-in controller and masqueradeAs() require both checks to pass. Keep authorization decisions on the server; never rely on hiding a button.
Registering routes
Register the route macro in routes/web.php. The web middleware is required for sessions, encrypted cookies, and CSRF protection:
This registers:
| Method | URI | Name | Purpose |
|---|---|---|---|
POST |
/masquerade/{id}/{guardName?} |
masquerade.take |
Start a masquerade |
DELETE |
/masquerade |
masquerade.leave |
Leave one level |
id must be the value returned by the subject's getAuthIdentifier(). It is not implicit route-model binding.
Starting and leaving
For another target guard, pass its name as the second route parameter:
The optional request fields are:
remember: a boolean override for remembered login behavior.redirect_to: a relative path or route name used after the transition.
Model API
The model API evaluates both authorization hooks:
Nested calls add frames to the stack. leaveMasquerade() unwinds only the latest frame.
Manager and facade
Resolve the request-scoped manager when you need stack or guard details:
The auto-discovered facade proxies the same manager:
MasqueradeManager::take() is a low-level transition primitive and does not evaluate model authorization hooks. Prefer $actor->masqueradeAs($subject) or the built-in controller for user-driven actions.
Configuration
The published config/masquerade.php contains:
| Key | Default | Meaning |
|---|---|---|
session_key |
masquerade.stack |
Session key containing the nested stack |
cookie_key |
masquerade_stack |
Encrypted stack-cookie name |
default_guard |
web |
Default subject guard |
remember |
inherit |
false, true, or inherit from a remembered source |
remember_cookie_minutes |
43200 |
Stack-cookie lifetime in minutes |
take_redirect_to |
/ |
Default redirect after starting |
leave_redirect_to |
/ |
Default redirect after leaving |
allow_external_redirects |
false |
Whether absolute external redirect URLs are accepted |
legacy_get_routes |
false |
Enables legacy state-changing GET routes |
Keep legacy_get_routes disabled. GET requests should not change authentication state.
Redirects
Redirect values may be relative paths, back, or Laravel route names. Invalid route names and disallowed external URLs safely fall back to /.
Request-specific resolver callbacks can be installed on the current manager instance:
Remembered and nested masquerades
With remember set to inherit, the subject receives a normal Laravel recaller when the source was restored via remember-me or has a remember token. The nested stack is also queued in an encrypted, HTTP-only, SameSite=Lax cookie.
Cookie recovery occurs only when Laravel's recaller restores the same subject represented by the top stack frame. Clearing or leaving the final frame removes both session and cookie state.
Blade conditions and helpers
Equivalent helpers are available:
Events and audit logging
MasqueradeStarted and MasqueradeEnded expose:
$masquerader$subject$sourceGuard$targetGuard$depthafter the transition
Example listener:
Protecting sensitive routes
Apply masquerade.protect to billing, credentials, destructive operations, or other sensitive routes. It returns HTTP 403 while a masquerade is active:
Recommended safeguards:
- Audit both lifecycle events.
- Protect password, MFA, billing, API-token, and destructive routes.
- Keep external redirects and legacy GET routes disabled.
- Use short session lifetimes for privileged operators.
- Apply rate limiting and normal administrative authorization to the take route.
Local development
Use a Composer path repository before the package is available on Packagist:
Then run:
Testing
The CI matrix tests PHP 8.3, 8.4, and 8.5 against the lowest and current stable Laravel 13 dependency sets.
Maintainer release checklist
- Update
CHANGELOG.md, runcomposer validate --strict, and run the test suite. - Commit and push the release-ready source.
-
Create and push a semantic version tag, for example:
- The
ReleaseGitHub Actions workflow validates the full PHP/dependency matrix, confirms that the changelog contains a dated heading matching the tag, and creates the GitHub Release with generated notes. Av1.0.0tag therefore requires a heading such as## 1.0.0 - 2026-06-28. - Submit
https://github.com/agednerd/laravel-masqueradeat Packagist. - Connect Packagist to GitHub or configure its webhook so pushes and new tags are synchronized automatically.
- Verify the release:
Do not add a version field to composer.json; Composer derives release versions from Git tags.
License
Laravel Masquerade is open-source software licensed under the MIT license.
All versions of laravel-masquerade with dependencies
illuminate/auth Version ^13.0
illuminate/contracts Version ^13.0
illuminate/http Version ^13.0
illuminate/routing Version ^13.0
illuminate/session Version ^13.0
illuminate/support Version ^13.0
illuminate/view Version ^13.0