|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\JwtFrameworkAdapter\Model; |
| 10 | + |
| 11 | +use Jose\Component\Signature\JWSBuilder; |
| 12 | +use Jose\Component\Signature\JWSLoaderFactory as LoaderFactory; |
| 13 | +use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory as JWSSerializerPool; |
| 14 | +use Magento\Framework\Jwt\EncryptionSettingsInterface; |
| 15 | +use Magento\Framework\Jwt\Exception\EncryptionException; |
| 16 | +use Magento\Framework\Jwt\Exception\JwtException; |
| 17 | +use Magento\Framework\Jwt\Exception\MalformedTokenException; |
| 18 | +use Magento\Framework\Jwt\HeaderInterface; |
| 19 | +use Magento\Framework\Jwt\Jwk; |
| 20 | +use Magento\Framework\Jwt\Jws\JwsInterface; |
| 21 | +use Magento\Framework\Jwt\Jws\JwsSignatureJwks; |
| 22 | +use Jose\Component\Core\JWK as AdapterJwk; |
| 23 | +use Jose\Component\Core\JWKSet as AdapterJwkSet; |
| 24 | + |
| 25 | +/** |
| 26 | + * Works with JWS. |
| 27 | + */ |
| 28 | +class JwsManager |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var JWSBuilder |
| 32 | + */ |
| 33 | + private $jwsBuilder; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var LoaderFactory |
| 37 | + */ |
| 38 | + private $jwsLoaderFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var JWSSerializerPool |
| 42 | + */ |
| 43 | + private $jwsSerializerFactory; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var JwsFactory |
| 47 | + */ |
| 48 | + private $jwsFactory; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param JwsBuilderFactory $builderFactory |
| 52 | + * @param JwsSerializerPoolFactory $serializerPoolFactory |
| 53 | + * @param JwsLoaderFactory $jwsLoaderFactory |
| 54 | + * @param JwsFactory $jwsFactory |
| 55 | + */ |
| 56 | + public function __construct( |
| 57 | + JwsBuilderFactory $builderFactory, |
| 58 | + JwsSerializerPoolFactory $serializerPoolFactory, |
| 59 | + JwsLoaderFactory $jwsLoaderFactory, |
| 60 | + JwsFactory $jwsFactory |
| 61 | + ) { |
| 62 | + $this->jwsBuilder = $builderFactory->create(); |
| 63 | + $this->jwsSerializerFactory = $serializerPoolFactory->create(); |
| 64 | + $this->jwsLoaderFactory = $jwsLoaderFactory->create(); |
| 65 | + $this->jwsFactory = $jwsFactory; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Generate JWS token. |
| 70 | + * |
| 71 | + * @param JwsInterface $jws |
| 72 | + * @param EncryptionSettingsInterface|JwsSignatureJwks $encryptionSettings |
| 73 | + * @return string |
| 74 | + * @throws JwtException |
| 75 | + */ |
| 76 | + public function build(JwsInterface $jws, EncryptionSettingsInterface $encryptionSettings): string |
| 77 | + { |
| 78 | + if (!$encryptionSettings instanceof JwsSignatureJwks) { |
| 79 | + throw new JwtException('Can only work with JWK encryption settings for JWS tokens'); |
| 80 | + } |
| 81 | + $signaturesCount = count($encryptionSettings->getJwkSet()->getKeys()); |
| 82 | + if ($jws->getProtectedHeaders() && count($jws->getProtectedHeaders()) !== $signaturesCount) { |
| 83 | + throw new MalformedTokenException('Number of headers must equal to number of JWKs'); |
| 84 | + } |
| 85 | + if ($jws->getUnprotectedHeaders() |
| 86 | + && count($jws->getUnprotectedHeaders()) !== $signaturesCount |
| 87 | + ) { |
| 88 | + throw new MalformedTokenException('There must be an equal number of protected and unprotected headers.'); |
| 89 | + } |
| 90 | + $builder = $this->jwsBuilder->create(); |
| 91 | + $builder = $builder->withPayload($jws->getPayload()->getContent()); |
| 92 | + for ($i = 0; $i < $signaturesCount; $i++) { |
| 93 | + $jwk = $encryptionSettings->getJwkSet()->getKeys()[$i]; |
| 94 | + $alg = $jwk->getAlgorithm(); |
| 95 | + if (!$alg) { |
| 96 | + throw new EncryptionException('Algorithm is required for JWKs'); |
| 97 | + } |
| 98 | + $protected = []; |
| 99 | + if ($jws->getPayload()->getContentType()) { |
| 100 | + $protected['cty'] = $jws->getPayload()->getContentType(); |
| 101 | + } |
| 102 | + if ($jws->getProtectedHeaders()) { |
| 103 | + $protected = $this->extractHeaderData($jws->getProtectedHeaders()[$i]); |
| 104 | + } |
| 105 | + $protected['alg'] = $alg; |
| 106 | + $unprotected = []; |
| 107 | + if ($jws->getUnprotectedHeaders()) { |
| 108 | + $unprotected = $this->extractHeaderData($jws->getUnprotectedHeaders()[$i]); |
| 109 | + } |
| 110 | + $builder = $builder->addSignature(new AdapterJwk($jwk->getJsonData()), $protected, $unprotected); |
| 111 | + } |
| 112 | + $jwsCreated = $builder->build(); |
| 113 | + |
| 114 | + if ($signaturesCount > 1) { |
| 115 | + return $this->jwsSerializerFactory->all()['jws_json_general']->serialize($jwsCreated); |
| 116 | + } |
| 117 | + if ($jws->getUnprotectedHeaders()) { |
| 118 | + return $this->jwsSerializerFactory->all()['jws_json_flattened']->serialize($jwsCreated); |
| 119 | + } |
| 120 | + return $this->jwsSerializerFactory->all()['jws_compact']->serialize($jwsCreated); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Read and verify JWS token. |
| 125 | + * |
| 126 | + * @param string $token |
| 127 | + * @param EncryptionSettingsInterface|JwsSignatureJwks $encryptionSettings |
| 128 | + * @return JwsInterface |
| 129 | + * @throws JwtException |
| 130 | + */ |
| 131 | + public function read(string $token, EncryptionSettingsInterface $encryptionSettings): JwsInterface |
| 132 | + { |
| 133 | + if (!$encryptionSettings instanceof JwsSignatureJwks) { |
| 134 | + throw new JwtException('Can only work with JWK settings for JWS tokens'); |
| 135 | + } |
| 136 | + |
| 137 | + $loader = $this->jwsLoaderFactory->create( |
| 138 | + ['jws_compact', 'jws_json_flattened', 'jws_json_general'], |
| 139 | + array_map( |
| 140 | + function (Jwk $jwk) { |
| 141 | + return $jwk->getAlgorithm(); |
| 142 | + }, |
| 143 | + $encryptionSettings->getJwkSet()->getKeys() |
| 144 | + ) |
| 145 | + ); |
| 146 | + $jwkSet = new AdapterJwkSet( |
| 147 | + array_map( |
| 148 | + function (Jwk $jwk) { |
| 149 | + return new AdapterJwk($jwk->getJsonData()); |
| 150 | + }, |
| 151 | + $encryptionSettings->getJwkSet()->getKeys() |
| 152 | + ) |
| 153 | + ); |
| 154 | + try { |
| 155 | + $jws = $loader->loadAndVerifyWithKeySet( |
| 156 | + $token, |
| 157 | + $jwkSet, |
| 158 | + $signature, |
| 159 | + null |
| 160 | + ); |
| 161 | + } catch (\Throwable $exception) { |
| 162 | + throw new MalformedTokenException('Failed to read JWS token', 0, $exception); |
| 163 | + } |
| 164 | + if ($signature === null) { |
| 165 | + throw new EncryptionException('Failed to verify a JWS token'); |
| 166 | + } |
| 167 | + $headers = $jws->getSignature($signature); |
| 168 | + if ($jws->isPayloadDetached()) { |
| 169 | + throw new JwtException('Detached payload is not supported'); |
| 170 | + } |
| 171 | + |
| 172 | + return $this->jwsFactory->create( |
| 173 | + $headers->getProtectedHeader(), |
| 174 | + $jws->getPayload(), |
| 175 | + $headers->getHeader() ? $headers->getHeader() : null |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Extract JOSE header data. |
| 181 | + * |
| 182 | + * @param HeaderInterface $header |
| 183 | + * @return array |
| 184 | + */ |
| 185 | + private function extractHeaderData(HeaderInterface $header): array |
| 186 | + { |
| 187 | + $data = []; |
| 188 | + foreach ($header->getParameters() as $parameter) { |
| 189 | + $data[$parameter->getName()] = $parameter->getValue(); |
| 190 | + } |
| 191 | + |
| 192 | + return $data; |
| 193 | + } |
| 194 | +} |
0 commit comments