[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare(strict_types=1); 4 5 /* 6 * The MIT License (MIT) 7 * 8 * Copyright (c) 2014-2020 Spomky-Labs 9 * 10 * This software may be modified and distributed under the terms 11 * of the MIT license. See the LICENSE file for details. 12 */ 13 14 namespace Jose\Component\Signature\Serializer; 15 16 use Base64Url\Base64Url; 17 use function count; 18 use InvalidArgumentException; 19 use function is_array; 20 use Jose\Component\Core\Util\JsonConverter; 21 use Jose\Component\Signature\JWS; 22 23 final class JSONFlattenedSerializer extends Serializer 24 { 25 public const NAME = 'jws_json_flattened'; 26 27 public function displayName(): string 28 { 29 return 'JWS JSON Flattened'; 30 } 31 32 public function name(): string 33 { 34 return self::NAME; 35 } 36 37 public function serialize(JWS $jws, ?int $signatureIndex = null): string 38 { 39 if (null === $signatureIndex) { 40 $signatureIndex = 0; 41 } 42 $signature = $jws->getSignature($signatureIndex); 43 44 $data = []; 45 $values = [ 46 'payload' => $jws->getEncodedPayload(), 47 'protected' => $signature->getEncodedProtectedHeader(), 48 'header' => $signature->getHeader(), 49 ]; 50 $encodedPayload = $jws->getEncodedPayload(); 51 if (null !== $encodedPayload && '' !== $encodedPayload) { 52 $data['payload'] = $encodedPayload; 53 } 54 $encodedProtectedHeader = $signature->getEncodedProtectedHeader(); 55 if (null !== $encodedProtectedHeader && '' !== $encodedProtectedHeader) { 56 $data['protected'] = $encodedProtectedHeader; 57 } 58 $header = $signature->getHeader(); 59 if (0 !== count($header)) { 60 $data['header'] = $header; 61 } 62 $data['signature'] = Base64Url::encode($signature->getSignature()); 63 64 return JsonConverter::encode($data); 65 } 66 67 /** 68 * @throws InvalidArgumentException if the input is not supported 69 * @throws InvalidArgumentException if the JWS header is invalid 70 */ 71 public function unserialize(string $input): JWS 72 { 73 $data = JsonConverter::decode($input); 74 if (!is_array($data)) { 75 throw new InvalidArgumentException('Unsupported input.'); 76 } 77 if (!isset($data['signature'])) { 78 throw new InvalidArgumentException('Unsupported input.'); 79 } 80 $signature = Base64Url::decode($data['signature']); 81 82 if (isset($data['protected'])) { 83 $encodedProtectedHeader = $data['protected']; 84 $protectedHeader = JsonConverter::decode(Base64Url::decode($data['protected'])); 85 } else { 86 $encodedProtectedHeader = null; 87 $protectedHeader = []; 88 } 89 if (isset($data['header'])) { 90 if (!is_array($data['header'])) { 91 throw new InvalidArgumentException('Bad header.'); 92 } 93 $header = $data['header']; 94 } else { 95 $header = []; 96 } 97 98 if (isset($data['payload'])) { 99 $encodedPayload = $data['payload']; 100 $payload = $this->isPayloadEncoded($protectedHeader) ? Base64Url::decode($encodedPayload) : $encodedPayload; 101 } else { 102 $payload = null; 103 $encodedPayload = null; 104 } 105 106 $jws = new JWS($payload, $encodedPayload, null === $encodedPayload); 107 108 return $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader, $header); 109 } 110 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |