Skip to content

Commit b3b7c5a

Browse files
committed
Include "additional parameters" into authorization id hash
This change includes the "additional parameters" into the calculation of the derived authorization id. These parameters typically contain the "audience" for which an access token is requested. Therefore, considering these parameters while building the id allows us to retrieve and store separate access tokens for different purposes / audiences.
1 parent 6c982d3 commit b3b7c5a

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Classes/Authorization.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,17 @@ public static function generateAuthorizationIdForAuthorizationCodeGrant(string $
134134
* @param string $clientId
135135
* @param string $clientSecret
136136
* @param string $scope
137+
* @param array $additionalParameters
137138
* @return string
138139
*/
139-
public static function generateAuthorizationIdForClientCredentialsGrant(string $serviceName, string $clientId, string $clientSecret, string $scope): string
140+
public static function generateAuthorizationIdForClientCredentialsGrant(string $serviceName, string $clientId, string $clientSecret, string $scope, array $additionalParameters = []): string
140141
{
141-
return hash('sha512', $serviceName . $clientId . $clientSecret . $scope . self::GRANT_CLIENT_CREDENTIALS);
142+
try {
143+
$additionalParametersJson = json_encode($additionalParameters, JSON_THROW_ON_ERROR);
144+
} catch (JsonException $e) {
145+
$additionalParametersJson = '';
146+
}
147+
return hash('sha512', $serviceName . $clientId . $clientSecret . $scope . $additionalParametersJson . self::GRANT_CLIENT_CREDENTIALS);
142148
}
143149

144150
/**

Classes/OAuthClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public static function generateAuthorizationIdQueryParameterName(string $service
226226
*/
227227
public function requestAccessToken(string $serviceName, string $clientId, string $clientSecret, string $scope, array $additionalParameters = []): void
228228
{
229-
$authorizationId = Authorization::generateAuthorizationIdForClientCredentialsGrant($serviceName, $clientId, $clientSecret, $scope);
229+
$authorizationId = Authorization::generateAuthorizationIdForClientCredentialsGrant($serviceName, $clientId, $clientSecret, $scope, $additionalParameters);
230230
$this->logger->info(sprintf('OAuth (%s): Retrieving access token using client credentials grant for client "%s" using a %s bytes long secret. (authorization id: %s)', $this->getServiceType(), $clientId, strlen($clientSecret), $authorizationId));
231231

232232
$existingAuthorization = $this->getAuthorization($authorizationId);

Tests/Unit/AuthorizationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ public function getAccessTokenFailsOnEncryptedTokenIfKeyWasChanged(): void
152152
public function generateAuthorizationIdForClientCredentialsGrantReturnsSha1(): void
153153
{
154154
$authorizationId = Authorization::generateAuthorizationIdForClientCredentialsGrant(
155-
'oidc_test', 'ac36cGG4d2Cef1DeuevA7T1u7V4WOUI14', 'CMc4EHfyMPLw}Tua%rnyxCnrTWMuX3', 'oidc profile'
155+
'oidc_test', 'ac36cGG4d2Cef1DeuevA7T1u7V4WOUI14', 'CMc4EHfyMPLw}Tua%rnyxCnrTWMuX3', 'oidc profile', ['audience' => 'https://www.example.com']
156156
);
157-
self::assertSame('bd55b7bc1b40d6342789c74fcc1900877b3966f4656c5d6a1c0a9111a1da02365ba9f00fcb1d058629446f7ec83d02166b0a8c271cbf1374467e7f294bb4b784', $authorizationId);
157+
self::assertSame('c2d332337e6765c1f6876fe61c6bc63e98c1d3018ff5b56899ee54a1d1e8b1a5272b9ae9f73dc37429bccec583c3754d52bd8ef4e0f05001aa02a50e24b654a5', $authorizationId);
158158
}
159159

160160
/**

0 commit comments

Comments
 (0)