-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserStorage.php
More file actions
135 lines (112 loc) · 3.48 KB
/
UserStorage.php
File metadata and controls
135 lines (112 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace App\Security;
use App\Exceptions\InvalidAccessTokenException;
use App\Model\Entity\User;
use Nette;
use Nette\Security\IIdentity;
use InvalidArgumentException;
class UserStorage implements Nette\Security\UserStorage
{
use Nette\SmartObject;
private const AUTH_HEADER = "Authorization";
/** @var Nette\Http\IRequest */
private $httpRequest;
/** @var AccessManager */
private $accessManager;
private $authenticated;
/** @var ?IIdentity */
private $cachedIdentity;
public function __construct(AccessManager $accessManager, Nette\Http\IRequest $httpRequest)
{
$this->httpRequest = $httpRequest;
$this->accessManager = $accessManager;
}
/**
* @inheritDoc
*/
public function setExpiration(?string $expire, bool $clearIdentity): void
{
// no-op - doesn't really make sense with this implementation
}
/**
* @inheritDoc
*/
public function saveAuthentication(?IIdentity $identity): void
{
if ($identity !== null && !($identity instanceof Identity)) {
throw new InvalidArgumentException("Wrong identity class");
}
$this->authenticated = true;
$this->cachedIdentity = $identity;
}
/**
* @inheritDoc
*/
public function clearAuthentication(bool $clearIdentity): void
{
$this->authenticated = false;
if ($clearIdentity) {
$this->cachedIdentity = null;
}
}
/**
* @inheritDoc
*/
public function getState(): array
{
return [$this->isAuthenticated(), $this->getIdentity(), 0];
}
/**
* Is this user authenticated?
* @return bool
*/
public function isAuthenticated(): bool
{
return $this->httpRequest->getHeader(self::AUTH_HEADER) !== null || $this->authenticated;
}
/**
* Returns current user identity, if any.
* @return IIdentity|null
*/
public function getIdentity(): ?IIdentity
{
if ($this->cachedIdentity === null) {
$token = $this->accessManager->getGivenAccessToken($this->httpRequest);
if ($token === null) {
$this->cachedIdentity = new Identity(null, null);
return $this->cachedIdentity;
}
$decodedToken = $this->accessManager->decodeToken($token);
$user = $this->accessManager->getUser($decodedToken);
$this->checkTokenForRevocation($decodedToken, $user);
$this->cachedIdentity = new Identity($user, $decodedToken);
}
return $this->cachedIdentity;
}
/**
* Returns current user entity from user identity, if any.
* @return ?\App\Model\Entity\User
*/
public function getUserData()
{
$identity = $this->getIdentity();
if ($identity instanceof Identity) {
return $identity->getUserData();
} else {
return null;
}
}
/**
* @throws InvalidAccessTokenException
* @throws InvalidArgumentException
*/
protected function checkTokenForRevocation(AccessToken $token, User $user)
{
$validityThreshold = $user->getTokenValidityThreshold();
$wasTokenIssuedAfterThreshold = $validityThreshold === null
|| $token->getIssuedAt() >= $validityThreshold->getTimestamp();
if (!$wasTokenIssuedAfterThreshold) {
throw new InvalidAccessTokenException("Your access token was revoked and cannot be used anymore");
}
}
}