Skip to content

Commit 6c8b3cb

Browse files
committed
Let the OpenID4VCI issuer conformance flows through: wallet-initiated requests, offerless issuance, PAR after login
Wallet-initiated VCI authorization code requests (a credential configuration scope or openid_credential authorization details, while VCI is enabled) no longer need the openid scope; the non-registered client fallback still requires an issuer_state. The credential endpoint accepts a token without an issuer_state from a registered client, and requires the requested credential to be among the token's scopes. A PAR request_uri is consumed when the authorization response is issued instead of on first validation, so the request validated again after the login succeeds, and expired PAR rows are kept for session.state.timeout. The issuer display in the credential issuer metadata leaves out what is not configured. The conformance harness gives VCI client 2 the suite's query-string redirect URIs and an EC key.
1 parent 6b2b790 commit 6c8b3cb

23 files changed

Lines changed: 1052 additions & 102 deletions

‎conformance-tests/conformance-vci-issuer.json‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
"jwks": {
2828
"keys": [
2929
{
30-
"kty": "RSA",
31-
"d": "Nha1ReQVpOa1vsTz0oEcbtCd2UAbYWXhLr06CXXENXxDHWhTkXCaayh4kEOp6CNfY5b75tbBZB2hhxyY7vPNSmnvrIHn1YE5a321vHEti0GoNI9UGi2KhAO40Qu0ZPqvYCo3nfRsS-CWUWzchcKlqwiwuIwXOWPEGTnuKf-iqsqm6zNXB6zkIXb0Qkm5ByyGiF_wgOJLdlEx9hHlmHPiT_RKYbsQQ4mNqwOVnkInttzPiWmfY1cYN4qlxMYtIF7vb1pz1eWjZLlQpM37qQSxNXVeKNIUrs3NRyP-VDv47YFWDRLaPEPnnS7Y5RwJgeydTPHfZ91C2Q_m79KMGHSqkQ",
32-
"e": "AQAB",
33-
"kid": "fapi-jwt-assertion-20180817-2",
34-
"alg": "PS256",
35-
"n": "kne7a8IYQR6jweqpHAplq-XRGOuiVyF5Siy6_647OhOC8ppRIMV2O_wP6qK1AKCFb78Bb8qbRI3Mz-Tr9hCWm1BZQkD-HGbNowjVsOj7oB2nbNbGfqciTyT3kTG1f5PmeX2N4-f9zZM-J4Jmi9PdMjn2fkNl9oMCW9XaLHHzCU6f-vYftxdCnVQD7ZKr40HjoAeXjwdGhgzvuWSZHkhEqx_QMh8JskqP46PjsMykFWiryju9balCdS5yASf-Fno8pXMFEV1wgipy-FPlhB5FZtLwVvH9F2jAxRaWkRQzhM5hWugIUi8YobjoIwhrmJ04JTK-DGOlThJsNvS4QANDZw"
30+
"kty": "EC",
31+
"d": "0uJtKib8z1Mql7OEL7-FWibQ2rMBhyK14fFtYvgWANA",
32+
"use": "sig",
33+
"crv": "P-256",
34+
"kid": "vci-conformance-client-2",
35+
"x": "rPyeMbaj5YghbJLlBI4XUTi1bNazW4MJN5H5Na1UDpc",
36+
"y": "mLSi9IXjY1Tdf8qZy28FEnnhBpzkskeSKuUUQ5XBmMo",
37+
"alg": "ES256"
3638
}
3739
]
3840
}

‎docker/conformance-vci.sql‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
-- other relies on, and they answer to their own conformance suite alias, simplesamlphp-module-oidc-vci,
55
-- which is the alias in conformance-tests/conformance-vci-issuer.json. The suite authenticates both with
66
-- private_key_jwt, signing with the private halves of these keys held in that file; the secret only
7-
-- satisfies the schema. The one scope is the one the credential configuration in docker/ssp/module_oidc.php
8-
-- names, which is what the suite requests.
7+
-- satisfies the schema. Both keys are EC P-256: the suite also signs its credential key proofs with the
8+
-- client's key, and reads that key as an EC key. The one scope is the one the credential configuration in docker/ssp/module_oidc.php
9+
-- names, which is what the suite requests. The suite sends client 2's requests from the callback with a
10+
-- query string added (?dummy1=lorem&dummy2=ipsum), to check that it is compared exactly, so client 2 has
11+
-- those redirect URIs too.
912
INSERT INTO oidc_client (id, secret, name, description, auth_source, redirect_uri, scopes, is_enabled, is_confidential, jwks, registration_type)
1013
VALUES (
1114
'_3eaf06703ec3a31c4b5b5577427f28ee5e76e83f5f',
@@ -27,10 +30,10 @@ VALUES (
2730
'VCI Conformance Client 2',
2831
'Client 2 for the OpenID4VCI issuer test plan, used by its multiple clients test',
2932
'example-userpass',
30-
'["https:\/\/localhost.emobix.co.uk:8443\/test\/a\/simplesamlphp-module-oidc-vci\/callback","https:\/\/www.certification.openid.net\/test\/a\/simplesamlphp-module-oidc-vci\/callback"]',
33+
'["https:\/\/localhost.emobix.co.uk:8443\/test\/a\/simplesamlphp-module-oidc-vci\/callback","https:\/\/localhost.emobix.co.uk:8443\/test\/a\/simplesamlphp-module-oidc-vci\/callback?dummy1=lorem&dummy2=ipsum","https:\/\/www.certification.openid.net\/test\/a\/simplesamlphp-module-oidc-vci\/callback","https:\/\/www.certification.openid.net\/test\/a\/simplesamlphp-module-oidc-vci\/callback?dummy1=lorem&dummy2=ipsum"]',
3134
'["ResearchAndScholarshipCredentialDcSdJwt"]',
3235
1,
3336
1,
34-
'{"keys":[{"kty":"RSA","e":"AQAB","kid":"fapi-jwt-assertion-20180817-2","alg":"PS256","n":"kne7a8IYQR6jweqpHAplq-XRGOuiVyF5Siy6_647OhOC8ppRIMV2O_wP6qK1AKCFb78Bb8qbRI3Mz-Tr9hCWm1BZQkD-HGbNowjVsOj7oB2nbNbGfqciTyT3kTG1f5PmeX2N4-f9zZM-J4Jmi9PdMjn2fkNl9oMCW9XaLHHzCU6f-vYftxdCnVQD7ZKr40HjoAeXjwdGhgzvuWSZHkhEqx_QMh8JskqP46PjsMykFWiryju9balCdS5yASf-Fno8pXMFEV1wgipy-FPlhB5FZtLwVvH9F2jAxRaWkRQzhM5hWugIUi8YobjoIwhrmJ04JTK-DGOlThJsNvS4QANDZw"}]}',
37+
'{"keys":[{"kty":"EC","use":"sig","crv":"P-256","kid":"vci-conformance-client-2","x":"rPyeMbaj5YghbJLlBI4XUTi1bNazW4MJN5H5Na1UDpc","y":"mLSi9IXjY1Tdf8qZy28FEnnhBpzkskeSKuUUQ5XBmMo","alg":"ES256"}]}',
3538
'manual'
3639
);

‎src/Controllers/VerifiableCredentials/CredentialIssuerCredentialController.php‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SimpleSAML\Module\oidc\Bridges\PsrHttpBridge;
1111
use SimpleSAML\Module\oidc\Codebooks\FlowTypeEnum;
1212
use SimpleSAML\Module\oidc\Entities\AccessTokenEntity;
13+
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
1314
use SimpleSAML\Module\oidc\Exceptions\CredentialRequestException;
1415
use SimpleSAML\Module\oidc\Helpers;
1516
use SimpleSAML\Module\oidc\ModuleConfig;
@@ -157,10 +158,12 @@ public function credential(Request $request): Response
157158
$issuerState = $accessToken->getIssuerState();
158159
if (
159160
!is_string($issuerState) &&
160-
($accessToken->getFlowTypeEnum() === FlowTypeEnum::VciAuthorizationCode)
161+
($accessToken->getFlowTypeEnum() === FlowTypeEnum::VciAuthorizationCode) &&
162+
!$this->isIssuedToRegisteredClient($accessToken)
161163
) {
162164
$this->loggerService->error(
163-
'CredentialIssuerCredentialController::credential: Issuer state missing in access token.',
165+
'CredentialIssuerCredentialController::credential: Issuer state missing in access token issued ' .
166+
'to a client which is not registered.',
164167
['accessTokenState' => $accessToken->getState()],
165168
);
166169
return $this->routes->newJsonErrorResponse(
@@ -398,6 +401,30 @@ public function credential(Request $request): Response
398401
);
399402
}
400403

404+
// Without an issuer state the token follows no offer, so what its scopes grant is all the registered
405+
// client was authorized for, and the credential has to be among them. A configuration requested
406+
// through authorization_details is among them too when it was granted: AuthCodeGrant adds those ids to
407+
// the scopes, and the token endpoint keeps only the scopes the client is registered for.
408+
if (
409+
!is_string($issuerState) &&
410+
($accessToken->getFlowTypeEnum() === FlowTypeEnum::VciAuthorizationCode) &&
411+
!$this->isCredentialConfigurationGranted($accessToken, $resolvedCredentialIdentifier)
412+
) {
413+
$this->loggerService->error(
414+
'CredentialIssuerCredentialController::credential: Credential configuration not granted by the ' .
415+
'access token.',
416+
[
417+
'credentialConfigurationId' => $resolvedCredentialIdentifier,
418+
'accessTokenId' => $accessToken->getIdentifier(),
419+
],
420+
);
421+
return $this->routes->newJsonErrorResponse(
422+
'insufficient_scope',
423+
'The access token does not grant the requested credential.',
424+
403,
425+
);
426+
}
427+
401428
$credentialFormatId = $resolvedCredentialConfiguration[ClaimsEnum::Format->value] ?? null;
402429
if (!is_string($credentialFormatId)) {
403430
$this->loggerService->error(
@@ -892,6 +919,36 @@ public function credential(Request $request): Response
892919
}
893920

894921

922+
/**
923+
* Whether the access token was issued to a registered client, which is what an authorization code token
924+
* without an issuer_state needs to be. A wallet which starts the flow on its own sends no issuer_state, and
925+
* the credential scopes it was granted are ones its registration allows (ScopeRepository::finalizeScopes());
926+
* the credential it asks for is then checked against them (isCredentialConfigurationGranted()).
927+
* The generic client stands in for wallets which are not registered, and ClientRule falls back to it only
928+
* for a request carrying an issuer_state, so its tokens always need one to be redeemed here.
929+
*/
930+
protected function isIssuedToRegisteredClient(AccessTokenEntity $accessToken): bool
931+
{
932+
$client = $accessToken->getClient();
933+
934+
return $client instanceof ClientEntityInterface && !$client->isGeneric();
935+
}
936+
937+
938+
protected function isCredentialConfigurationGranted(
939+
AccessTokenEntity $accessToken,
940+
string $credentialConfigurationId,
941+
): bool {
942+
foreach ($accessToken->getScopes() as $scope) {
943+
if ($scope->getIdentifier() === $credentialConfigurationId) {
944+
return true;
945+
}
946+
}
947+
948+
return false;
949+
}
950+
951+
895952
/**
896953
* Helper method to set a claim value at a path. Supports creating nested arrays dynamically.
897954
* @psalm-suppress UnusedVariable, MixedAssignment

‎src/Factories/AuthorizationServerFactory.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SimpleSAML\Module\oidc\ModuleConfig;
99
use SimpleSAML\Module\oidc\Repositories\AccessTokenRepository;
1010
use SimpleSAML\Module\oidc\Repositories\ClientRepository;
11+
use SimpleSAML\Module\oidc\Repositories\PushedAuthorizationRequestRepository;
1112
use SimpleSAML\Module\oidc\Repositories\ScopeRepository;
1213
use SimpleSAML\Module\oidc\Server\AuthorizationServer;
1314
use SimpleSAML\Module\oidc\Server\Grants\AuthCodeGrant;
@@ -34,6 +35,7 @@ public function __construct(
3435
private readonly CryptKey $privateKey,
3536
private readonly PreAuthCodeGrant $preAuthCodeGrant,
3637
private readonly LoggerService $loggerService,
38+
private readonly PushedAuthorizationRequestRepository $pushedAuthorizationRequestRepository,
3739
) {
3840
}
3941

@@ -49,6 +51,7 @@ public function build(): AuthorizationServer
4951
$this->tokenResponse,
5052
$this->requestRulesManager,
5153
$this->loggerService,
54+
$this->pushedAuthorizationRequestRepository,
5255
);
5356

5457
$authorizationServer->enableGrantType(

‎src/Repositories/PushedAuthorizationRequestRepository.php‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SimpleSAML\Module\oidc\Repositories;
66

7+
use DateInterval;
78
use PDO;
89
use SimpleSAML\Database;
910
use SimpleSAML\Module\oidc\Codebooks\DateFormatsEnum;
@@ -143,13 +144,29 @@ public function consume(string $requestUri): bool
143144

144145

145146
/**
146-
* Delete expired Pushed Authorization Request records.
147+
* Delete Pushed Authorization Request records which no authorization can still use.
148+
*
149+
* A record outlives its expiry by SimpleSAMLphp's session.state.timeout. The request_uri is consumed when the
150+
* authorization response is issued (AuthorizationServer::completeAuthorizationRequest()), and an
151+
* authorization validated before the expiry may resume after it: an authproc such as consent is resumed
152+
* from saved state, without validating the request_uri again, and that state lives for
153+
* session.state.timeout. Deleted earlier, the record would be missing when that authorization is completed,
154+
* and the consume would refuse it as a replay. An expired record can not start a new authorization, since
155+
* RequestUriRule refuses it, so keeping it longer lets nothing else through.
156+
*
157+
* @throws \Exception
147158
*/
148159
public function removeExpired(): void
149160
{
161+
$stateTimeout = max(
162+
0,
163+
$this->moduleConfig->sspConfig()->getOptionalInteger('session.state.timeout', 60 * 60),
164+
);
165+
$cutoff = $this->helpers->dateTime()->getUtc()->sub(new DateInterval('PT' . $stateTimeout . 'S'));
166+
150167
$this->database->write(
151-
"DELETE FROM {$this->getTableName()} WHERE expires_at < :now",
152-
['now' => $this->helpers->dateTime()->getUtc()->format(DateFormatsEnum::DB_DATETIME->value)],
168+
"DELETE FROM {$this->getTableName()} WHERE expires_at < :cutoff",
169+
['cutoff' => $cutoff->format(DateFormatsEnum::DB_DATETIME->value)],
153170
);
154171
}
155172
}

‎src/Server/AuthorizationServer.php‎

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface as OAuth2AuthorizationRequestInterface;
1515
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
1616
use LogicException;
17+
use Psr\Http\Message\ResponseInterface;
1718
use Psr\Http\Message\ServerRequestInterface;
1819
use SimpleSAML\Error\BadRequest;
20+
use SimpleSAML\Module\oidc\Factories\Entities\PushedAuthorizationRequestEntityFactory;
21+
use SimpleSAML\Module\oidc\Repositories\PushedAuthorizationRequestRepository;
1922
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
2023
use SimpleSAML\Module\oidc\Server\Grants\Interfaces\AuthorizationValidatableWithRequestRules;
24+
use SimpleSAML\Module\oidc\Server\RequestRules\Interfaces\ResultBagInterface;
2125
use SimpleSAML\Module\oidc\Server\RequestRules\RequestRulesManager;
2226
use SimpleSAML\Module\oidc\Server\RequestRules\Rules\ClientRedirectUriRule;
2327
use SimpleSAML\Module\oidc\Server\RequestRules\Rules\ClientRule;
@@ -27,6 +31,7 @@
2731
use SimpleSAML\Module\oidc\Server\RequestRules\Rules\ResponseModeRule;
2832
use SimpleSAML\Module\oidc\Server\RequestRules\Rules\StateRule;
2933
use SimpleSAML\Module\oidc\Server\RequestRules\Rules\UiLocalesRule;
34+
use SimpleSAML\Module\oidc\Server\RequestTypes\AuthorizationRequest;
3035
use SimpleSAML\Module\oidc\Server\RequestTypes\LogoutRequest;
3136
use SimpleSAML\Module\oidc\Server\ResponseModes\QueryResponseMode;
3237
use SimpleSAML\Module\oidc\Services\LoggerService;
@@ -61,6 +66,7 @@ public function __construct(
6166
?ResponseTypeInterface $responseType = null,
6267
?RequestRulesManager $requestRulesManager = null,
6368
protected readonly ?LoggerService $loggerService = null,
69+
protected readonly ?PushedAuthorizationRequestRepository $pushedAuthorizationRequestRepository = null,
6470
) {
6571
parent::__construct(
6672
$clientRepository,
@@ -153,7 +159,10 @@ public function validateAuthorizationRequest(ServerRequestInterface $request): O
153159
),
154160
);
155161

156-
return $grantType->validateAuthorizationRequestWithRequestRules($request, $resultBag);
162+
$authorizationRequest = $grantType->validateAuthorizationRequestWithRequestRules($request, $resultBag);
163+
$this->bindPushedAuthorizationRequestUri($authorizationRequest, $resultBag);
164+
165+
return $authorizationRequest;
157166
} else {
158167
$this->loggerService?->debug(
159168
'AuthorizationServer: Grant type can NOT respond to ' .
@@ -171,6 +180,94 @@ public function validateAuthorizationRequest(ServerRequestInterface $request): O
171180
}
172181

173182

183+
/**
184+
* Consume the Pushed Authorization Request the request was made with, if any, before the grant issues the
185+
* authorization response.
186+
*
187+
* A PAR request_uri is one-time use (RFC 9126 section 4), but it can not be consumed when the request is
188+
* validated: the user agent comes back to the authorization endpoint with the same request_uri after the
189+
* login, and that request is validated again. RFC 9126 allows a request_uri to be used more than once that
190+
* way. It is consumed here instead, atomically, so one request_uri still yields at most one response, and a
191+
* second one, from another tab or a replay, is refused the way RequestUriRule refuses a consumed one.
192+
*
193+
* @inheritDoc
194+
* @throws \SimpleSAML\Error\BadRequest
195+
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
196+
*/
197+
public function completeAuthorizationRequest(
198+
OAuth2AuthorizationRequestInterface $authRequest,
199+
ResponseInterface $response,
200+
): ResponseInterface {
201+
if ($authRequest instanceof AuthorizationRequest) {
202+
$this->consumePushedAuthorizationRequest($authRequest);
203+
}
204+
205+
return parent::completeAuthorizationRequest($authRequest, $response);
206+
}
207+
208+
209+
/**
210+
* Carry the PAR request_uri, if the request was made with one, on the validated authorization request,
211+
* which is what survives the login and the authprocs. A request of another type could not carry it, and
212+
* its PAR could then never be consumed, so it is refused.
213+
*
214+
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
215+
*/
216+
protected function bindPushedAuthorizationRequestUri(
217+
OAuth2AuthorizationRequestInterface $authorizationRequest,
218+
ResultBagInterface $resultBag,
219+
): void {
220+
$requestUri = $resultBag->get(RequestUriRule::class)?->getValue();
221+
222+
if (
223+
!is_string($requestUri) ||
224+
!str_starts_with($requestUri, PushedAuthorizationRequestEntityFactory::REQUEST_URI_PREFIX)
225+
) {
226+
return;
227+
}
228+
229+
if (!$authorizationRequest instanceof AuthorizationRequest) {
230+
$this->loggerService?->error(
231+
'AuthorizationServer: pushed authorization request can not be tracked for one-time use.',
232+
['authorizationRequestType' => $authorizationRequest::class],
233+
);
234+
throw OidcServerException::serverError('Pushed authorization request can not be tracked.');
235+
}
236+
237+
$authorizationRequest->setPushedAuthorizationRequestUri($requestUri);
238+
}
239+
240+
241+
/**
242+
* @throws \SimpleSAML\Error\BadRequest
243+
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
244+
*/
245+
protected function consumePushedAuthorizationRequest(AuthorizationRequest $authorizationRequest): void
246+
{
247+
$requestUri = $authorizationRequest->getPushedAuthorizationRequestUri();
248+
249+
if ($requestUri === null) {
250+
return;
251+
}
252+
253+
if ($this->pushedAuthorizationRequestRepository === null) {
254+
$this->loggerService?->error(
255+
'AuthorizationServer: no pushed authorization request repository to consume the request with.',
256+
compact('requestUri'),
257+
);
258+
throw OidcServerException::serverError('Pushed authorization request can not be consumed.');
259+
}
260+
261+
if (!$this->pushedAuthorizationRequestRepository->consume($requestUri)) {
262+
$this->loggerService?->warning(
263+
'AuthorizationServer: pushed authorization request replay attempt.',
264+
compact('requestUri'),
265+
);
266+
throw new BadRequest('AuthorizationServer: Pushed authorization request has already been used.');
267+
}
268+
}
269+
270+
174271
/**
175272
* @throws \Throwable
176273
* @throws \SimpleSAML\Error\BadRequest

‎src/Server/RequestRules/Rules/ClientRedirectUriRule.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public function checkRule(
8585
}
8686
} catch (Throwable $exception) {
8787
if (
88-
$this->requestParamsResolver->isVciAuthorizationCodeRequest($request, $allowedServerRequestMethods) &&
88+
$this->requestParamsResolver->isVciAuthorizationCodeRequestWithIssuerState(
89+
$request,
90+
$allowedServerRequestMethods,
91+
) &&
8992
$this->moduleConfig->getVciEnabled() &&
9093
$this->moduleConfig->getVciAllowNonRegisteredClients()
9194
) {

‎src/Server/RequestRules/Rules/ClientRule.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ public function checkRule(
134134
}
135135

136136
if (
137-
$this->requestParamsResolver->isVciAuthorizationCodeRequest($request, $allowedServerRequestMethods) &&
137+
$this->requestParamsResolver->isVciAuthorizationCodeRequestWithIssuerState(
138+
$request,
139+
$allowedServerRequestMethods,
140+
) &&
138141
$this->moduleConfig->getVciEnabled() &&
139142
$this->moduleConfig->getVciAllowNonRegisteredClients()
140143
) {

0 commit comments

Comments
 (0)