|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\JwtFrameworkAdapter\Model; |
| 7 | + |
| 8 | +class JwsManagerTest extends \PHPUnit\Framework\TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Magento\Framework\ObjectManagerInterface |
| 12 | + */ |
| 13 | + private $objectManager; |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 18 | + |
| 19 | + parent::setUp(); |
| 20 | + } |
| 21 | + |
| 22 | + public function testCreatingJwsWithAlgorithmSetInHeadersDirectly(): void |
| 23 | + { |
| 24 | + $secret = "ZXF1YXRpb24tS2VudHVja3ktY29udGludWVkLWRpZmZlcmVuY2U="; |
| 25 | + $payload = json_encode([ |
| 26 | + 'MyCustomClaim' => 'some value', // not important at all |
| 27 | + 'nbf' => time(), |
| 28 | + 'exp' => time() + 600, |
| 29 | + 'iat' => time() |
| 30 | + ]); |
| 31 | + $header = [ |
| 32 | + 'alg' => 'HS256', |
| 33 | + 'typ' => 'JWT' |
| 34 | + ]; |
| 35 | + |
| 36 | + /** @var \Magento\Framework\Jwt\JwkFactory $jwkFactory */ |
| 37 | + $jwkFactory = $this->objectManager->create(\Magento\Framework\Jwt\JwkFactory::class); |
| 38 | + $jwk = $jwkFactory->createFromData(['kty' => 'oct', 'k' => $secret]); |
| 39 | + |
| 40 | + /** @var \Magento\JwtFrameworkAdapter\Model\JwsFactory $jwsFactory */ |
| 41 | + $jwsFactory = $this->objectManager->create(\Magento\JwtFrameworkAdapter\Model\JwsFactory::class); |
| 42 | + $jws = $jwsFactory->create($header, $payload, null); |
| 43 | + |
| 44 | + /** @var \Magento\Framework\Jwt\Jws\JwsSignatureSettingsInterface $encryptionSettings */ |
| 45 | + $encryptionSettings = $this->objectManager->create( |
| 46 | + \Magento\Framework\Jwt\Jws\JwsSignatureJwks::class, |
| 47 | + [ |
| 48 | + 'jwk' => $jwk |
| 49 | + ] |
| 50 | + ); |
| 51 | + |
| 52 | + /** @var \Magento\JwtFrameworkAdapter\Model\JwsManager $jwsManager */ |
| 53 | + $jwsManager = $this->objectManager->create(\Magento\JwtFrameworkAdapter\Model\JwsManager::class); |
| 54 | + |
| 55 | + $token = $jwsManager->build($jws, $encryptionSettings); |
| 56 | + |
| 57 | + $this->assertIsString($token); |
| 58 | + } |
| 59 | +} |
0 commit comments