-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verifications OIDC (idtoken et nonce)
- Loading branch information
1 parent
6543eec
commit 82f83c7
Showing
7 changed files
with
391 additions
and
250 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace MonIndemnisationJustice\Security\Jwt; | ||
|
||
enum JwkKeyType: string | ||
{ | ||
case RSA = 'RSA'; | ||
case EC = 'EC'; | ||
} | ||
|
||
enum JwkUseType: string | ||
{ | ||
case SIG = 'sig'; | ||
case ENC = 'enc'; | ||
} | ||
|
||
enum JwkEncryptionAlgorithm: string | ||
{ | ||
case ES256 = 'ES256'; | ||
case RS256 = 'RS256'; | ||
} | ||
|
||
/** | ||
* JSON Web Key (see [official RFC](https://datatracker.ietf.org/doc/html/rfc7517)). | ||
*/ | ||
class Jwk | ||
{ | ||
/** | ||
* @var string the Key Type parameter | ||
*/ | ||
public readonly JwkKeyType $kty; | ||
public readonly JwkUseType $use; | ||
public readonly JwkEncryptionAlgorithm $alg; | ||
/** | ||
* @var string the key ID | ||
*/ | ||
public readonly string $kid; | ||
public readonly array $data; | ||
|
||
public static function fromArray(array $values): Jwk | ||
{ | ||
$jwk = new Jwk(); | ||
$jwk->kty = JwkKeyType::from($values['kty']); | ||
$jwk->use = JwkUseType::from($values['use']); | ||
$jwk->alg = JwkEncryptionAlgorithm::from($values['alg']); | ||
$jwk->kid = $values['kid']; | ||
// Send as key data the other values | ||
$jwk->data = array_filter($values, function ($k) { | ||
return !in_array($k, ['kty', 'use', 'alg', 'kid']); | ||
}, ARRAY_FILTER_USE_KEY); | ||
|
||
return $jwk; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace MonIndemnisationJustice\Security\Jwt; | ||
|
||
use Firebase\JWT\JWK as FirebaseJWK; | ||
use Firebase\JWT\JWT as FirebaseJWT; | ||
use Firebase\JWT\SignatureInvalidException; | ||
|
||
class Jwt | ||
{ | ||
protected readonly string $value; | ||
protected readonly array $header; | ||
protected readonly array $payload; | ||
protected readonly string $message; | ||
protected readonly string $signature; | ||
|
||
public function __construct( | ||
string $value, | ||
) { | ||
$this->value = $value; | ||
list($header, $payload, $signature) = explode('.', $value); | ||
|
||
$this->header = json_decode(base64_decode(urldecode($header)), true); | ||
$this->payload = json_decode(base64_decode(urldecode($payload)), true); | ||
$this->message = "{$header}.{$payload}"; | ||
$this->signature = $signature; | ||
} | ||
|
||
public function getPayload(): array | ||
{ | ||
return $this->payload; | ||
} | ||
|
||
public function getValue(string $name): mixed | ||
{ | ||
return $this->payload[$name] ?? null; | ||
} | ||
|
||
protected function extractJwkForAlgo(array $jwks, string $kid) | ||
{ | ||
$key = array_search($kid, array_column($jwks, 'kid')); | ||
|
||
return $jwks[$key]; | ||
} | ||
|
||
protected function buildPem(array $jwk): string | ||
{ | ||
// Ça ne fonctionne pas, voire vendor/firebase/php-jwt/src/JWK.php:231 | ||
|
||
return "-----BEGIN PUBLIC KEY-----\n". | ||
chunk_split(base64_encode('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA'.strtr($jwk['n'].'ID'.$jwk['e'], '-_', '+/')), 64). | ||
'-----END PUBLIC KEY-----'; | ||
} | ||
|
||
public function verify(array $jwks): bool | ||
{ | ||
$jwk = $this->extractJwkForAlgo($jwks, $this->header['kid']); | ||
|
||
try { | ||
/* | ||
return 1 === openssl_verify( | ||
$this->message, | ||
base64_decode($this->signature), | ||
$this->buildPem($jwk)) | ||
OPENSSL_ALGO_SHA256 | ||
); */ | ||
FirebaseJWT::decode($this->value, FirebaseJWK::parseKey($jwk)); | ||
|
||
return true; | ||
} catch (SignatureInvalidException) { | ||
return false; | ||
} | ||
} | ||
|
||
private static function base64Encode(string $value): string | ||
{ | ||
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($value)); | ||
} | ||
|
||
public static function parse(string $jwt): Jwt | ||
{ | ||
return new Jwt($jwt); | ||
} | ||
} |
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