-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef11bd7
commit fc302a6
Showing
2 changed files
with
89 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
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,88 @@ | ||
<?php | ||
|
||
namespace Illuminate\Session\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Auth\AuthenticationException; | ||
use Illuminate\Contracts\Auth\Factory as AuthFactory; | ||
|
||
class AuthenticateSession | ||
{ | ||
/** | ||
* The authentication factory implementation. | ||
* | ||
* @var \Illuminate\Contracts\Auth\Factory | ||
*/ | ||
protected $auth; | ||
|
||
/** | ||
* Create a new middleware instance. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Factory $auth | ||
* @return void | ||
*/ | ||
public function __construct(AuthFactory $auth) | ||
{ | ||
$this->auth = $auth; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if (! $request->user() || ! $request->session()) { | ||
return $next($request); | ||
} | ||
|
||
if (! $request->session()->has('password_hash') && $this->auth->viaRemember()) { | ||
$this->logout($request); | ||
} | ||
|
||
if (! $request->session()->has('password_hash')) { | ||
$this->storePasswordHashInSession($request); | ||
} | ||
|
||
if ($request->session()->get('password_hash') !== $request->user()->password) { | ||
$this->logout($request); | ||
} | ||
|
||
return tap($next($request), function () use ($request) { | ||
$this->storePasswordHashInSession($request); | ||
}); | ||
} | ||
|
||
/** | ||
* Store the user's current password hash in the session. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return void | ||
*/ | ||
protected function storePasswordHashInSession($request) | ||
{ | ||
$request->session()->put([ | ||
'password_hash' => $request->user()->password, | ||
]); | ||
} | ||
|
||
/** | ||
* Log the user out of the application. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return void | ||
* | ||
* @throws \Illuminate\Auth\AuthenticationException | ||
*/ | ||
protected function logout($request) | ||
{ | ||
$this->auth->logout(); | ||
|
||
$request->session()->flush(); | ||
|
||
throw new AuthenticationException; | ||
} | ||
} |