forked from simplesamlphp/simplesamlphp-module-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVciCredentialOfferApiController.php
More file actions
220 lines (190 loc) · 8.98 KB
/
VciCredentialOfferApiController.php
File metadata and controls
220 lines (190 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\oidc\Controllers\Api;
use SimpleSAML\Module\oidc\Codebooks\ApiScopesEnum;
use SimpleSAML\Module\oidc\Exceptions\AuthorizationException;
use SimpleSAML\Module\oidc\Factories\CredentialOfferUriFactory;
use SimpleSAML\Module\oidc\ModuleConfig;
use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException;
use SimpleSAML\Module\oidc\Services\Api\Authorization;
use SimpleSAML\Module\oidc\Services\LoggerService;
use SimpleSAML\Module\oidc\Utils\Routes;
use SimpleSAML\OpenID\Codebooks\GrantTypesEnum;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class VciCredentialOfferApiController
{
/**
* @throws OidcServerException
*/
public function __construct(
protected readonly ModuleConfig $moduleConfig,
protected readonly Authorization $authorization,
protected readonly LoggerService $loggerService,
protected readonly Routes $routes,
protected readonly CredentialOfferUriFactory $credentialOfferUriFactory,
) {
if (!$this->moduleConfig->getApiEnabled()) {
$this->loggerService->warning('API capabilities not enabled.');
throw OidcServerException::forbidden('API capabilities not enabled.');
}
if (!$this->moduleConfig->getVciEnabled()) {
$this->loggerService->warning('Verifiable Credential capabilities not enabled.');
throw OidcServerException::forbidden('Verifiable Credential capabilities not enabled.');
}
}
/**
* @throws OidcServerException
*/
public function credentialOffer(Request $request): Response
{
if (!$this->moduleConfig->getApiVciCredentialOfferEndpointEnabled()) {
$this->loggerService->warning('Credential Offer API endpoint not enabled.');
throw OidcServerException::forbidden('Credential Offer API endpoint not enabled.');
}
$this->loggerService->debug('VciCredentialOfferApiController::credentialOffer');
$this->loggerService->debug(
'VciCredentialOfferApiController: Request data: ',
$request->getPayload()->all(),
);
try {
$this->authorization->requireTokenForAnyOfScope(
$request,
[ApiScopesEnum::VciCredentialOffer, ApiScopesEnum::VciAll, ApiScopesEnum::All],
);
} catch (AuthorizationException $e) {
$this->loggerService->error(
'VciCredentialOfferApiController: AuthorizationException: ' . $e->getMessage(),
);
return $this->routes->newJsonErrorResponse(
error: 'unauthorized',
description: $e->getMessage(),
httpCode: Response::HTTP_UNAUTHORIZED,
);
}
$input = $request->getPayload()->all();
$credentialConfigurationId = $input['credential_configuration_id'] ?? null;
if (!is_string($credentialConfigurationId)) {
$this->loggerService->error(
'VciCredentialOfferApiController: credential_configuration_id not provided or not a string.',
);
return $this->routes->newJsonErrorResponse(
error: 'invalid_request',
description: 'No credential configuration ID (credential_configuration_id) provided.',
httpCode: Response::HTTP_BAD_REQUEST,
);
}
$credentialConfiguration = $this->moduleConfig->getVciCredentialConfiguration($credentialConfigurationId);
if (!is_array($credentialConfiguration)) {
$this->loggerService->error(
'VciCredentialOfferApiController: Provided Credential Configuration ID is not supported.',
['credentialConfigurationId' => $credentialConfigurationId],
);
return $this->routes->newJsonErrorResponse(
error: 'invalid_request',
description: 'Provided credential configuration ID (credential_configuration_id) is not supported.',
httpCode: Response::HTTP_BAD_REQUEST,
);
}
$grantType = $input['grant_type'] ?? null;
if (!is_string($grantType)) {
$this->loggerService->error('VciCredentialOfferApiController: Grant Type (grant_type) not provided.');
return $this->routes->newJsonErrorResponse(
error: 'invalid_request',
description: 'No credential Grant Type (grant_type) provided.',
httpCode: Response::HTTP_BAD_REQUEST,
);
}
$grantTypeEnum = GrantTypesEnum::tryFrom($grantType);
if (!$grantTypeEnum instanceof GrantTypesEnum) {
$this->loggerService->error(
'VciCredentialOfferApiController: Invalid credential Grant Type (grant_type) provided.',
['grantType' => $grantType],
);
return $this->routes->newJsonErrorResponse(
error: 'invalid_request',
description: 'Invalid credential Grant Type (grant_type) provided.',
httpCode: Response::HTTP_BAD_REQUEST,
);
}
if (!$grantTypeEnum->canBeUsedForVerifiableCredentialIssuance()) {
$this->loggerService->error(
'VciCredentialOfferApiController: Provided Grant Type can not be used for verifiable credential' .
' issuance.',
['grantType' => $grantType],
);
return $this->routes->newJsonErrorResponse(
error: 'invalid_request',
description: 'Provided Grant Type can not be used for verifiable credential issuance.',
httpCode: Response::HTTP_BAD_REQUEST,
);
}
$credentialOfferUri = null;
if ($grantTypeEnum === GrantTypesEnum::AuthorizationCode) {
$this->loggerService->debug(
'VciCredentialOfferApiController: AuthorizationCode Grant Type provided. Building credential ' .
'offer for Authorization Code Flow.',
);
$credentialOfferUri = $this->credentialOfferUriFactory->buildForAuthorization(
[$credentialConfigurationId],
);
}
if ($grantTypeEnum === GrantTypesEnum::PreAuthorizedCode) {
$this->loggerService->debug(
'VciCredentialOfferApiController: PreAuthorizedCode Grant Type provided. Building credential ' .
'offer for Pre-authorized Code Flow.',
);
/** @psalm-suppress MixedAssignment */
$userAttributes = $input['user_attributes'] ?? [];
$userAttributes = is_array($userAttributes) ? $userAttributes : [];
$useTxCode = boolval($input['use_tx_code'] ?? false);
/** @psalm-suppress MixedAssignment */
$usersEmailAttributeName = $input['users_email_attribute_name'] ?? null;
$usersEmailAttributeName = is_string($usersEmailAttributeName) ? $usersEmailAttributeName : null;
/** @psalm-suppress MixedAssignment */
$authenticationSourceId = $input['authentication_source_id'] ?? null;
$authenticationSourceId = is_string($authenticationSourceId) ? $authenticationSourceId : null;
if (is_null($usersEmailAttributeName) && is_string($authenticationSourceId)) {
$usersEmailAttributeName = $this->moduleConfig->getUsersEmailAttributeNameForAuthSourceId(
$authenticationSourceId,
);
}
$this->loggerService->debug(
'VciCredentialOfferApiController: PreAuthorizedCode data:',
[
'userAttributes' => $userAttributes,
'useTxCode' => $useTxCode,
'authenticationSourceId' => $authenticationSourceId,
'usersEmailAttributeName' => $usersEmailAttributeName,
],
);
$credentialOfferUri = $this->credentialOfferUriFactory->buildPreAuthorized(
[$credentialConfigurationId],
$userAttributes,
$useTxCode,
$usersEmailAttributeName,
);
}
if ($credentialOfferUri !== null) {
$data = [
'credential_offer_uri' => $credentialOfferUri,
];
$this->loggerService->debug(
'VciCredentialOfferApiController: Credential Offer URI built successfully, returning data:',
$data,
);
return $this->routes->newJsonResponse(
data: $data,
);
}
$this->loggerService->debug(
'VciCredentialOfferApiController: Credential Offer URI NOT built for provided Grant Type.',
['grantType' => $grantType],
);
return $this->routes->newJsonErrorResponse(
error: 'invalid_request',
description: 'No implementation for provided Grant Type.',
httpCode: Response::HTTP_BAD_REQUEST,
);
}
}