Skip to content

Commit

Permalink
Adding support for a nonce
Browse files Browse the repository at this point in the history
According to the OpenID connect spec:

> nonce
> String value used to associate a Client session with an ID Token, and to mitigate replay attacks. The value is passed through unmodified from the Authentication Request to the ID Token. If present in the ID Token, Clients MUST verify that the nonce Claim Value is equal to the value of the nonce parameter sent in the Authentication Request. If present in the Authentication Request, Authorization Servers MUST include a nonce Claim in the ID Token with the Claim Value being the nonce value sent in the Authentication Request. Authorization Servers SHOULD perform no other processing on nonce values used. The nonce value is a case-sensitive string.

Right now, if a client passes a "nounce", we don't give it back and
the client fails. This is happening to me right now with the client
from Matrix Synapse.

Here, I'm creating a new service (`CurrentRequestService`).
With this new service, I can get the current PSR-7 request.

I extend the AuthCodeGrant and inject this service into the extended class.
With this, I can:

- read the "nonce" from the request
- encode the "nonce" in the "code"

Then, in the `IdTokenResponse`, I read the "code" (if it is present),
extract the "nounce" and inject it in the ID token as a new claim.

The whole process is inspired by this comment: steverhoades/oauth2-openid-connect-server#47 (comment)

With those changes, nounce is correctly handled and I've successfully
tested a connection with the OpenID client from Matrix Synapse.
  • Loading branch information
moufmouf committed Jan 19, 2024
1 parent 3be9e08 commit 425632e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ To enable OpenID Connect, follow these simple steps
```php
$privateKeyPath = 'tmp/private.key';

$currentRequestService = new CurrentRequestService();
$currentRequestService->setRequest(ServerRequestFactory::fromGlobals());

// create the response_type
$responseType = new IdTokenResponse(
new IdentityRepository(),
Expand All @@ -44,6 +47,8 @@ $responseType = new IdTokenResponse(
new Sha256(),
InMemory::file($privateKeyPath),
),
$currentRequestService,
$encryptionKey,
);

$server = new \League\OAuth2\Server\AuthorizationServer(
Expand All @@ -62,6 +67,17 @@ Provide more scopes (e.g. `openid profile email`) to receive additional claims i

For a complete implementation, visit [the OAuth2 Server example](https://github.com/ronvanderheijden/openid-connect/tree/main/example).

## Nonce support

To prevent replay attacks, some clients can provide a "nonce" in the authorization request. If a client does so, the
server MUST include back a `nonce` claim in the `id_token`.

To enable this feature, when registering an AuthCodeGrant, you need to use the `\OpenIDConnect\Grant\AuthCodeGrant`
instead of `\League\OAuth2\Server\Grant\AuthCodeGrant`.

> ![NOTE]
> If you are using Laravel, the `AuthCodeGrant` is already registered for you by the service provider.
## Laravel Passport

You can use this package with Laravel Passport in 2 simple steps.
Expand Down
36 changes: 32 additions & 4 deletions src/IdTokenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,43 @@

use DateInterval;
use DateTimeImmutable;
use Defuse\Crypto\Key;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Configuration;
use League\OAuth2\Server\CryptTrait;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use OpenIDConnect\Interfaces\CurrentRequestServiceInterface;
use OpenIDConnect\Interfaces\IdentityEntityInterface;
use OpenIDConnect\Interfaces\IdentityRepositoryInterface;

class IdTokenResponse extends BearerTokenResponse
{
use CryptTrait;

protected IdentityRepositoryInterface $identityRepository;

protected ClaimExtractor $claimExtractor;

private Configuration $config;
private ?string $issuer;
private ?CurrentRequestServiceInterface $currentRequestService;

/**
* @param string|Key|null $encryptionKey
*/
public function __construct(
IdentityRepositoryInterface $identityRepository,
ClaimExtractor $claimExtractor,
Configuration $config,
string $issuer = null,
CurrentRequestServiceInterface $currentRequestService = null,
$encryptionKey = null,
) {
$this->identityRepository = $identityRepository;
$this->claimExtractor = $claimExtractor;
$this->config = $config;
$this->issuer = $issuer;
$this->currentRequestService = $currentRequestService;
$this->encryptionKey = $encryptionKey;
}

protected function getBuilder(
Expand All @@ -41,10 +51,17 @@ protected function getBuilder(
): Builder {
$dateTimeImmutableObject = new DateTimeImmutable();

if ($this->currentRequestService) {
$uri = $this->currentRequestService->getRequest()->getUri();
$issuer = $uri->getScheme() . '://' . $uri->getHost() . ($uri->getPort() ? ':' . $uri->getPort() : '');
} else {
$issuer = 'https://' . $_SERVER['HTTP_HOST'];
}

return $this->config
->builder()
->permittedFor($accessToken->getClient()->getIdentifier())
->issuedBy($this->issuer ?? 'https://' . $_SERVER['HTTP_HOST'])
->issuedBy($issuer)
->issuedAt($dateTimeImmutableObject)
->expiresAt($dateTimeImmutableObject->add(new DateInterval('PT1H')))
->relatedTo($userEntity->getIdentifier());
Expand All @@ -71,6 +88,17 @@ protected function getExtraParams(AccessTokenEntityInterface $accessToken): arra
$builder = $builder->withClaim($claimName, $claimValue);
}

if ($this->currentRequestService) {
// If the request contains a code, we look into the code to find the nonce.
$body = $this->currentRequestService->getRequest()->getParsedBody();
if (isset($body['code'])) {
$authCodePayload = json_decode($this->decrypt($body['code']), true, 512, JSON_THROW_ON_ERROR);
if (isset($authCodePayload['nonce'])) {
$builder = $builder->withClaim('nonce', $authCodePayload['nonce']);
}
}
}

$token = $builder->getToken(
$this->config->signer(),
$this->config->signingKey(),
Expand Down
25 changes: 23 additions & 2 deletions src/Laravel/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\CryptTrait;
use Nyholm\Psr7\Response;
use OpenIDConnect\ClaimExtractor;
use OpenIDConnect\Claims\ClaimSet;
use OpenIDConnect\Grant\AuthCodeGrant;
use OpenIDConnect\IdTokenResponse;

class PassportServiceProvider extends Passport\PassportServiceProvider
Expand Down Expand Up @@ -45,6 +48,7 @@ public function boot()
public function makeAuthorizationServer(): AuthorizationServer
{
$cryptKey = $this->makeCryptKey('private');
$encryptionKey = app(Encrypter::class)->getKey();

$responseType = new IdTokenResponse(
app(config('openid.repositories.identity')),
Expand All @@ -53,19 +57,36 @@ public function makeAuthorizationServer(): AuthorizationServer
app(config('openid.signer')),
InMemory::file($cryptKey->getKeyPath()),
),
app('request')->getSchemeAndHttpHost(),
app(LaravelCurrentRequestService::class),
$encryptionKey,
);

return new AuthorizationServer(
app(ClientRepository::class),
app(AccessTokenRepository::class),
app(config('openid.repositories.scope')),
$cryptKey,
app(Encrypter::class)->getKey(),
$encryptionKey,
$responseType,
);
}

/**
* Build the Auth Code grant instance.
*
* @return AuthCodeGrant
*/
protected function buildAuthCodeGrant()
{
return new AuthCodeGrant(
$this->app->make(Passport\Bridge\AuthCodeRepository::class),
$this->app->make(Passport\Bridge\RefreshTokenRepository::class),
new \DateInterval('PT10M'),
new Response(),
$this->app->make(LaravelCurrentRequestService::class),
);
}

public function registerClaimExtractor() {
$this->app->singleton(ClaimExtractor::class, function () {
$customClaimSets = config('openid.custom_claim_sets');
Expand Down

0 comments on commit 425632e

Please sign in to comment.