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 | 1 | public function __construct(JwtService $jwtService) |
|
33 | { |
||
34 | 1 | $this->jwtService = $jwtService; |
|
35 | 1 | } |
|
36 | |||
37 | /** |
||
38 | * @param bool $checkIp |
||
39 | * |
||
40 | * @return $this |
||
41 | */ |
||
42 | 2 | public function setCheckIp(bool $checkIp): self |
|
43 | { |
||
44 | 2 | $this->checkIp = $checkIp; |
|
45 | 2 | return $this; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $cookieName |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | 2 | public function setCookieName(string $cookieName): self |
|
54 | { |
||
55 | 2 | $this->cookieName = $cookieName; |
|
56 | 2 | return $this; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param bool $cookieNameWithIp |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | 2 | public function setCookieNameWithIp(bool $cookieNameWithIp): self |
|
65 | { |
||
66 | 2 | $this->cookieNameWithIp = $cookieNameWithIp; |
|
67 | 2 | return $this; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $attr |
||
72 | * |
||
73 | * @return $this |
||
74 | */ |
||
75 | 2 | public function setIpAttr(string $attr): self |
|
76 | { |
||
77 | 2 | $this->ipAttr = $attr; |
|
78 | 2 | return $this; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param string $attr |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | 2 | public function setTokenAttr(string $attr): self |
|
87 | { |
||
88 | 2 | $this->tokenAttr = $attr; |
|
89 | 2 | return $this; |
|
90 | } |
||
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 | 2 | protected function getTokenFromRequest(ServerRequestInterface $request): ?string |
|
148 | { |
||
149 | 2 | return $request->hasHeader('Authorization') |
|
150 | 1 | ? $this->getTokenFromBearerHeader($request) |
|
151 | 2 | : $this->getTokenFromCookie($request); |
|
152 | } |
||
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 | 3 | protected function getCookieName(ServerRequestInterface $request): string |
|
181 | { |
||
182 | 3 | if (!$this->cookieNameWithIp) { |
|
183 | 1 | return $this->cookieName; |
|
184 | } |
||
185 | |||
186 | 2 | return $this->cookieName.'_'.$request->getAttribute($this->ipAttr); |
|
188 | } |
||
189 |