Skip to content

Commit

Permalink
Update idtoken (#42)
Browse files Browse the repository at this point in the history
* Add signer configuration option. Default algorithm is RS256

* Add ISS
  • Loading branch information
sgomez authored May 17, 2020
1 parent bc58529 commit 2f44424
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
6 changes: 6 additions & 0 deletions config-templates/module_oidc.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
// Tag to run storage cleanup script using the cron module...
'cron_tag' => 'hourly',

// Set token signer
// See Lcobucci\JWT\Signer algorithms in https://github.com/lcobucci/jwt/tree/master/src/Signer
'signer' => \Lcobucci\JWT\Signer\Rsa\Sha256::class,
// 'signer' => \Lcobucci\JWT\Signer\Hmac\Sha256::class,
// 'signer' => \Lcobucci\JWT\Signer\Ecdsa\Sha256::class,

// this is the auth source used for authentication,
'auth' => 'default-sp',
// useridattr is the attribute-name that contains the userid as returned from idp
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/ClientEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function __invoke(ServerRequest $request)
$client = $this->getClientFromRequest($request);

$form = $this->formFactory->build(ClientForm::class);
$formAction = sprintf("%s/clients/edit.php?client_id=%s",
$formAction = sprintf(
"%s/clients/edit.php?client_id=%s",
$this->configurationService->getOpenIdConnectModuleURL(),
$client->getIdentifier()
) ;
Expand Down
12 changes: 2 additions & 10 deletions lib/Factories/IdTokenResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

namespace SimpleSAML\Modules\OpenIDConnect\Factories;

use Lcobucci\JWT\Builder;
use SimpleSAML\Modules\OpenIDConnect\ClaimTranslatorExtractor;
use SimpleSAML\Modules\OpenIDConnect\Repositories\UserRepository;
use SimpleSAML\Modules\OpenIDConnect\Server\ResponseTypes\IdTokenResponse;
Expand Down Expand Up @@ -49,17 +48,10 @@ public function __construct(

public function build(): IdTokenResponse
{
$builder = (new Builder())
->issuedBy($this->configurationService->getSimpleSAMLSelfURLHost())
->withHeader('kid', 'oidc')
;

$token = new IdTokenResponse(
return new IdTokenResponse(
$this->userRepository,
$this->claimTranslatorExtractor,
$builder
$this->configurationService
);

return $token;
}
}
36 changes: 23 additions & 13 deletions lib/Server/ResponseTypes/IdTokenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Entities\UserEntityInterface;
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use OpenIDConnectServer\ClaimExtractor;
use OpenIDConnectServer\Entities\ClaimSetInterface;
use OpenIDConnectServer\Repositories\IdentityProviderInterface;
use SimpleSAML\Modules\OpenIDConnect\Services\ConfigurationService;

/**
* Class IdTokenResponse.
Expand All @@ -45,18 +45,18 @@ class IdTokenResponse extends BearerTokenResponse
*/
protected $claimExtractor;
/**
* @var Builder
* @var ConfigurationService
*/
private $builder;
private $configurationService;

public function __construct(
IdentityProviderInterface $identityProvider,
ClaimExtractor $claimExtractor,
Builder $builder
ConfigurationService $configurationService
) {
$this->identityProvider = $identityProvider;
$this->claimExtractor = $claimExtractor;
$this->builder = $builder;
$this->configurationService = $configurationService;
}

/**
Expand All @@ -78,12 +78,7 @@ protected function getExtraParams(AccessTokenEntityInterface $accessToken)
}

// Add required id_token claims
$builder = $this->builder
->permittedFor($accessToken->getClient()->getIdentifier())
->issuedAt(time())
->expiresAt($accessToken->getExpiryDateTime()->getTimestamp())
->relatedTo($userEntity->getIdentifier())
;
$builder = $this->getBuilder($accessToken, $userEntity);

// Need a claim factory here to reduce the number of claims by provided scope.
$claims = $this->claimExtractor->extract($accessToken->getScopes(), $userEntity->getClaims());
Expand All @@ -92,14 +87,29 @@ protected function getExtraParams(AccessTokenEntityInterface $accessToken)
$builder->withClaim($claimName, $claimValue);
}

$key = new Key($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase());
$token = $builder->getToken(new Sha256(), $key);
$token = $builder->getToken(
$this->configurationService->getSigner(),
new Key($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase())
);

return [
'id_token' => (string) $token,
];
}

protected function getBuilder(AccessTokenEntityInterface $accessToken, UserEntityInterface $userEntity)
{
return (new Builder())
->issuedBy($this->configurationService->getSimpleSAMLSelfURLHost())
->permittedFor($accessToken->getClient()->getIdentifier())
->identifiedBy($accessToken->getIdentifier())
->canOnlyBeUsedAfter(\time())
->expiresAt($accessToken->getExpiryDateTime()->getTimestamp())
->relatedTo($userEntity->getIdentifier())
->issuedAt(\time())
->withHeader('kid', '0');
}

/**
* @param ScopeEntityInterface[] $scopes
*
Expand Down
17 changes: 17 additions & 0 deletions lib/Services/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace SimpleSAML\Modules\OpenIDConnect\Services;

use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use SimpleSAML\Configuration;
use SimpleSAML\Error\ConfigurationError;
use SimpleSAML\Module;
Expand Down Expand Up @@ -118,4 +120,19 @@ function ($scope, $name) {
}
);
}

public function getSigner(): Signer
{
/** @psalm-var class-string $signerClassname */
$signerClassname = (string) $this->getOpenIDConnectConfiguration()->getString('signer', Sha256::class);

$class = new \ReflectionClass($signerClassname);
$signer = $class->newInstance();

if (!$signer instanceof Signer) {
return new Sha256();
}

return $signer;
}
}

0 comments on commit 2f44424

Please sign in to comment.