1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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
|
|
|
class JWESerializerManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var JWESerializer[] |
22
|
|
|
*/ |
23
|
|
|
private $serializers = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* JWESerializerManager constructor. |
27
|
|
|
* |
28
|
|
|
* @param JWESerializer[] $serializers |
29
|
|
|
*/ |
30
|
|
|
private function __construct(array $serializers) |
31
|
|
|
{ |
32
|
|
|
foreach ($serializers as $serializer) { |
33
|
|
|
$this->add($serializer); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates a serializer manager using the given serializers. |
39
|
|
|
* |
40
|
|
|
* @param JWESerializer[] $serializers |
41
|
|
|
* |
42
|
|
|
* @return JWESerializerManager |
43
|
|
|
*/ |
44
|
|
|
public static function create(array $serializers): self |
45
|
|
|
{ |
46
|
|
|
return new self($serializers); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Adds a serializer to the manager. |
51
|
|
|
* |
52
|
|
|
* @param JWESerializer $serializer |
53
|
|
|
* |
54
|
|
|
* @return JWESerializerManager |
55
|
|
|
*/ |
56
|
|
|
private function add(JWESerializer $serializer): self |
57
|
|
|
{ |
58
|
|
|
$this->serializers[$serializer->name()] = $serializer; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the serializer names supported by the manager. |
65
|
|
|
* |
66
|
|
|
* @deprecated Will be removed in v2.0. Please use names() instead. |
67
|
|
|
* |
68
|
|
|
* @return string[] |
69
|
|
|
*/ |
70
|
|
|
public function list(): array |
71
|
|
|
{ |
72
|
|
|
return $this->names(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return the serializer names supported by the manager. |
77
|
|
|
* |
78
|
|
|
* @return string[] |
79
|
|
|
*/ |
80
|
|
|
public function names(): array |
81
|
|
|
{ |
82
|
|
|
return array_keys($this->serializers); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Converts a JWE into a string. |
87
|
|
|
* Throws an exception if none of the serializer was able to convert the input. |
88
|
|
|
* |
89
|
|
|
* @param string $name |
90
|
|
|
* @param JWE $jws |
91
|
|
|
* @param int|null $recipientIndex |
92
|
|
|
* |
93
|
|
|
* @throws \Exception |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function serialize(string $name, JWE $jws, ?int $recipientIndex = null): string |
98
|
|
|
{ |
99
|
|
|
if (!array_key_exists($name, $this->serializers)) { |
100
|
|
|
throw new \InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return ($this->serializers[$name])->serialize($jws, $recipientIndex); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Loads data and return a JWE object. |
108
|
|
|
* Throws an exception if none of the serializer was able to convert the input. |
109
|
|
|
* |
110
|
|
|
* @param string $input A string that represents a JWE |
111
|
|
|
* @param string|null $name the name of the serializer if the input is unserialized |
112
|
|
|
* |
113
|
|
|
* @throws \Exception |
114
|
|
|
* |
115
|
|
|
* @return JWE |
116
|
|
|
*/ |
117
|
|
|
public function unserialize(string $input, ?string &$name = null): JWE |
118
|
|
|
{ |
119
|
|
|
foreach ($this->serializers as $serializer) { |
120
|
|
|
try { |
121
|
|
|
$jws = $serializer->unserialize($input); |
122
|
|
|
$name = $serializer->name(); |
123
|
|
|
|
124
|
|
|
return $jws; |
125
|
|
|
} catch (\InvalidArgumentException $e) { |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
throw new \InvalidArgumentException('Unsupported input.'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|