-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure device has not been logged out
This adds a middleware to check that the password has in session is the same as the current users password. This fixes a security issue where an attacker can keep sending requests to an API using the sanctum auth after the password has been changed.
- Loading branch information
1 parent
7f4d571
commit 9392ec9
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Laravel\Sanctum\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Auth\AuthenticationException; | ||
use Illuminate\Contracts\Auth\Factory as AuthFactory; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class EnsureDeviceHasNotBeenLoggedOut | ||
{ | ||
public function __construct(protected AuthFactory $auth) | ||
{ | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
if (! $request->hasSession() || ! $request->user()) { | ||
return $next($request); | ||
} | ||
|
||
if ($request->session()->get('password_hash_'.$this->auth->getDefaultDriver()) !== $request->user()->getAuthPassword()) { | ||
$this->logout($request); | ||
|
||
throw new AuthenticationException('Unauthenticated.', [$this->auth->getDefaultDriver()]); | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
protected function logout(Request $request) | ||
{ | ||
$this->auth->logoutCurrentDevice(); | ||
|
||
$request->session()->flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters