|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Modules\Core\Traits\Supports; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Modules\Core\Enums\StatusCodeEnum; |
|
9
|
|
|
use Modules\Core\ErrorCodes\JWTErrorCode; |
|
10
|
|
|
use Modules\Core\Supports\Handler; |
|
11
|
|
|
use Tymon\JWTAuth\Exceptions\{ |
|
12
|
|
|
InvalidClaimException, |
|
13
|
|
|
PayloadException, |
|
14
|
|
|
TokenBlacklistedException, |
|
15
|
|
|
TokenExpiredException, |
|
16
|
|
|
TokenInvalidException, |
|
17
|
|
|
UserNotDefinedException |
|
18
|
|
|
}; |
|
19
|
|
|
|
|
20
|
|
|
trait HandleParseTrait |
|
21
|
|
|
{ |
|
22
|
|
|
protected function parseInvalidClaimException(array $response, Exception $exception) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($exception instanceof InvalidClaimException) { |
|
25
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::INVALID_CLAIM; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return $response; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function parsePayloadException(array $response, Exception $exception) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($exception instanceof PayloadException) { |
|
34
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::PAYLOAD; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $response; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function parseTokenBlacklistedException(array $response, Exception $exception) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($exception instanceof TokenBlacklistedException) { |
|
43
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::TOKEN_BLACKLISTED; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $response; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function parseTokenExpiredException(array $response, Exception $exception) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($exception instanceof TokenExpiredException) { |
|
52
|
|
|
if ('Token has expired and can no longer be refreshed' === $exception->getMessage()) { |
|
53
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::CAN_NOT_REFRESHED; |
|
54
|
|
|
} else { |
|
55
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::TOKEN_EXPIRED; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $response; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function parseTokenInvalidException(array $response, Exception $exception) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($exception instanceof TokenInvalidException) { |
|
65
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::TOKEN_INVALID; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function parseUserNotDefinedException(array $response, Exception $exception) |
|
72
|
|
|
{ |
|
73
|
|
|
if ($exception instanceof UserNotDefinedException) { |
|
74
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::USER_NOT_DEFINED; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $response; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function parseJWTException(array $response, Exception $exception): array |
|
81
|
|
|
{ |
|
82
|
|
|
$response['meta'][Handler::ERROR_CODE] = JWTErrorCode::DEFAULT; |
|
83
|
|
|
$response['meta'][Handler::STATUS_CODE] = StatusCodeEnum::HTTP_UNAUTHORIZED; |
|
84
|
|
|
$response['meta'][Handler::MESSAGE] = $exception->getMessage(); |
|
85
|
|
|
|
|
86
|
|
|
$response = $this->parseInvalidClaimException($response, $exception); |
|
87
|
|
|
$response = $this->parsePayloadException($response, $exception); |
|
88
|
|
|
$response = $this->parseTokenBlacklistedException($response, $exception); |
|
89
|
|
|
$response = $this->parseTokenExpiredException($response, $exception); |
|
90
|
|
|
$response = $this->parseTokenInvalidException($response, $exception); |
|
91
|
|
|
$response = $this->parseUserNotDefinedException($response, $exception); |
|
92
|
|
|
|
|
93
|
|
|
return $response; |
|
94
|
|
|
} |
|
95
|
|
|
} |