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