|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JWTAuth\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use JWTAuth\JWTManager; |
|
8
|
|
|
use JWTAuth\JWTPayload; |
|
9
|
|
|
|
|
10
|
|
|
trait HasJwtToken |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* JWT token object. |
|
14
|
|
|
* |
|
15
|
|
|
* @var JWTManager|null |
|
16
|
|
|
*/ |
|
17
|
|
|
protected ?JWTManager $jwtToken = null; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Payload key name. |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
4 |
|
public function getJwtPayloadIdentifierKey(): string |
|
26
|
|
|
{ |
|
27
|
4 |
|
return $this->getJwtAuthIdentifierKey(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Model token key name. |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function getJwtAuthIdentifierKey(): string |
|
36
|
|
|
{ |
|
37
|
4 |
|
if (method_exists($this, 'getAuthIdentifierName')) { |
|
38
|
4 |
|
return $this->getAuthIdentifierName(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return 'id'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get stored JWT tokens for current model. |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function storedJwtTokens(): \Illuminate\Database\Eloquent\Relations\MorphMany |
|
51
|
|
|
{ |
|
52
|
4 |
|
if (!method_exists($this, 'morphMany')) { |
|
53
|
|
|
throw new \Exception('Method "morphMany" should be provided.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
return $this->morphMany(config('jwt-auth.models.tokens', StoredJwtToken::class), 'tokenable'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check abilities for current JWT token. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $ability |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function jwtTokenCan(string $ability): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->currentJwtToken() && $this->currentJwtToken()->payload()->can($ability); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get loaded JWT token. |
|
73
|
|
|
* |
|
74
|
|
|
* @return JWTManager|null |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function currentJwtToken(): ?JWTManager |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->jwtToken; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set JWT token. |
|
83
|
|
|
* |
|
84
|
|
|
* @param JWTManager $jwt |
|
85
|
|
|
* |
|
86
|
|
|
* @return static |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function withJwtToken(JWTManager $jwt): static |
|
89
|
|
|
{ |
|
90
|
4 |
|
$this->jwtToken = $jwt; |
|
91
|
|
|
|
|
92
|
4 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritDoc |
|
97
|
|
|
*/ |
|
98
|
4 |
|
public function createPayload(string $name, array $abilities = [ '*' ], ?int $lifetimeInSeconds = null): JWTPayload |
|
99
|
|
|
{ |
|
100
|
4 |
|
$storedToken = $this->storedJwtTokens()->create([ |
|
101
|
4 |
|
'name' => $name, |
|
102
|
4 |
|
'jti' => hash(config('jwt-auth.token.jti.hash_algo', 'sha256'), Str::random(40)), |
|
103
|
4 |
|
'abilities' => $abilities, |
|
104
|
4 |
|
'exp' => Carbon::now()->addSeconds($lifetimeInSeconds ?: $this->jwtTokenLifetimeInSeconds())->timestamp, |
|
105
|
4 |
|
]); |
|
106
|
4 |
|
$payload = ( new JWTPayload([ |
|
107
|
4 |
|
$this->getJwtPayloadIdentifierKey() => $this->{$this->getJwtAuthIdentifierKey()}, |
|
108
|
4 |
|
'exp' => $storedToken->exp, |
|
109
|
4 |
|
'jti' => $storedToken->jti, |
|
110
|
4 |
|
'abilities' => $storedToken->abilities, |
|
111
|
4 |
|
'iat' => $storedToken->created_at->timestamp, |
|
112
|
4 |
|
]) ); |
|
113
|
|
|
|
|
114
|
4 |
|
if (method_exists($this, 'additionalJwtPayload')) { |
|
115
|
|
|
$payload->add($this->additionalJwtPayload()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
return $payload; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
4 |
|
public function jwtTokenLifetimeInSeconds():int |
|
122
|
|
|
{ |
|
123
|
4 |
|
return (int) config('jwt-auth.token.expiration', 3600 * 24); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|