1 | <?php declare(strict_types = 1); |
||
19 | final class JwtToPsrMapper implements JwtToPsrMapperInterface |
||
20 | { |
||
21 | /** @var Signer */ |
||
22 | private $signer; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $signatureKey; |
||
26 | |||
27 | /** @var string */ |
||
28 | private $verificationKey; |
||
29 | |||
30 | /** @var int */ |
||
31 | private $expirationTime; |
||
32 | |||
33 | /** @var int */ |
||
34 | private $refreshTime; |
||
35 | |||
36 | /** @var Parser */ |
||
37 | private $tokenParser; |
||
38 | |||
39 | /** @var SetCookie */ |
||
40 | private $defaultCookie; |
||
41 | |||
42 | 17 | public function __construct( |
|
60 | |||
61 | 1 | public static function fromAsymmetricKeyDefaults( |
|
79 | |||
80 | /** @return ?Token */ |
||
81 | 4 | public function parseToken(ServerRequestInterface $request) |
|
102 | |||
103 | 8 | public function appendToken( |
|
104 | SessionInterface $session, |
||
105 | ResponseInterface $response, |
||
106 | Token $token = null |
||
107 | ) : ResponseInterface |
||
108 | { |
||
109 | 8 | $sessionContainerChanged = $session->hasChanged(); |
|
110 | |||
111 | 8 | if ($sessionContainerChanged && $session->isEmpty()) { |
|
112 | 2 | return FigResponseCookies::set($response, $this->getExpirationCookie()); |
|
113 | } |
||
114 | |||
115 | 6 | if ($sessionContainerChanged || ($this->shouldTokenBeRefreshed($token) && !$session->isEmpty())) { |
|
116 | 3 | return FigResponseCookies::set($response, $this->getTokenCookie($session)); |
|
117 | } |
||
118 | |||
119 | 3 | return $response; |
|
120 | } |
||
121 | |||
122 | 2 | private function getExpirationCookie() : SetCookie |
|
123 | { |
||
124 | 2 | $expirationDate = new \DateTime('-30 days'); |
|
125 | return $this |
||
126 | 2 | ->defaultCookie |
|
127 | 2 | ->withValue(null) |
|
128 | 2 | ->withExpires($expirationDate->getTimestamp()); |
|
129 | } |
||
130 | |||
131 | 4 | private function shouldTokenBeRefreshed(Token $token = null) : bool |
|
132 | { |
||
133 | 4 | if (is_null($token)) { |
|
134 | 2 | return false; |
|
135 | } |
||
136 | |||
137 | 2 | if (!$token->hasClaim(self::ISSUED_AT_CLAIM)) { |
|
138 | return false; |
||
139 | } |
||
140 | |||
141 | 2 | return time() >= ($token->getClaim(self::ISSUED_AT_CLAIM) + $this->refreshTime); |
|
142 | } |
||
143 | |||
144 | 3 | private function getTokenCookie(SessionInterface $session) : SetCookie |
|
145 | { |
||
146 | 3 | $timestamp = time(); |
|
147 | return $this |
||
148 | 3 | ->defaultCookie |
|
149 | 3 | ->withValue( |
|
150 | 3 | (new Builder()) |
|
151 | 3 | ->setIssuedAt($timestamp) |
|
152 | 3 | ->setExpiration($timestamp + $this->expirationTime) |
|
153 | 3 | ->set(self::SESSION_CLAIM, $session->toArray()) |
|
154 | 3 | ->sign($this->signer, $this->signatureKey) |
|
155 | 3 | ->getToken() |
|
156 | ) |
||
157 | 3 | ->withExpires($timestamp + $this->expirationTime); |
|
158 | } |
||
159 | |||
160 | 4 | public function extractSessionContainer(Token $token = null) : SessionInterface |
|
175 | } |
||
176 |