File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change 44
55use  Lexik \Bundle \JWTAuthenticationBundle \Event \JWTCreatedEvent ;
66use  Lexik \Bundle \JWTAuthenticationBundle \Events ;
7+ use  Psr \Clock \ClockInterface ;
78use  Symfony \Component \EventDispatcher \EventSubscriberInterface ;
9+ use  Symfony \Component \Clock \NativeClock ;
810
911final  class  AdditionalAccessTokenClaimsAndHeaderSubscriber implements  EventSubscriberInterface
1012{
11-     /** 
12-      * @var int|null 
13-      */ 
14-     private  $ ttl ;
13+     private  ?int  $ ttl ;
14+     private  ?ClockInterface   $ clock ;
1515
16-     public  function  __construct (?int  $ ttl )
16+     public  function  __construct (?int  $ ttl, ? ClockInterface   $ clock  =  null )
1717    {
1818        $ this  ->ttl  = $ ttl ;
19+ 
20+         if  (null  === $ clock ) {
21+             $ this  ->clock  = new  NativeClock (new  \DateTimeZone ('UTC ' ));
22+         }
1923    }
2024
2125    public  static  function  getSubscribedEvents (): array 
@@ -29,14 +33,16 @@ public static function getSubscribedEvents(): array
2933
3034    public  function  addClaims (JWTCreatedEvent   $ event ): void 
3135    {
36+         $ now  = $ this  ->clock ->now ();
37+ 
3238        $ claims  = [
3339            'jti '  => uniqid ('' , true ),
34-             'iat '  => time () ,
35-             'nbf '  => time () ,
40+             'iat '  => $ now  ,
41+             'nbf '  => $ now  ,
3642        ];
3743        $ data  = $ event ->getData ();
3844        if  (!array_key_exists ('exp ' , $ data ) && $ this  ->ttl  > 0 ) {
39-             $ claims ['exp ' ] = time () +  $ this  ->ttl ;
45+             $ claims ['exp ' ] = $ now -> modify ( sprintf ( ' +%d second ' , $ this  ->ttl )) ;
4046        }
4147        $ event ->setData (array_merge ($ claims , $ data ));
4248    }
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments