|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | + |
| 11 | +use OCA\UserOIDC\Service\JweService; |
| 12 | +use OCA\UserOIDC\Service\JwkService; |
| 13 | +use OCP\AppFramework\Services\IAppConfig; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class JweServiceTest extends TestCase { |
| 18 | + |
| 19 | + /** @var IAppConfig|MockObject */ |
| 20 | + private $appConfig; |
| 21 | + /** @var JwkService|MockObject */ |
| 22 | + private $jwkService; |
| 23 | + /** @var JweService|MockObject */ |
| 24 | + private $jweService; |
| 25 | + |
| 26 | + public function setUp(): void { |
| 27 | + parent::setUp(); |
| 28 | + $this->appConfig = $this->createMock(IAppConfig::class); |
| 29 | + $this->jwkService = new JwkService($this->appConfig); |
| 30 | + $this->jweService = new JweService($this->jwkService); |
| 31 | + } |
| 32 | + |
| 33 | + public function testJweEncryptionDecryption() { |
| 34 | + $myPemEncryptionKey = $this->jwkService->getMyEncryptionKey(true); |
| 35 | + $sslEncryptionKey = openssl_pkey_get_private($myPemEncryptionKey); |
| 36 | + $sslEncryptionKeyDetails = openssl_pkey_get_details($sslEncryptionKey); |
| 37 | + $encPublicJwk = $this->jwkService->getJwkFromSslKey($sslEncryptionKeyDetails, isEncryptionKey: true); |
| 38 | + $encPrivJwk = $this->jwkService->getJwkFromSslKey($sslEncryptionKeyDetails, isEncryptionKey: true, includePrivateKey: true); |
| 39 | + |
| 40 | + $inputPayloadArray = [ |
| 41 | + 'iat' => time(), |
| 42 | + 'nbf' => time(), |
| 43 | + 'exp' => time() + 3600, |
| 44 | + 'iss' => 'My service', |
| 45 | + 'aud' => 'Your application', |
| 46 | + ]; |
| 47 | + |
| 48 | + $serializedJweToken = $this->jweService->createSerializedJwe($inputPayloadArray, $encPublicJwk); |
| 49 | + $decryptedJweString = $this->jweService->decryptSerializedJwe($serializedJweToken, $encPrivJwk); |
| 50 | + |
| 51 | + $outputPayloadArray = json_decode($decryptedJweString, true); |
| 52 | + $this->assertEquals($inputPayloadArray, $outputPayloadArray); |
| 53 | + } |
| 54 | +} |
0 commit comments