1 | <?php |
||
39 | final class SessionMiddleware implements MiddlewareInterface |
||
40 | { |
||
41 | const ISSUED_AT_CLAIM = 'iat'; |
||
42 | const SESSION_CLAIM = 'session-data'; |
||
43 | const SESSION_ATTRIBUTE = 'session'; |
||
44 | const DEFAULT_COOKIE = 'slsession'; |
||
45 | const DEFAULT_REFRESH_TIME = 60; |
||
46 | |||
47 | /** |
||
48 | * @var Signer |
||
49 | */ |
||
50 | private $signer; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $signatureKey; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $verificationKey; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | private $expirationTime; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | private $refreshTime; |
||
71 | |||
72 | /** |
||
73 | * @var Parser |
||
74 | */ |
||
75 | private $tokenParser; |
||
76 | |||
77 | /** |
||
78 | * @var SetCookie |
||
79 | */ |
||
80 | private $defaultCookie; |
||
81 | |||
82 | /** |
||
83 | * @var CurrentTimeProviderInterface |
||
84 | */ |
||
85 | private $currentTimeProvider; |
||
86 | |||
87 | /** |
||
88 | * @param Signer $signer |
||
89 | * @param string $signatureKey |
||
90 | * @param string $verificationKey |
||
91 | * @param SetCookie $defaultCookie |
||
92 | * @param Parser $tokenParser |
||
93 | * @param int $expirationTime |
||
94 | * @param CurrentTimeProviderInterface $currentTimeProvider |
||
95 | * @param int $refreshTime |
||
96 | */ |
||
97 | 7 | public function __construct( |
|
116 | |||
117 | /** |
||
118 | * This constructor simplifies instantiation when using HTTPS (REQUIRED!) and symmetric key encription |
||
119 | * |
||
120 | * @param string $symmetricKey |
||
121 | * @param int $expirationTime |
||
122 | * |
||
123 | * @return self |
||
124 | */ |
||
125 | 1 | public static function fromSymmetricKeyDefaults(string $symmetricKey, int $expirationTime) : SessionMiddleware |
|
140 | |||
141 | /** |
||
142 | * This constructor simplifies instantiation when using HTTPS (REQUIRED!) and asymmetric key encription |
||
143 | * based on RSA keys |
||
144 | * |
||
145 | * @param string $privateRsaKey |
||
146 | * @param string $publicRsaKey |
||
147 | * @param int $expirationTime |
||
148 | * |
||
149 | * @return self |
||
150 | */ |
||
151 | 1 | public static function fromAsymmetricKeyDefaults( |
|
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | * |
||
173 | * @throws \InvalidArgumentException |
||
174 | * @throws \OutOfBoundsException |
||
175 | */ |
||
176 | 40 | public function __invoke(Request $request, Response $response, callable $out = null) : Response |
|
189 | |||
190 | /** |
||
191 | * Extract the token from the given request object |
||
192 | * |
||
193 | * @param Request $request |
||
194 | * |
||
195 | * @return Token|null |
||
196 | */ |
||
197 | 40 | private function parseToken(Request $request) |
|
218 | |||
219 | /** |
||
220 | * @param Token|null $token |
||
221 | * |
||
222 | * @return SessionInterface |
||
223 | */ |
||
224 | 38 | public function extractSessionContainer(Token $token = null) : SessionInterface |
|
238 | |||
239 | /** |
||
240 | * @param SessionInterface $sessionContainer |
||
241 | * @param Response $response |
||
242 | * @param Token $token |
||
243 | * |
||
244 | * @return Response |
||
245 | * |
||
246 | * @throws \InvalidArgumentException |
||
247 | */ |
||
248 | 40 | private function appendToken(SessionInterface $sessionContainer, Response $response, Token $token = null) : Response |
|
263 | |||
264 | /** |
||
265 | * {@inheritDoc} |
||
266 | 28 | */ |
|
267 | private function shouldTokenBeRefreshed(Token $token) : bool |
||
275 | |||
276 | 9 | /** |
|
277 | * @param SessionInterface $sessionContainer |
||
278 | * |
||
279 | * @return SetCookie |
||
280 | */ |
||
281 | private function getTokenCookie(SessionInterface $sessionContainer) : SetCookie |
||
297 | |||
298 | 17 | /** |
|
299 | * @return SetCookie |
||
300 | */ |
||
301 | private function getExpirationCookie() : SetCookie |
||
312 | 3 | ||
313 | 3 | private function timestamp() : int |
|
319 | } |
||
320 |