1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 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\Encryption\Serializer; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Encryption\JWE; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class JWESerializationManager. |
20
|
|
|
*/ |
21
|
|
|
final class JWESerializerManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var JWESerializerInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $serializers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* JWESerializerManager constructor. |
30
|
|
|
* |
31
|
|
|
* @param JWESerializerInterface[] $serializers |
32
|
|
|
*/ |
33
|
|
|
private function __construct(array $serializers) |
34
|
|
|
{ |
35
|
|
|
foreach ($serializers as $serializer) { |
36
|
|
|
$this->add($serializer); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param JWESerializerInterface[] $serializers |
42
|
|
|
* |
43
|
|
|
* @return JWESerializerManager |
44
|
|
|
*/ |
45
|
|
|
public static function create(array $serializers): JWESerializerManager |
46
|
|
|
{ |
47
|
|
|
return new self($serializers); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param JWESerializerInterface $serializer |
52
|
|
|
* |
53
|
|
|
* @return JWESerializerManager |
54
|
|
|
*/ |
55
|
|
|
private function add(JWESerializerInterface $serializer): JWESerializerManager |
56
|
|
|
{ |
57
|
|
|
$this->serializers[$serializer->name()] = $serializer; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Converts a JWE into a string. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @param JWE $jws |
67
|
|
|
* @param int|null $recipientIndex |
68
|
|
|
* |
69
|
|
|
* @throws \Exception |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function serialize(string $name, JWE $jws, ?int $recipientIndex = null): string |
74
|
|
|
{ |
75
|
|
|
if (!array_key_exists($name, $this->serializers)) { |
76
|
|
|
throw new \InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return ($this->serializers[$name])->serialize($jws, $recipientIndex); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Loads data and return a JWE object. |
84
|
|
|
* |
85
|
|
|
* @param string $input A string that represents a JWE |
86
|
|
|
* @param string|null $name The name of the serializer if the input is unserialized. |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
* |
90
|
|
|
* @return JWE |
91
|
|
|
*/ |
92
|
|
|
public function unserialize(string $input, ?string &$name = null): JWE |
93
|
|
|
{ |
94
|
|
|
foreach ($this->serializers as $serializer) { |
95
|
|
|
try { |
96
|
|
|
$jws = $serializer->unserialize($input); |
97
|
|
|
$name = $serializer->name(); |
98
|
|
|
|
99
|
|
|
return $jws; |
100
|
|
|
} catch (\InvalidArgumentException $e) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
throw new \InvalidArgumentException('Unsupported input.'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|