Skip to content

Commit

Permalink
Add OneTimetokenAuthenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
ajibarra committed Jan 31, 2025
1 parent 0f8efc8 commit ab070c5
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Authenticator/OneTimeTokenAuthenticator.php
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

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

RiskyTruthyFalsyComparison

src/Authenticator/OneTimeTokenAuthenticator.php:42:13: RiskyTruthyFalsyComparison: Operand of type mixed|null contains type mixed, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
}

$usersTable = TableRegistry::getTableLocator()->get(Configure::read('Users.table'));

$user = $usersTable->loginWithToken($name, $token);

Check failure on line 48 in src/Authenticator/OneTimeTokenAuthenticator.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method Cake\ORM\Table::loginWithToken().

if (!$user) {
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
}

return new Result($user, Result::SUCCESS);
}
}

0 comments on commit ab070c5

Please sign in to comment.