-
Notifications
You must be signed in to change notification settings - Fork 31
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
Showing
1 changed file
with
56 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,56 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace CakeDC\Auth\Authenticator; | ||
|
||
use Authentication\Authenticator\AuthenticatorInterface; | ||
use Authentication\Authenticator\Result; | ||
use Authentication\Authenticator\ResultInterface; | ||
use Cake\Core\Configure; | ||
use Cake\Core\InstanceConfigTrait; | ||
use Cake\ORM\TableRegistry; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class OneTimeTokenAuthenticator implements AuthenticatorInterface | ||
{ | ||
use InstanceConfigTrait; | ||
|
||
/** | ||
* Settings for base authenticator | ||
* | ||
* @var array | ||
*/ | ||
protected $_defaultConfig = [ | ||
]; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function authenticate(ServerRequestInterface $request): ResultInterface | ||
{ | ||
/** @var \Cake\Http\ServerRequest $request */ | ||
$token = $request->getQuery('token') ?: $request->getData('token'); | ||
if (is_array($token)) { | ||
$token = join($token); | ||
} | ||
|
||
if (!$token) { | ||
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING); | ||
} | ||
|
||
$name = $request->getSession()->read('OneTimeLogin.name'); | ||
if (!$name) { | ||
Check failure on line 42 in src/Authenticator/OneTimeTokenAuthenticator.php
|
||
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING); | ||
} | ||
|
||
$usersTable = TableRegistry::getTableLocator()->get(Configure::read('Users.table')); | ||
|
||
$user = $usersTable->loginWithToken($name, $token); | ||
|
||
if (!$user) { | ||
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING); | ||
} | ||
|
||
return new Result($user, Result::SUCCESS); | ||
} | ||
} |