Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for EdDSA #1

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Add support for EdDSA signed JWTs

## [1.0.1] - 2024-09-13

### Fixed
Expand Down
53 changes: 45 additions & 8 deletions src/OpenIDConnectClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class OpenIDConnectClient

/**
* @var mixed holds well-known openid configuration parameters, like policy for MS Azure AD B2C User Flow
* @see https://docs.microsoft.com/en-us/azure/active-directory-b2c/user-flow-overview
* @see https://docs.microsoft.com/en-us/azure/active-directory-b2c/user-flow-overview
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto fixing of whitespace by my IDE

*/
private $wellKnownConfigParameters = [];

Expand Down Expand Up @@ -686,7 +686,7 @@ public function getRedirectURL(): string
} else {
$protocol = 'http';
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto fixing of whitespace by my IDE

if (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) {
$port = (int)$_SERVER['HTTP_X_FORWARDED_PORT'];
} elseif (isset($_SERVER['SERVER_PORT'])) {
Expand All @@ -709,7 +709,7 @@ public function getRedirectURL(): string
}

$port = (443 === $port) || (80 === $port) ? '' : ':' . $port;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto fixing of whitespace by my IDE

$explodedRequestUri = isset($_SERVER['REQUEST_URI']) ? explode('?', $_SERVER['REQUEST_URI']) : [];
return sprintf('%s://%s%s/%s', $protocol, $host, $port, trim(reset($explodedRequestUri), '/'));
}
Expand Down Expand Up @@ -1087,6 +1087,18 @@ private function verifyRSAJWTSignature(string $hashType, stdClass $key, $payload
return $key->verify($payload, $signature);
}

private function verifyEdDSAJWTsignature($key, $payload, $signature) {
if (!(property_exists($key, 'x'))) {
throw new OpenIDConnectClientException('Malformed key object');
}

if (!function_exists("sodium_crypto_sign_verify_detached")) {
throw new OpenIDConnectClientException('sodium_crypto_sign_verify_detached support unavailable.');
}

return sodium_crypto_sign_verify_detached($signature, $payload, base64url_decode($key->x));
}

private function verifyHMACJWTSignature(string $hashType, string $key, string $payload, string $signature): bool
{
$expected = hash_hmac($hashType, $payload, $key, true);
Expand Down Expand Up @@ -1145,6 +1157,24 @@ public function verifyJWTSignature(string $jwt): bool
$jwk,
$payload, $signature, $signatureType);
break;
case 'EdDSA':
if (isset($header->jwk)) {
$jwk = $header->jwk;
$this->verifyJWKHeader($jwk);
} else {
$jwks = json_decode($this->fetchURL($this->getProviderConfigValue('jwks_uri')));
if ($jwks === NULL) {
throw new OpenIDConnectClientException('Error decoding JSON from jwks_uri');
}
$jwk = $this->getKeyForHeader($jwks->keys, $header);
}

$verified = $this->verifyEdDSAJWTsignature(
$jwk,
$payload,
$signature
);
break;
case 'HS256':
case 'HS512':
case 'HS384':
Expand Down Expand Up @@ -1192,12 +1222,19 @@ protected function validateIssuer(string $iss): bool
protected function verifyJWTClaims($claims, string $accessToken = null): bool
{
if(isset($claims->at_hash, $accessToken)) {
if(isset($this->getIdTokenHeader()->alg) && $this->getIdTokenHeader()->alg !== 'none') {
$bit = substr($this->getIdTokenHeader()->alg, 2, 3);
} else {
// TODO: Error case. throw exception???
$bit = '256';
switch($this->getIdTokenHeader()->alg ?? '') {
case 'EdDSA':
$bit = '512';
break;
case 'none':
case '':
// TODO: Error case. throw exception???
$bit = '256';
break;
default:
$bit = substr($this->getIdTokenHeader()->alg, 2, 3);
}

$len = ((int)$bit)/16;
$expected_at_hash = $this->urlEncode(substr(hash('sha'.$bit, $accessToken, true), 0, $len));
}
Expand Down