7
7
use DateTime ;
8
8
use DateTimeImmutable ;
9
9
use DateTimeInterface ;
10
+ use Illuminate \Support \Traits \Conditionable ;
11
+ use Illuminate \Support \Traits \ForwardsCalls ;
10
12
use Lcobucci \JWT \Encoding \ChainedFormatter ;
11
13
use Lcobucci \JWT \Encoding \JoseEncoder ;
12
14
use Lcobucci \JWT \Signer \Hmac \Sha256 ;
19
21
*/
20
22
class Client
21
23
{
24
+ use ForwardsCalls, Conditionable;
25
+
22
26
protected Builder $ builder ;
23
- protected string $ signingKey ;
24
- protected bool $ isSigned = false ;
25
27
protected array $ configures = [];
26
28
27
29
public function __construct (
28
- protected string $ defaultSigningKey ,
30
+ protected string $ signingKey ,
29
31
protected int |CarbonImmutable $ lifetime ,
30
32
protected string $ issuer ,
31
33
protected string $ audience )
32
- {
33
- $ this ->reset ();
34
- }
35
-
36
- public function reset (): self
37
34
{
38
35
$ this ->builder = new Builder (new JoseEncoder (), ChainedFormatter::default ());
39
- $ this ->lifetime ($ this ->lifetime );
40
- $ this ->configures = [];
41
- $ this ->signingKey = $ this ->defaultSigningKey ;
42
- $ this ->isSigned = false ;
43
-
44
- return $ this ;
45
- }
46
-
47
- public function defaultAudience (): string
48
- {
49
- return $ this ->audience ;
50
- }
51
-
52
- public function defaultIssuer (): string
53
- {
54
- return $ this ->issuer ;
55
36
}
56
37
57
38
public function signWith (string $ signingKey ): self
@@ -66,44 +47,46 @@ public function signingKey(): string
66
47
return $ this ->signingKey ;
67
48
}
68
49
69
- public function getToken (): Plain
50
+ public function audience (): string
70
51
{
71
- // Ensure we have an audience set
72
- if (!in_array ('permittedFor ' , $ this ->configures )) {
73
- $ this ->builder ->permittedFor ($ this ->audience );
74
- }
75
-
76
- // Ensure we have an issuer set
77
- if (!in_array ('issuedBy ' , $ this ->configures )) {
78
- $ this ->builder ->issuedBy ($ this ->issuer );
79
- }
52
+ return $ this ->audience ;
53
+ }
80
54
81
- $ token = $ this ->builder ->getToken (new Sha256 (), InMemory::plainText ($ this ->signingKey ()));
55
+ public function issuer (): string
56
+ {
57
+ return $ this ->issuer ;
58
+ }
82
59
83
- $ this ->reset ();
60
+ public function getToken (): Plain
61
+ {
62
+ // Set our own default audience, issuer, and expiration if none has been set so far
63
+ in_array ('permittedFor ' , $ this ->configures ) || $ this ->permittedFor ($ this ->audience ());
64
+ in_array ('issuedBy ' , $ this ->configures ) || $ this ->issuedBy ($ this ->issuer ());
65
+ in_array ('expiresAt ' , $ this ->configures ) || $ this ->lifetime ($ this ->lifetime );
84
66
85
- return $ token ;
67
+ return $ this -> builder -> getToken ( new Sha256 (), InMemory:: plainText ( $ this -> signingKey ())) ;
86
68
}
87
69
88
70
public function __toString (): string
89
71
{
90
- return ( string ) $ this ->getToken ();
72
+ return $ this ->getToken ()-> toString ();
91
73
}
92
74
93
- public function expiresAt (DateTime | DateTimeImmutable $ expiration ): self
75
+ public function expiresAt (DateTimeInterface $ expiration ): self
94
76
{
95
77
if ($ expiration instanceof DateTime) {
96
78
$ expiration = DateTimeImmutable::createFromMutable ($ expiration );
97
79
}
98
80
99
81
$ this ->builder ->expiresAt ($ expiration );
82
+ $ this ->configures [] = "expiresAt " ;
100
83
101
84
return $ this ;
102
85
}
103
86
104
- public function lifetime (int $ lifetime ): self
87
+ public function lifetime (int $ seconds ): self
105
88
{
106
- $ this ->builder -> expiresAt (CarbonImmutable::now ()->addSeconds ($ lifetime ));
89
+ $ this ->expiresAt (CarbonImmutable::now ()->addSeconds ($ seconds ));
107
90
108
91
return $ this ;
109
92
}
@@ -117,48 +100,23 @@ public function withClaims(array $claims = []): self
117
100
return $ this ;
118
101
}
119
102
120
- public function get (string $ id , array $ claims = [], int |DateTimeInterface $ lifetime = null , string $ signingKey = null ): string
103
+ public function get (string $ id , array $ claims = [], int |DateTimeInterface $ lifetime = null , string $ signingKey = null ): string
121
104
{
122
- if ($ signingKey !== null ) {
123
- $ this ->signWith ($ signingKey );
124
- }
125
-
126
- if (is_int ($ lifetime )) {
127
- $ this ->lifetime ($ lifetime );
128
- }
129
-
130
- if ($ lifetime instanceof DateTimeInterface) {
131
- $ this ->expiresAt ($ lifetime );
132
- }
133
-
134
105
return $ this
106
+ ->when ($ signingKey !== null , fn () => $ this ->signWith ($ signingKey ))
107
+ ->when (is_int ($ lifetime ), fn () => $ this ->lifetime ($ lifetime ))
108
+ ->when ($ lifetime instanceof DateTimeInterface, fn () => $ this ->expiresAt ($ lifetime ))
135
109
->withClaims ($ claims )
136
110
->identifiedBy ($ id )
137
111
->getToken ()
138
112
->toString ();
139
113
}
140
114
141
- public function setAudience (string $ audience ): self
142
- {
143
- $ this ->builder ->permittedFor ($ audience );
144
- $ this ->claims [] = "aud " ;
145
-
146
- return $ this ;
147
- }
148
-
149
- public function setIssuer (string $ issuer )
150
- {
151
- $ this ->builder ->issuedBy ($ issuer );
152
- $ this ->claims [] = "iss " ;
153
-
154
- return $ this ;
155
- }
156
-
157
115
public function __call (string $ method , array $ parameters ): mixed
158
116
{
159
117
$ this ->configures [] = $ method ;
160
118
161
- $ result = call_user_func_array ([ $ this ->builder , $ method] , $ parameters );
119
+ $ result = $ this -> forwardCallTo ( $ this ->builder , $ method , $ parameters );
162
120
163
121
return $ result instanceof Builder
164
122
? $ this
0 commit comments