[ 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 Exception; 17 use Jose\Component\Checker\HeaderCheckerManager; 18 use Jose\Component\Core\JWK; 19 use Jose\Component\Core\JWKSet; 20 use Jose\Component\Signature\Serializer\JWSSerializerManager; 21 use Throwable; 22 23 class JWSLoader 24 { 25 /** 26 * @var JWSVerifier 27 */ 28 private $jwsVerifier; 29 30 /** 31 * @var null|HeaderCheckerManager 32 */ 33 private $headerCheckerManager; 34 35 /** 36 * @var JWSSerializerManager 37 */ 38 private $serializerManager; 39 40 /** 41 * JWSLoader constructor. 42 */ 43 public function __construct(JWSSerializerManager $serializerManager, JWSVerifier $jwsVerifier, ?HeaderCheckerManager $headerCheckerManager) 44 { 45 $this->serializerManager = $serializerManager; 46 $this->jwsVerifier = $jwsVerifier; 47 $this->headerCheckerManager = $headerCheckerManager; 48 } 49 50 /** 51 * Returns the JWSVerifier associated to the JWSLoader. 52 */ 53 public function getJwsVerifier(): JWSVerifier 54 { 55 return $this->jwsVerifier; 56 } 57 58 /** 59 * Returns the Header Checker Manager associated to the JWSLoader. 60 */ 61 public function getHeaderCheckerManager(): ?HeaderCheckerManager 62 { 63 return $this->headerCheckerManager; 64 } 65 66 /** 67 * Returns the JWSSerializer associated to the JWSLoader. 68 */ 69 public function getSerializerManager(): JWSSerializerManager 70 { 71 return $this->serializerManager; 72 } 73 74 /** 75 * This method will try to load and verify the token using the given key. 76 * It returns a JWS and will populate the $signature variable in case of success, otherwise an exception is thrown. 77 * 78 * @throws Exception if the token cannot be loaded or verified 79 */ 80 public function loadAndVerifyWithKey(string $token, JWK $key, ?int &$signature, ?string $payload = null): JWS 81 { 82 $keyset = new JWKSet([$key]); 83 84 return $this->loadAndVerifyWithKeySet($token, $keyset, $signature, $payload); 85 } 86 87 /** 88 * This method will try to load and verify the token using the given key set. 89 * It returns a JWS and will populate the $signature variable in case of success, otherwise an exception is thrown. 90 * 91 * @throws Exception if the token cannot be loaded or verified 92 */ 93 public function loadAndVerifyWithKeySet(string $token, JWKSet $keyset, ?int &$signature, ?string $payload = null): JWS 94 { 95 try { 96 $jws = $this->serializerManager->unserialize($token); 97 $nbSignatures = $jws->countSignatures(); 98 for ($i = 0; $i < $nbSignatures; ++$i) { 99 if ($this->processSignature($jws, $keyset, $i, $payload)) { 100 $signature = $i; 101 102 return $jws; 103 } 104 } 105 } catch (Throwable $e) { 106 // Nothing to do. Exception thrown just after 107 } 108 109 throw new Exception('Unable to load and verify the token.'); 110 } 111 112 private function processSignature(JWS $jws, JWKSet $keyset, int $signature, ?string $payload): bool 113 { 114 try { 115 if (null !== $this->headerCheckerManager) { 116 $this->headerCheckerManager->check($jws, $signature); 117 } 118 119 return $this->jwsVerifier->verifyWithKeySet($jws, $keyset, $signature, $payload); 120 } catch (Throwable $e) { 121 return false; 122 } 123 } 124 }
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 |