1 | <?php |
||
13 | class CheckJwtToken implements MiddlewareInterface |
||
14 | { |
||
15 | |||
16 | /** @var string */ |
||
17 | protected $tokenAttr = 'token'; |
||
18 | /** @var string */ |
||
19 | protected $ipAttr = 'client-ip'; |
||
20 | /** @var string */ |
||
21 | protected $cookieName = 'auth_token'; |
||
22 | /** @var JwtService */ |
||
23 | protected $jwtService; |
||
24 | /** @var bool */ |
||
25 | protected $checkIp = true; |
||
26 | /** @var bool */ |
||
27 | protected $cookieNameWithIp = true; |
||
28 | |||
29 | /** |
||
30 | * @param JwtService $jwtService |
||
31 | */ |
||
32 | public function __construct(JwtService $jwtService) |
||
36 | |||
37 | /** |
||
38 | * @param bool $checkIp |
||
39 | * |
||
40 | * @return $this |
||
41 | */ |
||
42 | public function setCheckIp(bool $checkIp): self |
||
47 | |||
48 | /** |
||
49 | * @param string $cookieName |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function setCookieName(string $cookieName): self |
||
58 | |||
59 | /** |
||
60 | * @param bool $cookieNameWithIp |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function setCookieNameWithIp(bool $cookieNameWithIp): self |
||
69 | |||
70 | /** |
||
71 | * @param string $attr |
||
72 | * |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function setIpAttr(string $attr): self |
||
80 | |||
81 | /** |
||
82 | * @param string $attr |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function setTokenAttr(string $attr): self |
||
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | * @throws TokenException |
||
95 | */ |
||
96 | public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface |
||
118 | |||
119 | /** |
||
120 | * @param Token $token |
||
121 | * @param string|null $clientIp |
||
122 | * |
||
123 | * @throws TokenException |
||
124 | */ |
||
125 | protected function checkTokenIp(Token $token, string $clientIp = null): void |
||
135 | |||
136 | protected function getIpFromToken(Token $token): ?string |
||
141 | |||
142 | /** |
||
143 | * @param ServerRequestInterface $request |
||
144 | * |
||
145 | * @return null|string |
||
146 | */ |
||
147 | protected function getTokenFromRequest(ServerRequestInterface $request): ?string |
||
153 | |||
154 | /** |
||
155 | * @param ServerRequestInterface $request |
||
156 | * |
||
157 | * @return null|string |
||
158 | */ |
||
159 | 3 | protected function getTokenFromBearerHeader(ServerRequestInterface $request): ?string |
|
166 | |||
167 | /** |
||
168 | * @param ServerRequestInterface $request |
||
169 | * |
||
170 | * @return null|string |
||
171 | */ |
||
172 | 5 | protected function getTokenFromCookie(ServerRequestInterface $request): ?string |
|
179 | |||
180 | 5 | protected function getCookieName(ServerRequestInterface $request): string |
|
188 | } |
||
189 |