Skip to content

Commit 7de2564

Browse files
committed
Add issuer iss as HTTP Query Parameter to redirect URL in OAuth responses.
1 parent f5fa894 commit 7de2564

1 file changed

Lines changed: 41 additions & 2 deletions

File tree

src/Server.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ final public function respondToAccessTokenRequest(Request $request) : Response
4040
$response = $this->response;
4141

4242
try {
43-
return $authorizationServer->respondToAccessTokenRequest($request, $response);
43+
$httpResponse = $authorizationServer->respondToAccessTokenRequest($request, $response);
4444
} catch (OAuthServerException $serverException) {
45-
return $this->createOauthServerExceptionResponse($response, $serverException);
45+
$httpResponse = $this->createOauthServerExceptionResponse($response, $serverException);
4646
}
47+
48+
return $this->addIssuerToRedirectUrl($httpResponse);
4749
}
4850

4951
/**
@@ -95,6 +97,7 @@ final public function respondToAuthorizationRequest(
9597

9698
// Return the HTTP redirect response
9799
$response = $authorizationServer->completeAuthorizationRequest($authRequest, $response);
100+
$response = $this->addIssuerToRedirectUrl($response);
98101
} else {
99102
// @CHECKME: 404 or throw Exception?
100103
$response = $response->withStatus(404);
@@ -150,4 +153,40 @@ private function createJsonResponse(Response $response, $json = null) : Response
150153

151154
return $response->withHeader('content-type', 'application/json; charset=UTF-8');
152155
}
156+
157+
/**
158+
* Add `iss` query param to the Location header, if present and not already set.
159+
*
160+
* @see https://www.ietf.org/rfc/rfc9207
161+
*/
162+
private function addIssuerToRedirectUrl(Response $response): Response
163+
{
164+
if ($response->hasHeader('Location')) {
165+
$location = $response->getHeaderLine('Location');
166+
167+
$urlParts = parse_url($location);
168+
$queryParams = [];
169+
if (isset($urlParts['query'])) {
170+
parse_str($urlParts['query'], $queryParams);
171+
}
172+
173+
if ( ! array_key_exists('iss', $queryParams)) {
174+
$issuer = $this->config->getServer()->get(OidcMeta::ISSUER);
175+
$queryParams['iss'] = $issuer;
176+
177+
$urlParts['query'] = http_build_query($queryParams);
178+
179+
$location = vsprintf("%s%s%s?%s", [
180+
isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : '',
181+
$urlParts['host'] ?? '',
182+
$urlParts['path'] ?? '',
183+
$urlParts['query']
184+
]);
185+
186+
$response = $response->withHeader('Location', $location);
187+
}
188+
}
189+
190+
return $response;
191+
}
153192
}

0 commit comments

Comments
 (0)