|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * | |
| 7 | + * \ ___ / _________ |
| 8 | + * _ / \ _ GÉANT | * * | Co-Funded by |
| 9 | + * | ~ | Trust & Identity | * * | the European |
| 10 | + * \_/ Incubator |__*_*__| Union |
| 11 | + * = |
| 12 | + * |
| 13 | + * This file is part of the simplesamlphp-module-oidc. |
| 14 | + * |
| 15 | + * Copyright (C) 2018 by the Spanish Research and Academic Network. |
| 16 | + * |
| 17 | + * This code was developed by Universidad de Córdoba (UCO https://www.uco.es) |
| 18 | + * for the RedIRIS SIR service (SIR: http://www.rediris.es/sir) |
| 19 | + * |
| 20 | + * For the full copyright and license information, please view the LICENSE |
| 21 | + * file that was distributed with this source code. |
| 22 | + */ |
| 23 | + |
| 24 | +namespace SimpleSAML\Module\oidc\Controllers\VerifiableCredentials; |
| 25 | + |
| 26 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 27 | +use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException; |
| 28 | +use SimpleSAML\Module\oidc\Services\LoggerService; |
| 29 | +use SimpleSAML\Module\oidc\Utils\Routes; |
| 30 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 31 | +use Symfony\Component\HttpFoundation\Response; |
| 32 | + |
| 33 | +/** |
| 34 | + * Serves the JSON-LD context document for a specific vc+sd-jwt credential configuration, allowing |
| 35 | + * verifiers to resolve the custom terms used in credential subjects. |
| 36 | + * |
| 37 | + * The endpoint URL is included in the @context array of issued vc+sd-jwt credentials when a |
| 38 | + * context document is configured for the matching credential configuration ID. |
| 39 | + * |
| 40 | + * @see https://www.w3.org/TR/json-ld11/#interpreting-json-as-json-ld |
| 41 | + */ |
| 42 | +class CredentialJsonLdContextController |
| 43 | +{ |
| 44 | + /** |
| 45 | + * @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + protected readonly ModuleConfig $moduleConfig, |
| 49 | + protected readonly Routes $routes, |
| 50 | + protected readonly LoggerService $loggerService, |
| 51 | + ) { |
| 52 | + if (!$this->moduleConfig->getVciEnabled()) { |
| 53 | + $this->loggerService->warning('Verifiable Credential capabilities not enabled.'); |
| 54 | + throw OidcServerException::forbidden('Verifiable Credential capabilities not enabled.'); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Return the JSON-LD context document for the given credential configuration ID. |
| 60 | + * |
| 61 | + * Responds with HTTP 404 if no context document is configured for the given ID. |
| 62 | + * |
| 63 | + * @param string $credentialConfigurationId URL path parameter injected by the router. |
| 64 | + */ |
| 65 | + public function context(string $credentialConfigurationId): Response |
| 66 | + { |
| 67 | + $this->loggerService->debug( |
| 68 | + 'CredentialJsonLdContextController::context', |
| 69 | + ['credentialConfigurationId' => $credentialConfigurationId], |
| 70 | + ); |
| 71 | + |
| 72 | + $contextDocument = $this->moduleConfig->getVciCredentialJsonLdContextFor($credentialConfigurationId); |
| 73 | + |
| 74 | + if ($contextDocument === null) { |
| 75 | + $this->loggerService->warning( |
| 76 | + 'CredentialJsonLdContextController::context: No JSON-LD context configured for credential ' . |
| 77 | + 'configuration ID.', |
| 78 | + ['credentialConfigurationId' => $credentialConfigurationId], |
| 79 | + ); |
| 80 | + |
| 81 | + return $this->routes->newResponse(null, Response::HTTP_NOT_FOUND); |
| 82 | + } |
| 83 | + |
| 84 | + return new JsonResponse( |
| 85 | + $contextDocument, |
| 86 | + Response::HTTP_OK, |
| 87 | + ['Content-Type' => 'application/ld+json'], |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
0 commit comments