Skip to content
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

added possibility to inject custom encryption object for IdToken response type #1057

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/OAuth2/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use OAuth2\Controller\ResourceControllerInterface;
use OAuth2\Controller\ResourceController;
use OAuth2\Encryption\EncryptionInterface;
use OAuth2\Encryption\Jwt;
use OAuth2\OpenID\Controller\UserInfoControllerInterface;
use OAuth2\OpenID\Controller\UserInfoController;
use OAuth2\OpenID\Controller\AuthorizeController as OpenIDAuthorizeController;
Expand Down Expand Up @@ -110,6 +112,11 @@ class Server implements ResourceControllerInterface,
*/
protected $clientAssertionType;

/**
* @var EncryptionInterface
*/
protected $encryptionUtil;

/**
* @var array
*/
Expand Down Expand Up @@ -844,7 +851,7 @@ protected function createDefaultJwtAccessTokenResponseType()

$config = array_intersect_key($this->config, array_flip(explode(' ', 'store_encrypted_token_string issuer access_lifetime refresh_token_lifetime jwt_extra_payload_callable')));

return new JwtAccessToken($this->storages['public_key'], $tokenStorage, $refreshStorage, $config);
return new JwtAccessToken($this->storages['public_key'], $tokenStorage, $refreshStorage, $config, $this->getEncryptionUtil());
}

/**
Expand Down Expand Up @@ -883,7 +890,7 @@ protected function createDefaultIdTokenResponseType()

$config = array_intersect_key($this->config, array_flip(explode(' ', 'issuer id_lifetime')));

return new IdToken($this->storages['user_claims'], $this->storages['public_key'], $config);
return new IdToken($this->storages['user_claims'], $this->storages['public_key'], $config, $this->getEncryptionUtil());
}

/**
Expand Down Expand Up @@ -1017,4 +1024,24 @@ public function getConfig($name, $default = null)
{
return isset($this->config[$name]) ? $this->config[$name] : $default;
}

/**
* @return EncryptionInterface
*/
private function getEncryptionUtil(): EncryptionInterface
{
if (empty($this->encryptionUtil)) {
$this->encryptionUtil = new Jwt();
}
return $this->encryptionUtil;
}

/**
* @param EncryptionInterface $encryptionUtil
* @return void
*/
public function setEncryptionUtil(EncryptionInterface $encryptionUtil): void
{
$this->encryptionUtil = $encryptionUtil;
}
}
43 changes: 43 additions & 0 deletions test/OAuth2/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace OAuth2;

use OAuth2\Encryption\EncryptionInterface;
use OAuth2\OpenID\ResponseType\IdToken;
use OAuth2\Request\TestRequest;
use OAuth2\ResponseType\AuthorizationCode;
use OAuth2\ResponseType\JwtAccessToken;
use OAuth2\Storage\Bootstrap;
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
Expand Down Expand Up @@ -646,4 +649,44 @@ public function testAddGrantTypeWithKeyNotString()
$grantTypes = $server->getGrantTypes();
$this->assertEquals('authorization_code', key($grantTypes));
}

public function testUsingCustomEncryptionObjectForIdToken()
{
$client = $this->createMock('OAuth2\Storage\ClientInterface');
$userclaims = $this->createMock('OAuth2\OpenID\Storage\UserClaimsInterface');
$pubkey = $this->createMock('OAuth2\Storage\PublicKeyInterface');
$server = new Server(array($client, $userclaims, $pubkey), array(
'use_openid_connect' => true,
'issuer' => 'someguy',
));

$stub = $this->createStub(EncryptionInterface::class);
$stub->method('encode')->willReturn('mocked-encryption');
$server->setEncryptionUtil($stub);

$server->getAuthorizeController();

$responseType = $server->getResponseType('id_token');
/* @var IdToken $responseType*/
$this->assertEquals('mocked-encryption', $responseType->createIdToken('unit-tests', 'dummy-user'));
}

public function testUsingCustomEncryptionObjectForJwtAccessToken()
{
$pubkey = $this->createMock('OAuth2\Storage\PublicKeyInterface');
$client = $this->createMock('OAuth2\Storage\ClientInterface');
$server = new Server(array($pubkey, $client), array('use_jwt_access_tokens' => true, 'allow_implicit' => true));

$stub = $this->createStub(EncryptionInterface::class);
$stub->method('encode')->willReturn('mocked-encryption-access-token');
$server->setEncryptionUtil($stub);

$server->getAuthorizeController();

$responseType = $server->getResponseType('token');
/* @var JwtAccessToken $responseType*/
$accessToken = $responseType->createAccessToken('unit-tests', 'dummy-user');
$this->assertArrayHasKey('access_token', $accessToken);
$this->assertEquals('mocked-encryption-access-token', $accessToken['access_token']);
}
}