|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SpomkyLabs\JoseBundle\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Jose\Checker\CheckerManagerInterface; |
|
15
|
|
|
use Jose\Decrypter; |
|
16
|
|
|
use Jose\DecrypterInterface; |
|
17
|
|
|
use Jose\Encrypter; |
|
18
|
|
|
use Jose\EncrypterInterface; |
|
19
|
|
|
use Jose\Factory\CheckerManagerFactory; |
|
20
|
|
|
use Jose\JWTCreator; |
|
21
|
|
|
use Jose\JWTLoader; |
|
22
|
|
|
use Jose\Signer; |
|
23
|
|
|
use Jose\SignerInterface; |
|
24
|
|
|
use Jose\Verifier; |
|
25
|
|
|
use Jose\VerifierInterface; |
|
26
|
|
|
|
|
27
|
|
|
final class ServiceFactory |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \SpomkyLabs\JoseBundle\Service\AlgorithmManager |
|
31
|
|
|
*/ |
|
32
|
|
|
private $algorithm_manager; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \SpomkyLabs\JoseBundle\Service\CompressionManager |
|
36
|
|
|
*/ |
|
37
|
|
|
private $compression_manager; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \SpomkyLabs\JoseBundle\Service\CheckerManager |
|
41
|
|
|
*/ |
|
42
|
|
|
private $checker_manager; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* ServiceFactory constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \SpomkyLabs\JoseBundle\Service\AlgorithmManager $algorithm_manager |
|
48
|
|
|
* @param \SpomkyLabs\JoseBundle\Service\CompressionManager $compression_manager |
|
49
|
|
|
* @param \SpomkyLabs\JoseBundle\Service\CheckerManager $checker_manager |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(AlgorithmManager $algorithm_manager, CompressionManager $compression_manager, CheckerManager $checker_manager) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->algorithm_manager = $algorithm_manager; |
|
54
|
|
|
$this->compression_manager = $compression_manager; |
|
55
|
|
|
$this->checker_manager = $checker_manager; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string[] $selected_key_encryption_algorithms |
|
60
|
|
|
* @param string[] $selected_content_encryption_algorithms |
|
61
|
|
|
* @param string[] $selected_compression_methods |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Jose\EncrypterInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public function createEncrypter(array $selected_key_encryption_algorithms, array $selected_content_encryption_algorithms, array $selected_compression_methods) |
|
66
|
|
|
{ |
|
67
|
|
|
$key_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_key_encryption_algorithms); |
|
68
|
|
|
$content_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_content_encryption_algorithms); |
|
69
|
|
|
$compression_methods = $this->compression_manager->getSelectedCompressionMethods($selected_compression_methods); |
|
70
|
|
|
|
|
71
|
|
|
return Encrypter::createEncrypter($key_encryption_algorithms, $content_encryption_algorithms, $compression_methods); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string[] $selected_key_encryption_algorithms |
|
76
|
|
|
* @param string[] $selected_content_encryption_algorithms |
|
77
|
|
|
* @param string[] $selected_compression_methods |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Jose\DecrypterInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
public function createDecrypter(array $selected_key_encryption_algorithms, array $selected_content_encryption_algorithms, array $selected_compression_methods) |
|
82
|
|
|
{ |
|
83
|
|
|
$key_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_key_encryption_algorithms); |
|
84
|
|
|
$content_encryption_algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_content_encryption_algorithms); |
|
85
|
|
|
$compression_methods = $this->compression_manager->getSelectedCompressionMethods($selected_compression_methods); |
|
86
|
|
|
|
|
87
|
|
|
return Decrypter::createDecrypter($key_encryption_algorithms, $content_encryption_algorithms, $compression_methods); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string[] $selected_algorithms |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Jose\SignerInterface |
|
94
|
|
|
*/ |
|
95
|
|
|
public function createSigner(array $selected_algorithms) |
|
96
|
|
|
{ |
|
97
|
|
|
$algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms); |
|
98
|
|
|
|
|
99
|
|
|
return Signer::createSigner($algorithms); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string[] $selected_algorithms |
|
104
|
|
|
* |
|
105
|
|
|
* @return \Jose\VerifierInterface |
|
106
|
|
|
*/ |
|
107
|
|
|
public function createVerifier(array $selected_algorithms) |
|
108
|
|
|
{ |
|
109
|
|
|
$algorithms = $this->algorithm_manager->getSelectedAlgorithmMethods($selected_algorithms); |
|
110
|
|
|
|
|
111
|
|
|
return Verifier::createVerifier($algorithms); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string[] $selected_claim_checkers |
|
116
|
|
|
* @param string[] $selected_header_checkers |
|
117
|
|
|
* |
|
118
|
|
|
* @return \Jose\Checker\CheckerManagerInterface |
|
119
|
|
|
*/ |
|
120
|
|
|
public function createChecker(array $selected_claim_checkers, array $selected_header_checkers) |
|
121
|
|
|
{ |
|
122
|
|
|
$claim_checkers = $this->checker_manager->getSelectedClaimChecker($selected_claim_checkers); |
|
123
|
|
|
$header_checkers = $this->checker_manager->getSelectedHeaderChecker($selected_header_checkers); |
|
124
|
|
|
|
|
125
|
|
|
return CheckerManagerFactory::createClaimCheckerManager($claim_checkers, $header_checkers); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param \Jose\Checker\CheckerManagerInterface $checker_manager |
|
130
|
|
|
* @param \Jose\VerifierInterface $verifier |
|
131
|
|
|
* @param \Jose\DecrypterInterface|null $decrypter |
|
132
|
|
|
* |
|
133
|
|
|
* @return \Jose\JWTLoader |
|
134
|
|
|
*/ |
|
135
|
|
|
public function createJWTLoader(CheckerManagerInterface $checker_manager, VerifierInterface $verifier, DecrypterInterface $decrypter = null) |
|
136
|
|
|
{ |
|
137
|
|
|
$jwt_loader = new JWTLoader($checker_manager, $verifier); |
|
138
|
|
|
if (null !== $decrypter) { |
|
139
|
|
|
$jwt_loader->enableDecryptionSupport($decrypter); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $jwt_loader; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param \Jose\SignerInterface $signer |
|
147
|
|
|
* @param \Jose\EncrypterInterface|null $encrypter |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Jose\JWTCreator |
|
150
|
|
|
*/ |
|
151
|
|
|
public function createJWTCreator(SignerInterface $signer, EncrypterInterface $encrypter = null) |
|
152
|
|
|
{ |
|
153
|
|
|
$jwt_creator = new JWTCreator($signer); |
|
154
|
|
|
if (null !== $encrypter) { |
|
155
|
|
|
$jwt_creator->enableEncryptionSupport($encrypter); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $jwt_creator; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|