|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\oidc\Controllers\Admin; |
| 6 | + |
| 7 | +use SimpleSAML\Module\oidc\Admin\Authorization; |
| 8 | +use SimpleSAML\Module\oidc\Codebooks\ParametersEnum; |
| 9 | +use SimpleSAML\Module\oidc\Codebooks\RoutesEnum; |
| 10 | +use SimpleSAML\Module\oidc\Entities\ScopeEntity; |
| 11 | +use SimpleSAML\Module\oidc\Factories\Entities\AuthCodeEntityFactory; |
| 12 | +use SimpleSAML\Module\oidc\Factories\Entities\ClientEntityFactory; |
| 13 | +use SimpleSAML\Module\oidc\Factories\TemplateFactory; |
| 14 | +use SimpleSAML\Module\oidc\Helpers; |
| 15 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 16 | +use SimpleSAML\Module\oidc\Repositories\AuthCodeRepository; |
| 17 | +use SimpleSAML\Module\oidc\Repositories\ClientRepository; |
| 18 | +use SimpleSAML\Module\oidc\Services\LoggerService; |
| 19 | +use SimpleSAML\Module\oidc\Utils\Debug\ArrayLogger; |
| 20 | +use SimpleSAML\OpenID\Codebooks\ClaimsEnum; |
| 21 | +use SimpleSAML\OpenID\Codebooks\GrantTypesEnum; |
| 22 | +use SimpleSAML\OpenID\Federation; |
| 23 | +use SimpleSAML\OpenID\VerifiableCredentials; |
| 24 | +use Symfony\Component\HttpFoundation\Request; |
| 25 | +use Symfony\Component\HttpFoundation\Response; |
| 26 | + |
| 27 | +class VerifiableCredentailsTestController |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + protected readonly ModuleConfig $moduleConfig, |
| 31 | + protected readonly TemplateFactory $templateFactory, |
| 32 | + protected readonly Authorization $authorization, |
| 33 | + protected readonly VerifiableCredentials $verifiableCredentials, |
| 34 | + protected readonly AuthCodeRepository $authCodeRepository, |
| 35 | + protected readonly AuthCodeEntityFactory $authCodeEntityFactory, |
| 36 | + protected readonly ClientRepository $clientRepository, |
| 37 | + protected readonly ClientEntityFactory $clientEntityFactory, |
| 38 | + protected readonly Federation $federation, |
| 39 | + protected readonly Helpers $helpers, |
| 40 | + protected readonly LoggerService $loggerService, |
| 41 | + ) { |
| 42 | + $this->authorization->requireAdmin(true); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @throws \SimpleSAML\Error\ConfigurationError |
| 47 | + * @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException |
| 48 | + * @throws \SimpleSAML\OpenID\Exceptions\CredentialOfferException |
| 49 | + */ |
| 50 | + public function verifiableCredentialIssuance(Request $request): Response |
| 51 | + { |
| 52 | + $sampleData = [ |
| 53 | + 'eduPersonPrincipalName' => '[email protected]', |
| 54 | + 'eduPersonTargetedID' => 'abc123', |
| 55 | + 'displayName' => 'Test User', |
| 56 | + 'givenName' => 'Test', |
| 57 | + 'sn' => 'User', |
| 58 | + |
| 59 | + 'eduPersonScopedAffiliation' => '[email protected]', |
| 60 | + ]; |
| 61 | + |
| 62 | + $this->loggerService->info('test', $sampleData);; |
| 63 | + |
| 64 | + // TODO mivanci Wallet (client) credential_offer_endpoint metadata |
| 65 | + // https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#client-metadata |
| 66 | + |
| 67 | + $clientId = '1234567890'; |
| 68 | + $clientSecret = '1234567890'; |
| 69 | + |
| 70 | + if (($client = $this->clientRepository->findById($clientId)) === null) { |
| 71 | + $client = $this->clientEntityFactory->fromData( |
| 72 | + id: $clientId, |
| 73 | + secret: $clientSecret, |
| 74 | + name: 'VCI Test Client', |
| 75 | + description: 'Test client for VCI', |
| 76 | + redirectUri: ['https://example.com/oidc/callback'], |
| 77 | + scopes: ['openid', 'ResearchAndScholarshipCredentialJwtVcJson'], |
| 78 | + isEnabled: true, |
| 79 | + ); |
| 80 | + |
| 81 | + $this->clientRepository->add($client); |
| 82 | + ; |
| 83 | + } |
| 84 | + |
| 85 | + $authCodeId = '1234567890'; |
| 86 | + |
| 87 | + // TODO mivanci Add indication of preauthz code to the auth code table. |
| 88 | + |
| 89 | + if (($authCode = $this->authCodeRepository->findById($authCodeId)) === null) { |
| 90 | + $authCode = $this->authCodeEntityFactory->fromData( |
| 91 | + id: $authCodeId, |
| 92 | + client: $client, |
| 93 | + scopes: [ |
| 94 | + new ScopeEntity('openid'), |
| 95 | + new ScopeEntity('ResearchAndScholarshipCredentialJwtVcJson'), |
| 96 | + ], |
| 97 | + expiryDateTime: new \DateTimeImmutable('+1 month'), |
| 98 | + userIdentifier: 'testuid', |
| 99 | + redirectUri: 'https://example.com/oidc/callback', |
| 100 | + nonce: '1234567890', |
| 101 | + ); |
| 102 | + |
| 103 | + $this->authCodeRepository->persistNewAuthCode($authCode); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + $credentialOffer = $this->verifiableCredentials->credentialOfferFactory()->from( |
| 108 | + parameters: [ |
| 109 | + ClaimsEnum::CredentialIssuer->value => $this->moduleConfig->getIssuer(), |
| 110 | + ClaimsEnum::CredentialConfigurationIds->value => [ |
| 111 | + 'ResearchAndScholarshipCredentialJwtVcJson', |
| 112 | + ], |
| 113 | + ClaimsEnum::Grants->value => [ |
| 114 | + GrantTypesEnum::PreAuthorizedCode->value => [ |
| 115 | + ClaimsEnum::PreAuthorizedCode->value => $authCode->getIdentifier(), |
| 116 | + // TODO mivanci support for TxCode |
| 117 | + // ClaimsEnum::TxCode->value => [ |
| 118 | + // ClaimsEnum::InputMode->value => 'numeric', |
| 119 | + // ClaimsEnum::Length->value => 6, |
| 120 | + // ClaimsEnum::Description->value => 'Sent to user mail', |
| 121 | + // ], |
| 122 | + ], |
| 123 | + ], |
| 124 | + ], |
| 125 | + ); |
| 126 | + |
| 127 | + $credentialOfferValue = $credentialOffer->jsonSerialize(); |
| 128 | + $parameterName = ParametersEnum::CredentialOfferUri->value; |
| 129 | + if (is_array($credentialOfferValue)) { |
| 130 | + $parameterName = ParametersEnum::CredentialOffer->value; |
| 131 | + $credentialOfferValue = json_encode($credentialOfferValue); |
| 132 | + } |
| 133 | + |
| 134 | + $credentialOfferUri = "openid-credential-offer://?$parameterName=$credentialOfferValue"; |
| 135 | + |
| 136 | + // https://quickchart.io/documentation/qr-codes/ |
| 137 | + $qrUri = 'https://quickchart.io/qr?size=200&margin=1&text=' . urlencode($credentialOfferUri); |
| 138 | + |
| 139 | + return $this->templateFactory->build( |
| 140 | + 'oidc:tests/verifiable-credential-issuance.twig', |
| 141 | + compact('qrUri', 'sampleData', 'credentialOfferUri'), |
| 142 | + RoutesEnum::AdminTestVerifiableCredentialIssuance->value, |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments