Skip to content

Commit

Permalink
adds nonce support
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKubovic committed May 27, 2019
1 parent f48d907 commit 79cc5cf
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
55 changes: 55 additions & 0 deletions Grant/AuthCodeGrant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Trikoder\Bundle\OAuth2Bundle\Grant;

use DateInterval;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Grant\AuthCodeGrant as BaseAuthCodeGrant;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
use Psr\Http\Message\ServerRequestInterface;
use Trikoder\Bundle\OAuth2Bundle\League\Repository\AuthCodeRepository;
use Trikoder\Bundle\OAuth2Bundle\OpenIDConnect\IdTokenResponse;

/**
* @property-read AuthCodeRepository $authCodeRepository
*/
class AuthCodeGrant extends BaseAuthCodeGrant
{
/** @var string|null */
private $nonce;

public function validateAuthorizationRequest(ServerRequestInterface $request)
{
$authorizationRequest = parent::validateAuthorizationRequest($request);

$this->nonce = $this->getQueryStringParameter('nonce', $request, null);

return $authorizationRequest;
}

protected function issueAuthCode(DateInterval $authCodeTTL, ClientEntityInterface $client, $userIdentifier, $redirectUri, array $scopes = [])
{
$autCode = parent::issueAuthCode($authCodeTTL, $client, $userIdentifier, $redirectUri, $scopes);

if ($this->nonce !== null) {
$this->authCodeRepository->updateWithNonce($autCode, $this->nonce);
}

return $autCode;
}

public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseTypeInterface $responseType, DateInterval $accessTokenTTL)
{
$response = parent::respondToAccessTokenRequest($request, $responseType, $accessTokenTTL);

if ($response instanceof IdTokenResponse) {
$encryptedAuthCode = $this->getRequestParameter('code', $request, null);
$authCodePayload = json_decode($this->decrypt($encryptedAuthCode));

$nonce = $this->authCodeRepository->getNonce($authCodePayload->auth_code_id);
$response->setNonce($nonce);
}

return $response;
}
}
20 changes: 20 additions & 0 deletions League/Repository/AuthCodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCode)
$this->authorizationCodeManager->save($authorizationCode);
}

public function updateWithNonce(AuthCodeEntityInterface $authCode, string $nonce)
{
/** @var AuthorizationCode $authorizationCode */
$authorizationCode = $this->authorizationCodeManager->find($authCode->getIdentifier());

if (null === $authorizationCode) {
throw new \LogicException('You cant update code that wasnt\'t persisted');
}

$authorizationCode->setNonce($nonce);

$this->authorizationCodeManager->save($authorizationCode);
}

public function getNonce(string $authCodeIdentifier)
{
$authCode = $this->authorizationCodeManager->find($authCodeIdentifier);
return $authCode->getNonce();
}

/**
* {@inheritdoc}
*/
Expand Down
17 changes: 17 additions & 0 deletions Model/AuthorizationCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class AuthorizationCode
*/
private $revoked = false;

/** @var string|null */
private $nonce;

public function __construct(
string $identifier,
DateTime $expiry,
Expand Down Expand Up @@ -94,4 +97,18 @@ public function revoke(): self

return $this;
}

public function getNonce(): ?string
{
return $this->nonce;
}

public function setNonce(string $nonce): self
{
if ($this->nonce === null) {
$this->nonce = $nonce;
}

return $this;
}
}
29 changes: 29 additions & 0 deletions OpenIDConnect/IdTokenResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Trikoder\Bundle\OAuth2Bundle\OpenIDConnect;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\UserEntityInterface;
use OpenIDConnectServer\IdTokenResponse as BaseIdTokenResponse;

class IdTokenResponse extends BaseIdTokenResponse
{
/** @var string|null */
private $nonce;

public function setNonce(string $nonce)
{
$this->nonce = $nonce;
}

protected function getBuilder(AccessTokenEntityInterface $accessToken, UserEntityInterface $userEntity)
{
$builder = parent::getBuilder($accessToken, $userEntity);

if (null !== $this->nonce) {
$builder->set('nonce', $this->nonce);
}

return $builder;
}
}
1 change: 1 addition & 0 deletions Resources/config/doctrine/model/AuthorizationCode.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<field name="userIdentifier" type="string" length="128" nullable="true" />
<field name="scopes" type="oauth2_scope" nullable="true" />
<field name="revoked" type="boolean" />
<field name="nonce" type="string" nullable="true" />
<many-to-one field="client" target-entity="Trikoder\Bundle\OAuth2Bundle\Model\Client">
<join-column name="client" referenced-column-name="identifier" nullable="false" />
</many-to-one>
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<service id="league.oauth2.server.grant.refresh_token_grant" class="League\OAuth2\Server\Grant\RefreshTokenGrant">
<argument type="service" id="trikoder.oauth2.league.repository.refresh_token_repository" />
</service>
<service id="league.oauth2.server.grant.auth_code_grant" class="League\OAuth2\Server\Grant\AuthCodeGrant" >
<service id="league.oauth2.server.grant.auth_code_grant" class="Trikoder\Bundle\OAuth2Bundle\Grant\AuthCodeGrant" >
<argument type="service" id="trikoder.oauth2.league.repository.auth_code_repository" />
<argument type="service" id="trikoder.oauth2.league.repository.refresh_token_repository" />
<argument key="$authCodeTTL" />
Expand Down Expand Up @@ -136,7 +136,7 @@
<argument type="service" id="Symfony\Component\EventDispatcher\EventDispatcherInterface" />
</service>
<service id="openid_connect_server.claim_extractor" class="OpenIDConnectServer\ClaimExtractor" />
<service id="openid_connect_server.id_token_response" class="OpenIDConnectServer\IdTokenResponse">
<service id="openid_connect_server.id_token_response" class="Trikoder\Bundle\OAuth2Bundle\OpenIDConnect\IdTokenResponse">
<argument type="service" id="trikoder.oauth2.openid_connect.repository.identity_provider" />
<argument type="service" id="openid_connect_server.claim_extractor" />
</service>
Expand Down

0 comments on commit 79cc5cf

Please sign in to comment.