Skip to content

Commit 2f44424

Browse files
authored
Update idtoken (#42)
* Add signer configuration option. Default algorithm is RS256 * Add ISS
1 parent bc58529 commit 2f44424

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

config-templates/module_oidc.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
// Tag to run storage cleanup script using the cron module...
2525
'cron_tag' => 'hourly',
2626

27+
// Set token signer
28+
// See Lcobucci\JWT\Signer algorithms in https://github.com/lcobucci/jwt/tree/master/src/Signer
29+
'signer' => \Lcobucci\JWT\Signer\Rsa\Sha256::class,
30+
// 'signer' => \Lcobucci\JWT\Signer\Hmac\Sha256::class,
31+
// 'signer' => \Lcobucci\JWT\Signer\Ecdsa\Sha256::class,
32+
2733
// this is the auth source used for authentication,
2834
'auth' => 'default-sp',
2935
// useridattr is the attribute-name that contains the userid as returned from idp

lib/Controller/ClientEditController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public function __invoke(ServerRequest $request)
7171
$client = $this->getClientFromRequest($request);
7272

7373
$form = $this->formFactory->build(ClientForm::class);
74-
$formAction = sprintf("%s/clients/edit.php?client_id=%s",
74+
$formAction = sprintf(
75+
"%s/clients/edit.php?client_id=%s",
7576
$this->configurationService->getOpenIdConnectModuleURL(),
7677
$client->getIdentifier()
7778
) ;

lib/Factories/IdTokenResponseFactory.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
namespace SimpleSAML\Modules\OpenIDConnect\Factories;
1616

17-
use Lcobucci\JWT\Builder;
1817
use SimpleSAML\Modules\OpenIDConnect\ClaimTranslatorExtractor;
1918
use SimpleSAML\Modules\OpenIDConnect\Repositories\UserRepository;
2019
use SimpleSAML\Modules\OpenIDConnect\Server\ResponseTypes\IdTokenResponse;
@@ -49,17 +48,10 @@ public function __construct(
4948

5049
public function build(): IdTokenResponse
5150
{
52-
$builder = (new Builder())
53-
->issuedBy($this->configurationService->getSimpleSAMLSelfURLHost())
54-
->withHeader('kid', 'oidc')
55-
;
56-
57-
$token = new IdTokenResponse(
51+
return new IdTokenResponse(
5852
$this->userRepository,
5953
$this->claimTranslatorExtractor,
60-
$builder
54+
$this->configurationService
6155
);
62-
63-
return $token;
6456
}
6557
}

lib/Server/ResponseTypes/IdTokenResponse.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
use Lcobucci\JWT\Builder;
1818
use Lcobucci\JWT\Signer\Key;
19-
use Lcobucci\JWT\Signer\Rsa\Sha256;
2019
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
2120
use League\OAuth2\Server\Entities\ScopeEntityInterface;
2221
use League\OAuth2\Server\Entities\UserEntityInterface;
2322
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
2423
use OpenIDConnectServer\ClaimExtractor;
2524
use OpenIDConnectServer\Entities\ClaimSetInterface;
2625
use OpenIDConnectServer\Repositories\IdentityProviderInterface;
26+
use SimpleSAML\Modules\OpenIDConnect\Services\ConfigurationService;
2727

2828
/**
2929
* Class IdTokenResponse.
@@ -45,18 +45,18 @@ class IdTokenResponse extends BearerTokenResponse
4545
*/
4646
protected $claimExtractor;
4747
/**
48-
* @var Builder
48+
* @var ConfigurationService
4949
*/
50-
private $builder;
50+
private $configurationService;
5151

5252
public function __construct(
5353
IdentityProviderInterface $identityProvider,
5454
ClaimExtractor $claimExtractor,
55-
Builder $builder
55+
ConfigurationService $configurationService
5656
) {
5757
$this->identityProvider = $identityProvider;
5858
$this->claimExtractor = $claimExtractor;
59-
$this->builder = $builder;
59+
$this->configurationService = $configurationService;
6060
}
6161

6262
/**
@@ -78,12 +78,7 @@ protected function getExtraParams(AccessTokenEntityInterface $accessToken)
7878
}
7979

8080
// Add required id_token claims
81-
$builder = $this->builder
82-
->permittedFor($accessToken->getClient()->getIdentifier())
83-
->issuedAt(time())
84-
->expiresAt($accessToken->getExpiryDateTime()->getTimestamp())
85-
->relatedTo($userEntity->getIdentifier())
86-
;
81+
$builder = $this->getBuilder($accessToken, $userEntity);
8782

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

95-
$key = new Key($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase());
96-
$token = $builder->getToken(new Sha256(), $key);
90+
$token = $builder->getToken(
91+
$this->configurationService->getSigner(),
92+
new Key($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase())
93+
);
9794

9895
return [
9996
'id_token' => (string) $token,
10097
];
10198
}
10299

100+
protected function getBuilder(AccessTokenEntityInterface $accessToken, UserEntityInterface $userEntity)
101+
{
102+
return (new Builder())
103+
->issuedBy($this->configurationService->getSimpleSAMLSelfURLHost())
104+
->permittedFor($accessToken->getClient()->getIdentifier())
105+
->identifiedBy($accessToken->getIdentifier())
106+
->canOnlyBeUsedAfter(\time())
107+
->expiresAt($accessToken->getExpiryDateTime()->getTimestamp())
108+
->relatedTo($userEntity->getIdentifier())
109+
->issuedAt(\time())
110+
->withHeader('kid', '0');
111+
}
112+
103113
/**
104114
* @param ScopeEntityInterface[] $scopes
105115
*

lib/Services/ConfigurationService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace SimpleSAML\Modules\OpenIDConnect\Services;
1616

17+
use Lcobucci\JWT\Signer;
18+
use Lcobucci\JWT\Signer\Rsa\Sha256;
1719
use SimpleSAML\Configuration;
1820
use SimpleSAML\Error\ConfigurationError;
1921
use SimpleSAML\Module;
@@ -118,4 +120,19 @@ function ($scope, $name) {
118120
}
119121
);
120122
}
123+
124+
public function getSigner(): Signer
125+
{
126+
/** @psalm-var class-string $signerClassname */
127+
$signerClassname = (string) $this->getOpenIDConnectConfiguration()->getString('signer', Sha256::class);
128+
129+
$class = new \ReflectionClass($signerClassname);
130+
$signer = $class->newInstance();
131+
132+
if (!$signer instanceof Signer) {
133+
return new Sha256();
134+
}
135+
136+
return $signer;
137+
}
121138
}

0 commit comments

Comments
 (0)