1414
1515class TokenGenerator
1616{
17- use CryptTrait;
1817 ////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
1918
20- /** @var Config */
21- public $ config ;
19+ use CryptTrait;
20+
21+ public Config $ config ;
22+
23+ private \DateInterval $ validFor ;
2224
2325 //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2426
2527 final public function __construct (
26- Config $ config
28+ Config $ config ,
29+ \DateInterval $ validFor
2730 ) {
2831 $ this ->config = $ config ;
32+ $ this ->validFor = $ validFor ;
33+
2934 $ this ->setEncryptionKey ($ this ->config ->getKeys ()->getEncryptionKey ());
3035 }
31-
36+
3237 public function generateRegistrationAccessToken ($ clientId , $ privateKey ) {
3338 $ issuer = $ this ->config ->getServer ()->get (OidcMeta::ISSUER );
3439
@@ -42,18 +47,19 @@ public function generateRegistrationAccessToken($clientId, $privateKey) {
4247
4348 return $ token ->toString ();
4449 }
45-
46- public function generateIdToken ($ accessToken , $ clientId , $ subject , $ nonce , $ privateKey , $ dpopKey =null ) {
50+
51+ public function generateIdToken ($ accessToken , $ clientId , $ subject , $ nonce , $ privateKey , $ dpopKey, $ now =null ) {
4752 $ issuer = $ this ->config ->getServer ()->get (OidcMeta::ISSUER );
4853
4954 $ jwks = $ this ->getJwks ();
5055 $ tokenHash = $ this ->generateTokenHash ($ accessToken );
5156
5257 // Create JWT
5358 $ jwtConfig = Configuration::forSymmetricSigner (new Sha256 (), InMemory::plainText ($ privateKey ));
54- $ now = new DateTimeImmutable ();
59+ $ now = $ now ?? new DateTimeImmutable ();
5560 $ useAfter = $ now ->sub (new \DateInterval ('PT1S ' ));
56- $ expire = $ now ->add (new \DateInterval ('PT ' . 14 *24 *60 *60 . 'S ' ));
61+
62+ $ expire = $ now ->add ($ this ->validFor );
5763
5864 $ token = $ jwtConfig ->builder ()
5965 ->issuedBy ($ issuer )
@@ -75,7 +81,7 @@ public function generateIdToken($accessToken, $clientId, $subject, $nonce, $priv
7581 ->getToken ($ jwtConfig ->signer (), $ jwtConfig ->signingKey ());
7682 return $ token ->toString ();
7783 }
78-
84+
7985 public function respondToRegistration ($ registration , $ privateKey ) {
8086 /*
8187 Expects in $registration:
@@ -94,10 +100,10 @@ public function respondToRegistration($registration, $privateKey) {
94100 'token_endpoint_auth_method ' => 'client_secret_basic ' ,
95101 'registration_access_token ' => $ registration_access_token ,
96102 );
97-
103+
98104 return array_merge ($ registrationBase , $ registration );
99105 }
100-
106+
101107 public function addIdTokenToResponse ($ response , $ clientId , $ subject , $ nonce , $ privateKey , $ dpopKey =null ) {
102108 if ($ response ->hasHeader ("Location " )) {
103109 $ value = $ response ->getHeaderLine ("Location " );
@@ -111,7 +117,7 @@ public function addIdTokenToResponse($response, $clientId, $subject, $nonce, $pr
111117 $ privateKey ,
112118 $ dpopKey
113119 );
114- $ value = preg_replace ("/#access_token=(.*?)&/ " , "#access_token= \$1&id_token= $ idToken& " , $ value );
120+ $ value = preg_replace ("/#access_token=(.*?)&/ " , "#access_token= \$1&id_token= $ idToken& " , $ value );
115121 $ response = $ response ->withHeader ("Location " , $ value );
116122 } else if (preg_match ("/code=(.*?)&/ " , $ value , $ matches )) {
117123 $ idToken = $ this ->generateIdToken (
@@ -153,12 +159,13 @@ public function addIdTokenToResponse($response, $clientId, $subject, $nonce, $pr
153159 public function getCodeInfo ($ code ) {
154160 return json_decode ($ this ->decrypt ($ code ), true );
155161 }
162+
156163 ///////////////////////////// HELPER FUNCTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
157164
158165 private function generateJti () {
159166 return substr (md5 ((string )time ()), 12 ); // FIXME: generate unique jti values
160167 }
161-
168+
162169 private function generateTokenHash ($ accessToken ) {
163170 $ atHash = hash ('sha256 ' , $ accessToken );
164171 $ atHash = substr ($ atHash , 0 , 32 );
0 commit comments