-
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.
Support private key JWT token endpoint authentication (#39)
* Initial private key jwt builder * Set private key jwt generator in service provider * Use variables for config values * Use correct config key * Add signature algorithms config and add JWK loading * Reformat * Use JWSSerializer interface * Add test * Add jti claim Used the same jti implementation as jumbojett/OpenID-Connect-PHP * Configurable expiration in seconds * Add test for token endpoint request
- Loading branch information
1 parent
e619f01
commit 058238f
Showing
5 changed files
with
350 additions
and
8 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
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MinVWS\OpenIDConnectLaravel\Services\JWS; | ||
|
||
use Jose\Component\Core\JWK; | ||
use Jose\Component\Signature\JWSBuilder; | ||
use Jose\Component\Signature\Serializer\JWSSerializer; | ||
|
||
class PrivateKeyJWTBuilder | ||
{ | ||
public function __construct( | ||
protected string $clientId, | ||
protected JWSBuilder $jwsBuilder, | ||
protected JWK $signatureKey, | ||
protected string $signatureAlgorithm, | ||
protected JWSSerializer $serializer, | ||
protected int $tokenLifetimeInSeconds, | ||
) { | ||
} | ||
|
||
public function __invoke(string $audience): string | ||
{ | ||
return $this->buildJws($this->getPayload($audience)); | ||
} | ||
|
||
protected function getPayload(string $audience): string | ||
{ | ||
$jti = hash('sha256', bin2hex(random_bytes(64))); | ||
$now = time(); | ||
|
||
return json_encode([ | ||
'iss' => $this->clientId, | ||
'sub' => $this->clientId, | ||
'aud' => $audience, | ||
'jti' => $jti, | ||
'exp' => $now + $this->tokenLifetimeInSeconds, | ||
'iat' => $now, | ||
], JSON_THROW_ON_ERROR); | ||
} | ||
|
||
protected function buildJws(string $payload): string | ||
{ | ||
$jws = $this->jwsBuilder | ||
->create() | ||
->withPayload($payload) | ||
->addSignature($this->signatureKey, ['alg' => $this->signatureAlgorithm]) | ||
->build(); | ||
|
||
return $this->serializer->serialize($jws, 0); | ||
} | ||
} |
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
Oops, something went wrong.