-
Notifications
You must be signed in to change notification settings - Fork 107
Add PrimaryKeySessionAuthenticator #710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dereuromark
wants to merge
3
commits into
3.next
Choose a base branch
from
3.x-session-auth
base: 3.next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,110 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Authentication\Authenticator; | ||
|
||
use ArrayAccess; | ||
use Authentication\Identifier\IdentifierInterface; | ||
use Cake\Http\Exception\UnauthorizedException; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* Session Authenticator with only ID | ||
*/ | ||
class PrimaryKeySessionAuthenticator extends SessionAuthenticator | ||
{ | ||
/** | ||
* @param \Authentication\Identifier\IdentifierInterface $identifier | ||
* @param array<string, mixed> $config | ||
*/ | ||
public function __construct(IdentifierInterface $identifier, array $config = []) | ||
{ | ||
$config += [ | ||
'identifierKey' => 'key', | ||
'idField' => 'id', | ||
]; | ||
|
||
parent::__construct($identifier, $config); | ||
} | ||
|
||
/** | ||
* Authenticate a user using session data. | ||
* | ||
* @param \Psr\Http\Message\ServerRequestInterface $request The request to authenticate with. | ||
* @return \Authentication\Authenticator\ResultInterface | ||
*/ | ||
public function authenticate(ServerRequestInterface $request): ResultInterface | ||
{ | ||
$sessionKey = $this->getConfig('sessionKey'); | ||
/** @var \Cake\Http\Session $session */ | ||
$session = $request->getAttribute('session'); | ||
|
||
$userId = $session->read($sessionKey); | ||
if (!$userId) { | ||
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND); | ||
} | ||
|
||
$user = $this->_identifier->identify([$this->getConfig('identifierKey') => $userId]); | ||
if (!$user) { | ||
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND); | ||
} | ||
|
||
return new Result($user, Result::SUCCESS); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function persistIdentity(ServerRequestInterface $request, ResponseInterface $response, $identity): array | ||
{ | ||
$sessionKey = $this->getConfig('sessionKey'); | ||
/** @var \Cake\Http\Session $session */ | ||
$session = $request->getAttribute('session'); | ||
|
||
if (!$session->check($sessionKey)) { | ||
$session->renew(); | ||
$session->write($sessionKey, $identity[$this->getConfig('idField')]); | ||
} | ||
|
||
return [ | ||
'request' => $request, | ||
'response' => $response, | ||
]; | ||
} | ||
|
||
/** | ||
* Impersonates a user | ||
* | ||
* @param \Psr\Http\Message\ServerRequestInterface $request The request | ||
* @param \Psr\Http\Message\ResponseInterface $response The response | ||
* @param \ArrayAccess $impersonator User who impersonates | ||
* @param \ArrayAccess $impersonated User impersonated | ||
* @return array | ||
*/ | ||
public function impersonate( | ||
ServerRequestInterface $request, | ||
ResponseInterface $response, | ||
ArrayAccess $impersonator, | ||
ArrayAccess $impersonated, | ||
): array { | ||
$sessionKey = $this->getConfig('sessionKey'); | ||
$impersonateSessionKey = $this->getConfig('impersonateSessionKey'); | ||
/** @var \Cake\Http\Session $session */ | ||
$session = $request->getAttribute('session'); | ||
if ($session->check($impersonateSessionKey)) { | ||
throw new UnauthorizedException( | ||
'You are impersonating a user already. ' . | ||
'Stop the current impersonation before impersonating another user.', | ||
); | ||
} | ||
$session->write($impersonateSessionKey, $impersonator[$this->getConfig('idField')]); | ||
$session->write($sessionKey, $impersonated[$this->getConfig('idField')]); | ||
$this->setConfig('identify', true); | ||
|
||
return [ | ||
'request' => $request, | ||
'response' => $response, | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're inheriting from
SessionAuthenticator
how doesidentifierKey
interact withfields
?What is
key
here? I don't see that as a field in theauth_users
table that your tests use. Is it primarily to be aligned with the identifier configuration?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key here maps to the dataField of the identifier, this is how they pass in the data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fields is unused afaik.