|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_digital_post\Helper; |
| 4 | + |
| 5 | +use Drupal\os2forms_digital_post\Exception\CertificateLocatorException; |
| 6 | +use GuzzleHttp\Client; |
| 7 | +use Http\Adapter\Guzzle7\Client as GuzzleAdapter; |
| 8 | +use Http\Factory\Guzzle\RequestFactory; |
| 9 | +use ItkDev\AzureKeyVault\Authorisation\VaultToken; |
| 10 | +use ItkDev\AzureKeyVault\KeyVault\VaultSecret; |
| 11 | +use ItkDev\Serviceplatformen\Certificate\AzureKeyVaultCertificateLocator; |
| 12 | +use ItkDev\Serviceplatformen\Certificate\CertificateLocatorInterface; |
| 13 | +use ItkDev\Serviceplatformen\Certificate\FilesystemCertificateLocator; |
| 14 | + |
| 15 | +/** |
| 16 | + * Certificate locator helper. |
| 17 | + */ |
| 18 | +class CertificateLocatorHelper { |
| 19 | + public const LOCATOR_TYPE_AZURE_KEY_VAULT = 'azure_key_vault'; |
| 20 | + public const LOCATOR_TYPE_FILE_SYSTEM = 'file_system'; |
| 21 | + |
| 22 | + /** |
| 23 | + * {@inheritdoc} |
| 24 | + */ |
| 25 | + public function __construct( |
| 26 | + private readonly Settings $settings, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get certificate locator. |
| 32 | + */ |
| 33 | + public function getCertificateLocator(): CertificateLocatorInterface { |
| 34 | + $certificateSettings = $this->settings->getCertificate(); |
| 35 | + |
| 36 | + $locatorType = $certificateSettings['locator_type']; |
| 37 | + $options = $certificateSettings[$locatorType]; |
| 38 | + $options += [ |
| 39 | + 'passphrase' => $certificateSettings['passphrase'] ?: '', |
| 40 | + ]; |
| 41 | + |
| 42 | + if (self::LOCATOR_TYPE_AZURE_KEY_VAULT === $locatorType) { |
| 43 | + $httpClient = new GuzzleAdapter(new Client()); |
| 44 | + $requestFactory = new RequestFactory(); |
| 45 | + |
| 46 | + $vaultToken = new VaultToken($httpClient, $requestFactory); |
| 47 | + |
| 48 | + $token = $vaultToken->getToken( |
| 49 | + $options['tenant_id'], |
| 50 | + $options['application_id'], |
| 51 | + $options['client_secret'], |
| 52 | + ); |
| 53 | + |
| 54 | + $vault = new VaultSecret( |
| 55 | + $httpClient, |
| 56 | + $requestFactory, |
| 57 | + $options['name'], |
| 58 | + $token->getAccessToken() |
| 59 | + ); |
| 60 | + |
| 61 | + return new AzureKeyVaultCertificateLocator( |
| 62 | + $vault, |
| 63 | + $options['secret'], |
| 64 | + $options['version'], |
| 65 | + $options['passphrase'], |
| 66 | + ); |
| 67 | + } |
| 68 | + elseif (self::LOCATOR_TYPE_FILE_SYSTEM === $locatorType) { |
| 69 | + $certificatepath = realpath($options['path']) ?: NULL; |
| 70 | + if (NULL === $certificatepath) { |
| 71 | + throw new CertificateLocatorException(sprintf('Invalid certificate path %s', $options['path'])); |
| 72 | + } |
| 73 | + return new FilesystemCertificateLocator($certificatepath, $options['passphrase']); |
| 74 | + } |
| 75 | + |
| 76 | + throw new CertificateLocatorException(sprintf('Invalid certificate locator type: %s', $locatorType)); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments