Skip to content

Commit

Permalink
make aud and azp checks optional when logging in or validating a bear…
Browse files Browse the repository at this point in the history
…er token

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Aug 20, 2024
1 parent 125e525 commit 4630759
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,16 @@ it is possible to disable the classic "self-encoded" validation:
],
```

### Disable audience check in bearer token validation
### Disable audience and azp checks

The `audience` and `azp` token claims will be checked when validating a bearer token for authenticated API requests.
You can disable this check with this config value:
The `audience` and `azp` token claims will be checked when validating a login or bearer ID token.
You can disable these check with these config value (in config.php):
``` php
'user_oidc' => [
'login_validation_audience_check' => false,
'login_validation_azp_check' => false,
'selfencoded_bearer_validation_audience_check' => false,
'selfencoded_bearer_validation_azp_check' => false,
],
```

Expand Down
40 changes: 24 additions & 16 deletions lib/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,23 +489,31 @@ public function code(string $state = '', string $code = '', string $scope = '',
}

// Verify audience
$tokenAudience = $idTokenPayload->aud;
$providerClientId = $provider->getClientId();
if (
(is_string($tokenAudience) && $tokenAudience !== $providerClientId)
$checkAudience = !isset($oidcSystemConfig['login_validation_audience_check'])
|| !in_array($oidcSystemConfig['login_validation_audience_check'], [false, 'false', 0, '0'], true);
if ($checkAudience) {
$tokenAudience = $idTokenPayload->aud;
$providerClientId = $provider->getClientId();
if (
(is_string($tokenAudience) && $tokenAudience !== $providerClientId)
|| (is_array($tokenAudience) && !in_array($providerClientId, $tokenAudience, true))
) {
$this->logger->debug('This token is not for us');
$message = $this->l10n->t('The audience does not match ours');
return $this->build403TemplateResponse($message, Http::STATUS_FORBIDDEN, ['invalid_audience' => $idTokenPayload->aud]);
}

// ref https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
// If the azp claim is present, it should be the client ID
if (isset($idTokenPayload->azp) && $idTokenPayload->azp !== $provider->getClientId()) {
$this->logger->debug('This token is not for us, authorized party (azp) is different than the client ID');
$message = $this->l10n->t('The authorized party does not match ours');
return $this->build403TemplateResponse($message, Http::STATUS_FORBIDDEN, ['invalid_azp' => $idTokenPayload->azp]);
) {
$this->logger->debug('This token is not for us');
$message = $this->l10n->t('The audience does not match ours');
return $this->build403TemplateResponse($message, Http::STATUS_FORBIDDEN, ['invalid_audience' => $idTokenPayload->aud]);
}
}

$checkAzp = !isset($oidcSystemConfig['login_validation_azp_check'])
|| !in_array($oidcSystemConfig['login_validation_azp_check'], [false, 'false', 0, '0'], true);
if ($checkAzp) {
// ref https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
// If the azp claim is present, it should be the client ID
if (isset($idTokenPayload->azp) && $idTokenPayload->azp !== $provider->getClientId()) {
$this->logger->debug('This token is not for us, authorized party (azp) is different than the client ID');
$message = $this->l10n->t('The authorized party does not match ours');
return $this->build403TemplateResponse($message, Http::STATUS_FORBIDDEN, ['invalid_azp' => $idTokenPayload->azp]);
}
}

if (isset($idTokenPayload->nonce) && $idTokenPayload->nonce !== $this->session->get(self::NONCE)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/User/Validator/SelfEncodedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ public function isValidBearerToken(Provider $provider, string $bearerToken): ?st
$this->logger->debug('This token is not for us, the audience does not match the client ID');
return null;
}
}

$checkAzp = !isset($oidcSystemConfig['selfencoded_bearer_validation_azp_check'])
|| !in_array($oidcSystemConfig['selfencoded_bearer_validation_azp_check'], [false, 'false', 0, '0'], true);
if ($checkAzp) {
// ref https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
// If the azp claim is present, it should be the client ID
if (isset($payload->azp) && $payload->azp !== $providerClientId) {
Expand Down

0 comments on commit 4630759

Please sign in to comment.