77use DateTime ;
88use DateTimeImmutable ;
99use DateTimeInterface ;
10+ use Illuminate \Support \Traits \Conditionable ;
11+ use Illuminate \Support \Traits \ForwardsCalls ;
1012use Lcobucci \JWT \Encoding \ChainedFormatter ;
1113use Lcobucci \JWT \Encoding \JoseEncoder ;
1214use Lcobucci \JWT \Signer \Hmac \Sha256 ;
1921 */
2022class Client
2123{
24+ use ForwardsCalls, Conditionable;
25+
2226 protected Builder $ builder ;
23- protected string $ signingKey ;
24- protected bool $ isSigned = false ;
2527 protected array $ configures = [];
2628
2729 public function __construct (
28- protected string $ defaultSigningKey ,
30+ protected string $ signingKey ,
2931 protected int |CarbonImmutable $ lifetime ,
3032 protected string $ issuer ,
3133 protected string $ audience )
32- {
33- $ this ->reset ();
34- }
35-
36- public function reset (): self
3734 {
3835 $ 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 ;
5536 }
5637
5738 public function signWith (string $ signingKey ): self
@@ -66,44 +47,46 @@ public function signingKey(): string
6647 return $ this ->signingKey ;
6748 }
6849
69- public function getToken (): Plain
50+ public function audience (): string
7051 {
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+ }
8054
81- $ token = $ this ->builder ->getToken (new Sha256 (), InMemory::plainText ($ this ->signingKey ()));
55+ public function issuer (): string
56+ {
57+ return $ this ->issuer ;
58+ }
8259
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 );
8466
85- return $ token ;
67+ return $ this -> builder -> getToken ( new Sha256 (), InMemory:: plainText ( $ this -> signingKey ())) ;
8668 }
8769
8870 public function __toString (): string
8971 {
90- return ( string ) $ this ->getToken ();
72+ return $ this ->getToken ()-> toString ();
9173 }
9274
93- public function expiresAt (DateTime | DateTimeImmutable $ expiration ): self
75+ public function expiresAt (DateTimeInterface $ expiration ): self
9476 {
9577 if ($ expiration instanceof DateTime) {
9678 $ expiration = DateTimeImmutable::createFromMutable ($ expiration );
9779 }
9880
9981 $ this ->builder ->expiresAt ($ expiration );
82+ $ this ->configures [] = "expiresAt " ;
10083
10184 return $ this ;
10285 }
10386
104- public function lifetime (int $ lifetime ): self
87+ public function lifetime (int $ seconds ): self
10588 {
106- $ this ->builder -> expiresAt (CarbonImmutable::now ()->addSeconds ($ lifetime ));
89+ $ this ->expiresAt (CarbonImmutable::now ()->addSeconds ($ seconds ));
10790
10891 return $ this ;
10992 }
@@ -117,48 +100,23 @@ public function withClaims(array $claims = []): self
117100 return $ this ;
118101 }
119102
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
121104 {
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-
134105 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 ))
135109 ->withClaims ($ claims )
136110 ->identifiedBy ($ id )
137111 ->getToken ()
138112 ->toString ();
139113 }
140114
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-
157115 public function __call (string $ method , array $ parameters ): mixed
158116 {
159117 $ this ->configures [] = $ method ;
160118
161- $ result = call_user_func_array ([ $ this ->builder , $ method] , $ parameters );
119+ $ result = $ this -> forwardCallTo ( $ this ->builder , $ method , $ parameters );
162120
163121 return $ result instanceof Builder
164122 ? $ this
0 commit comments