forked from jumbojett/OpenID-Connect-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
private $wellKnownConfigParameters = []; | ||
|
||
|
@@ -686,7 +686,7 @@ public function getRedirectURL(): string | |
} else { | ||
$protocol = 'http'; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'])) { | ||
|
@@ -709,7 +709,7 @@ public function getRedirectURL(): string | |
} | ||
|
||
$port = (443 === $port) || (80 === $port) ? '' : ':' . $port; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), '/')); | ||
} | ||
|
@@ -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); | ||
|
@@ -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': | ||
|
@@ -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)); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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