1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the AppleApnPush package |
7
|
|
|
* |
8
|
|
|
* (c) Vitaliy Zhuk <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Apple\ApnPush\Jwt\SignatureGenerator; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Core\JWK as WebTokenComponentJwk; |
17
|
|
|
use Jose\Component\KeyManagement\JWKFactory as WebTokenComponentJWKFactory; |
18
|
|
|
use Jose\Component\Signature\JWS as WebTokenComponentJws; |
19
|
|
|
use Jose\Factory\JWKFactory; |
20
|
|
|
use Jose\Factory\JWSFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The factory for try to resolve the JWT signature generator. |
24
|
|
|
*/ |
25
|
|
|
class SignatureGeneratorFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array|callable[] |
29
|
|
|
*/ |
30
|
|
|
private static $resolvers = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add resolver for try to resolve the signature generator |
34
|
|
|
* |
35
|
|
|
* @param callable $resolver |
36
|
|
|
*/ |
37
|
|
|
public static function addResolver(callable $resolver): void |
38
|
|
|
{ |
39
|
|
|
array_unshift(self::$resolvers, $resolver); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Try to resolve the signature generator |
44
|
|
|
* |
45
|
|
|
* @return SignatureGeneratorInterface |
46
|
|
|
* |
47
|
|
|
* @throws \LogicException |
48
|
|
|
*/ |
49
|
|
|
public static function resolve(): SignatureGeneratorInterface |
50
|
|
|
{ |
51
|
|
|
self::addDefaultResolvers(); |
52
|
|
|
|
53
|
|
|
foreach (self::$resolvers as $resolver) { |
54
|
|
|
if ($generator = $resolver()) { |
55
|
|
|
return $generator; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw new \LogicException('Cannot resolve available JWT Signature Generator.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add default signature generator resolvers |
64
|
|
|
*/ |
65
|
|
|
private static function addDefaultResolvers(): void |
66
|
|
|
{ |
67
|
|
|
static $added = false; |
68
|
|
|
|
69
|
|
|
if ($added) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$added = true; |
74
|
|
|
|
75
|
|
|
self::addResolver([__CLASS__, 'tryResolveByWebTokenJwtSystem']); |
76
|
|
|
self::addResolver([__CLASS__, 'tryResolveBySpomkyLabsJoseSystem']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Try the resolve WebTokenJwtSignatureGenerator |
81
|
|
|
* |
82
|
|
|
* @return WebTokenJwtSignatureGenerator|null |
83
|
|
|
*/ |
84
|
|
|
private static function tryResolveByWebTokenJwtSystem(): ?WebTokenJwtSignatureGenerator |
85
|
|
|
{ |
86
|
|
|
$requiredClasses = [ |
87
|
|
|
WebTokenComponentJws::class, |
88
|
|
|
WebTokenComponentJwk::class, |
89
|
|
|
WebTokenComponentJWKFactory::class, |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
foreach ($requiredClasses as $requiredClass) { |
93
|
|
|
if (!class_exists($requiredClass)) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new WebTokenJwtSignatureGenerator(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Try to resolve SpomkyLabsJoseSignatureGenerator |
103
|
|
|
* |
104
|
|
|
* @return SpomkyLabsJoseSignatureGenerator|null |
105
|
|
|
*/ |
106
|
|
|
private static function tryResolveBySpomkyLabsJoseSystem(): ?SpomkyLabsJoseSignatureGenerator |
107
|
|
|
{ |
108
|
|
|
$requiredClasses = [ |
109
|
|
|
JWKFactory::class, |
110
|
|
|
JWSFactory::class, |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
foreach ($requiredClasses as $requiredClass) { |
114
|
|
|
if (!class_exists($requiredClass)) { |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return new SpomkyLabsJoseSignatureGenerator(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|