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
|
|
|
self::addResolver([__CLASS__, 'tryResolveByWebTokenJwtSystem']); |
74
|
|
|
self::addResolver([__CLASS__, 'tryResolveBySpomkyLabsJoseSystem']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Try the resolve WebTokenJwtSignatureGenerator |
79
|
|
|
* |
80
|
|
|
* @return WebTokenJwtSignatureGenerator|null |
81
|
|
|
*/ |
82
|
|
|
private static function tryResolveByWebTokenJwtSystem(): ?WebTokenJwtSignatureGenerator |
83
|
|
|
{ |
84
|
|
|
$requiredClasses = [ |
85
|
|
|
WebTokenComponentJws::class, |
86
|
|
|
WebTokenComponentJwk::class, |
87
|
|
|
WebTokenComponentJWKFactory::class, |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
foreach ($requiredClasses as $requiredClass) { |
91
|
|
|
if (!class_exists($requiredClass)) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new WebTokenJwtSignatureGenerator(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Try to resolve SpomkyLabsJoseSignatureGenerator |
101
|
|
|
* |
102
|
|
|
* @return SpomkyLabsJoseSignatureGenerator|null |
103
|
|
|
*/ |
104
|
|
|
private static function tryResolveBySpomkyLabsJoseSystem(): ?SpomkyLabsJoseSignatureGenerator |
105
|
|
|
{ |
106
|
|
|
$requiredClasses = [ |
107
|
|
|
JWKFactory::class, |
108
|
|
|
JWSFactory::class, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
foreach ($requiredClasses as $requiredClass) { |
112
|
|
|
if (!class_exists($requiredClass)) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return new SpomkyLabsJoseSignatureGenerator(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|