10
10
*/
11
11
namespace wcmf \lib \core \impl ;
12
12
13
+ use Lcobucci \Clock \SystemClock ;
13
14
use Lcobucci \JWT \Builder ;
14
- use Lcobucci \JWT \Parser ;
15
+ use Lcobucci \JWT \JwtFacade ;
15
16
use Lcobucci \JWT \Signer \Hmac \Sha256 ;
16
- use Lcobucci \JWT \ValidationData ;
17
+ use Lcobucci \JWT \Signer \Key \InMemory ;
18
+ use Lcobucci \JWT \Validation \Constraint ;
17
19
use wcmf \lib \config \Configuration ;
18
20
use wcmf \lib \core \ObjectFactory ;
19
- use wcmf \lib \core \Session ;
20
21
use wcmf \lib \core \TokenBasedSession ;
21
22
use wcmf \lib \security \principal \impl \AnonymousUser ;
22
23
use wcmf \lib \util \StringUtil ;
@@ -46,7 +47,7 @@ class ClientSideSession implements TokenBasedSession {
46
47
public function __construct (Configuration $ configuration ) {
47
48
$ this ->cookiePrefix = strtolower (StringUtil::slug ($ configuration ->getValue ('title ' , 'application ' )));
48
49
$ this ->tokenName = $ this ->getCookiePrefix ().'-auth-token ' ;
49
- $ this ->key = $ configuration ->getValue ('secret ' , 'application ' );
50
+ $ this ->key = InMemory:: plainText ( $ configuration ->getValue ('secret ' , 'application ' ) );
50
51
}
51
52
52
53
/**
@@ -156,7 +157,7 @@ public function getAuthUser() {
156
157
$ login = AnonymousUser::USER_GROUP_NAME ;
157
158
// check for auth user in token
158
159
if (($ data = $ this ->getTokenData ()) !== null && isset ($ data [self ::AUTH_USER_NAME ])) {
159
- $ login = $ data [self ::AUTH_USER_NAME ]-> getValue () ;
160
+ $ login = $ data [self ::AUTH_USER_NAME ];
160
161
}
161
162
return $ login ;
162
163
}
@@ -175,13 +176,16 @@ protected function getCookiePrefix() {
175
176
* @return String
176
177
*/
177
178
protected function createToken ($ login ) {
178
- $ jwt = (new Builder ())
179
- ->issueBy ($ this ->getTokenIssuer ())
180
- ->issuedAt (time ())
181
- ->expiresAt (time ()+3600 )
182
- ->withClaim (self ::AUTH_USER_NAME , $ login )
183
- ->getToken ($ this ->getTokenSigner (), $ this ->key );
184
- return $ jwt ->__toString ();
179
+ $ jwt = (new JwtFacade ())->issue (
180
+ $ this ->getTokenSigner (),
181
+ $ this ->key ,
182
+ function (Builder $ builder , \DateTimeImmutable $ issuedAt ) use ($ login ): Builder {
183
+ return $ builder
184
+ ->issuedBy ($ this ->getTokenIssuer ())
185
+ ->expiresAt ($ issuedAt ->modify ('+1 hours ' ))
186
+ ->withClaim (self ::AUTH_USER_NAME , $ login );
187
+ });
188
+ return $ jwt ->toString ();
185
189
}
186
190
187
191
/**
@@ -194,7 +198,7 @@ protected function getTokenIssuer() {
194
198
195
199
/**
196
200
* Get the token issuer
197
- * @return String
201
+ * @return \Lcobucci\JWT\Signer
198
202
*/
199
203
protected function getTokenSigner () {
200
204
return new Sha256 ();
@@ -211,13 +215,16 @@ protected function getTokenData() {
211
215
$ token = $ request ->hasHeader (self ::TOKEN_HEADER ) ?
212
216
trim (str_replace (self ::AUTH_TYPE , '' , $ request ->getHeader (self ::TOKEN_HEADER ))) : $ this ->token ;
213
217
if ($ token !== null ) {
214
- $ jwt = (new Parser ())->parse ((string )$ token );
215
-
216
- // validate
217
- $ data = new ValidationData ();
218
- $ data ->setIssuer ($ this ->getTokenIssuer ());
219
- if ($ jwt ->validate ($ data ) && $ jwt ->verify ($ this ->getTokenSigner (), $ this ->key )) {
220
- $ result = $ jwt ->getClaims ();
218
+ try {
219
+ $ jwt = (new JwtFacade ())->parse ((string )$ token ,
220
+ new Constraint \SignedWith ($ this ->getTokenSigner (), $ this ->key ),
221
+ new Constraint \StrictValidAt (SystemClock::fromSystemTimezone ()),
222
+ new Constraint \IssuedBy ($ this ->getTokenIssuer ())
223
+ );
224
+ $ result = $ jwt ->claims ()->all ();
225
+ }
226
+ catch (\Exception $ ex ) {
227
+ // invalid token
221
228
}
222
229
}
223
230
return $ result ;
0 commit comments