44
55use Pdsinterop \Solid \Auth \Utils \Jwks ;
66use Pdsinterop \Solid \Auth \Enum \OpenId \OpenIdConnectMetadata as OidcMeta ;
7+ use Laminas \Diactoros \Response \JsonResponse as JsonResponse ;
78
89class TokenGenerator
910{
@@ -90,8 +91,9 @@ public function respondToRegistration($registration, $privateKey) {
9091 }
9192
9293 public function addIdTokenToResponse ($ response , $ clientId , $ subject , $ nonce , $ privateKey ) {
93- if ($ response ->hasHeader ("Location " )) {
94+ if ($ response ->hasHeader ("Location " )) {
9495 $ value = $ response ->getHeaderLine ("Location " );
96+
9597 if (preg_match ("/#access_token=(.*?)&/ " , $ value , $ matches )) {
9698 $ idToken = $ this ->generateIdToken (
9799 $ matches [1 ],
@@ -113,6 +115,24 @@ public function addIdTokenToResponse($response, $clientId, $subject, $nonce, $pr
113115 $ value = preg_replace ("/code=(.*?)&/ " , "code= \$1&id_token= $ idToken& " , $ value );
114116 $ response = $ response ->withHeader ("Location " , $ value );
115117 }
118+ } else {
119+ $ response ->getBody ()->rewind ();
120+ $ responseBody = $ response ->getBody ()->getContents ();
121+ try {
122+ $ body = json_decode ($ responseBody , true );
123+ if (isset ($ body ['access_token ' ])) {
124+ $ body ['id_token ' ] = $ this ->generateIdToken (
125+ $ body ['access_token ' ],
126+ $ clientId ,
127+ $ subject ,
128+ $ nonce ,
129+ $ privateKey
130+ );
131+ return new JsonResponse ($ body );
132+ }
133+ } catch (\Exception $ e ) {
134+ // leave the response as it was;
135+ }
116136 }
117137 return $ response ;
118138 }
0 commit comments