[ 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; 15 16 use function count; 17 use InvalidArgumentException; 18 use Jose\Component\Core\JWT; 19 20 class JWS implements JWT 21 { 22 /** 23 * @var bool 24 */ 25 private $isPayloadDetached = false; 26 27 /** 28 * @var null|string 29 */ 30 private $encodedPayload; 31 32 /** 33 * @var Signature[] 34 */ 35 private $signatures = []; 36 37 /** 38 * @var null|string 39 */ 40 private $payload; 41 42 public function __construct(?string $payload, ?string $encodedPayload = null, bool $isPayloadDetached = false) 43 { 44 $this->payload = $payload; 45 $this->encodedPayload = $encodedPayload; 46 $this->isPayloadDetached = $isPayloadDetached; 47 } 48 49 public function getPayload(): ?string 50 { 51 return $this->payload; 52 } 53 54 /** 55 * Returns true if the payload is detached. 56 */ 57 public function isPayloadDetached(): bool 58 { 59 return $this->isPayloadDetached; 60 } 61 62 /** 63 * Returns the Base64Url encoded payload. 64 * If the payload is detached, this method returns null. 65 */ 66 public function getEncodedPayload(): ?string 67 { 68 if (true === $this->isPayloadDetached()) { 69 return null; 70 } 71 72 return $this->encodedPayload; 73 } 74 75 /** 76 * Returns the signatures associated with the JWS. 77 * 78 * @return Signature[] 79 */ 80 public function getSignatures(): array 81 { 82 return $this->signatures; 83 } 84 85 /** 86 * Returns the signature at the given index. 87 * 88 * @throws InvalidArgumentException if the signature index does not exist 89 */ 90 public function getSignature(int $id): Signature 91 { 92 if (isset($this->signatures[$id])) { 93 return $this->signatures[$id]; 94 } 95 96 throw new InvalidArgumentException('The signature does not exist.'); 97 } 98 99 /** 100 * This method adds a signature to the JWS object. 101 * Its returns a new JWS object. 102 * 103 * @internal 104 * 105 * @return JWS 106 */ 107 public function addSignature(string $signature, array $protectedHeader, ?string $encodedProtectedHeader, array $header = []): self 108 { 109 $jws = clone $this; 110 $jws->signatures[] = new Signature($signature, $protectedHeader, $encodedProtectedHeader, $header); 111 112 return $jws; 113 } 114 115 /** 116 * Returns the number of signature associated with the JWS. 117 */ 118 public function countSignatures(): int 119 { 120 return count($this->signatures); 121 } 122 123 /** 124 * This method splits the JWS into a list of JWSs. 125 * It is only useful when the JWS contains more than one signature (JSON General Serialization). 126 * 127 * @return JWS[] 128 */ 129 public function split(): array 130 { 131 $result = []; 132 foreach ($this->signatures as $signature) { 133 $jws = new self( 134 $this->payload, 135 $this->encodedPayload, 136 $this->isPayloadDetached 137 ); 138 $jws = $jws->addSignature( 139 $signature->getSignature(), 140 $signature->getProtectedHeader(), 141 $signature->getEncodedProtectedHeader(), 142 $signature->getHeader() 143 ); 144 145 $result[] = $jws; 146 } 147 148 return $result; 149 } 150 }
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 |