1010 */
1111namespace wcmf \lib \core \impl ;
1212
13+ use Lcobucci \Clock \SystemClock ;
1314use Lcobucci \JWT \Builder ;
14- use Lcobucci \JWT \Parser ;
15+ use Lcobucci \JWT \JwtFacade ;
1516use Lcobucci \JWT \Signer \Hmac \Sha256 ;
16- use Lcobucci \JWT \ValidationData ;
17+ use Lcobucci \JWT \Signer \Key \InMemory ;
18+ use Lcobucci \JWT \Validation \Constraint ;
1719use wcmf \lib \config \Configuration ;
1820use wcmf \lib \core \ObjectFactory ;
19- use wcmf \lib \core \Session ;
2021use wcmf \lib \core \TokenBasedSession ;
2122use wcmf \lib \security \principal \impl \AnonymousUser ;
2223use wcmf \lib \util \StringUtil ;
@@ -46,7 +47,7 @@ class ClientSideSession implements TokenBasedSession {
4647 public function __construct (Configuration $ configuration ) {
4748 $ this ->cookiePrefix = strtolower (StringUtil::slug ($ configuration ->getValue ('title ' , 'application ' )));
4849 $ this ->tokenName = $ this ->getCookiePrefix ().'-auth-token ' ;
49- $ this ->key = $ configuration ->getValue ('secret ' , 'application ' );
50+ $ this ->key = InMemory:: plainText ( $ configuration ->getValue ('secret ' , 'application ' ) );
5051 }
5152
5253 /**
@@ -156,7 +157,7 @@ public function getAuthUser() {
156157 $ login = AnonymousUser::USER_GROUP_NAME ;
157158 // check for auth user in token
158159 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 ];
160161 }
161162 return $ login ;
162163 }
@@ -175,13 +176,16 @@ protected function getCookiePrefix() {
175176 * @return String
176177 */
177178 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 ();
185189 }
186190
187191 /**
@@ -194,7 +198,7 @@ protected function getTokenIssuer() {
194198
195199 /**
196200 * Get the token issuer
197- * @return String
201+ * @return \Lcobucci\JWT\Signer
198202 */
199203 protected function getTokenSigner () {
200204 return new Sha256 ();
@@ -211,13 +215,16 @@ protected function getTokenData() {
211215 $ token = $ request ->hasHeader (self ::TOKEN_HEADER ) ?
212216 trim (str_replace (self ::AUTH_TYPE , '' , $ request ->getHeader (self ::TOKEN_HEADER ))) : $ this ->token ;
213217 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
221228 }
222229 }
223230 return $ result ;
0 commit comments