Skip to content

Commit

Permalink
Merge pull request #8 from pdsinterop/feature/token-endpoint
Browse files Browse the repository at this point in the history
added id_token option for token endpoint response
  • Loading branch information
ylebre authored Oct 3, 2020
2 parents d82391d + c6fd002 commit 62d3e87
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/TokenGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Pdsinterop\Solid\Auth\Utils\Jwks;
use Pdsinterop\Solid\Auth\Enum\OpenId\OpenIdConnectMetadata as OidcMeta;
use Laminas\Diactoros\Response\JsonResponse as JsonResponse;

class TokenGenerator
{
Expand Down Expand Up @@ -90,8 +91,9 @@ public function respondToRegistration($registration, $privateKey) {
}

public function addIdTokenToResponse($response, $clientId, $subject, $nonce, $privateKey) {
if ($response->hasHeader("Location")) {
if ($response->hasHeader("Location")) {
$value = $response->getHeaderLine("Location");

if (preg_match("/#access_token=(.*?)&/", $value, $matches)) {
$idToken = $this->generateIdToken(
$matches[1],
Expand All @@ -113,6 +115,24 @@ public function addIdTokenToResponse($response, $clientId, $subject, $nonce, $pr
$value = preg_replace("/code=(.*?)&/", "code=\$1&id_token=$idToken&", $value);
$response = $response->withHeader("Location", $value);
}
} else {
$response->getBody()->rewind();
$responseBody = $response->getBody()->getContents();
try {
$body = json_decode($responseBody, true);
if (isset($body['access_token'])) {
$body['id_token'] = $this->generateIdToken(
$body['access_token'],
$clientId,
$subject,
$nonce,
$privateKey
);
return new JsonResponse($body);
}
} catch (\Exception $e) {
// leave the response as it was;
}
}
return $response;
}
Expand Down

0 comments on commit 62d3e87

Please sign in to comment.