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 | |||
27 | /** |
||
28 | * @param JwtService $jwtService |
||
29 | */ |
||
30 | public function __construct(JwtService $jwtService) |
||
34 | |||
35 | /** |
||
36 | * @param bool $checkIp |
||
37 | * |
||
38 | * @return $this |
||
39 | */ |
||
40 | public function setCheckIp(bool $checkIp): self |
||
41 | { |
||
42 | $this->checkIp = $checkIp; |
||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string $cookieName |
||
48 | * |
||
49 | * @return $this |
||
50 | */ |
||
51 | public function setCookieName(string $cookieName): self |
||
56 | |||
57 | /** |
||
58 | * @param string $attr |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function setIpAttr(string $attr): self |
||
67 | |||
68 | /** |
||
69 | * @param string $attr |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function setTokenAttr(string $attr): self |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | * @throws TokenException |
||
82 | */ |
||
83 | public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface |
||
105 | |||
106 | /** |
||
107 | * @param Token $token |
||
108 | * @param string|null $clientIp |
||
109 | * |
||
110 | * @throws TokenException |
||
111 | */ |
||
112 | protected function checkTokenIp(Token $token, string $clientIp = null): void |
||
122 | |||
123 | protected function getIpFromToken(Token $token): ?string |
||
128 | |||
129 | /** |
||
130 | * @param ServerRequestInterface $request |
||
131 | * |
||
132 | * @return null|string |
||
133 | */ |
||
134 | protected function getTokenFromRequest(ServerRequestInterface $request): ?string |
||
140 | |||
141 | /** |
||
142 | * @param ServerRequestInterface $request |
||
143 | * |
||
144 | * @return null|string |
||
145 | */ |
||
146 | protected function getTokenFromBearerHeader(ServerRequestInterface $request): ?string |
||
153 | |||
154 | /** |
||
155 | * @param ServerRequestInterface $request |
||
156 | * |
||
157 | * @return null|string |
||
158 | */ |
||
159 | protected function getTokenFromCookie(ServerRequestInterface $request): ?string |
||
166 | } |
||
167 |