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 Jose; |
13
|
|
|
|
14
|
|
|
use Assert\Assertion; |
15
|
|
|
use Jose\Factory\JWEFactory; |
16
|
|
|
use Jose\Factory\JWSFactory; |
17
|
|
|
use Jose\Object\JWKInterface; |
18
|
|
|
|
19
|
|
|
final class JWTCreator |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Jose\EncrypterInterface|null |
23
|
|
|
*/ |
24
|
|
|
private $encrypter = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Jose\SignerInterface |
28
|
|
|
*/ |
29
|
|
|
private $signer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* JWTCreator constructor. |
33
|
|
|
* |
34
|
|
|
* @param \Jose\SignerInterface $signer |
35
|
|
|
*/ |
36
|
|
|
public function __construct(SignerInterface $signer) |
37
|
|
|
{ |
38
|
|
|
$this->signer = $signer; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \Jose\EncrypterInterface $encrypter |
43
|
|
|
*/ |
44
|
|
|
public function enableEncryptionSupport(EncrypterInterface $encrypter) |
45
|
|
|
{ |
46
|
|
|
$this->encrypter = $encrypter; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param mixed $payload |
51
|
|
|
* @param array $signature_protected_headers |
52
|
|
|
* @param \Jose\Object\JWKInterface $signature_key |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function sign($payload, array $signature_protected_headers, JWKInterface $signature_key) |
57
|
|
|
{ |
58
|
|
|
$jws = JWSFactory::createJWS($payload); |
59
|
|
|
|
60
|
|
|
$jws = $jws->addSignatureInformation($signature_key, $signature_protected_headers); |
61
|
|
|
$this->signer->sign($jws); |
62
|
|
|
|
63
|
|
|
return $jws->toCompactJSON(0); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $payload |
68
|
|
|
* @param array $encryption_protected_headers |
69
|
|
|
* @param \Jose\Object\JWKInterface $encryption_key |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function encrypt($payload, array $encryption_protected_headers, JWKInterface $encryption_key) |
74
|
|
|
{ |
75
|
|
|
Assertion::notNull($this->encrypter, 'The encryption support is not enabled'); |
76
|
|
|
|
77
|
|
|
$jwe = JWEFactory::createJWE($payload, $encryption_protected_headers); |
78
|
|
|
$jwe = $jwe->addRecipientInformation($encryption_key); |
79
|
|
|
$this->encrypter->encrypt($jwe); |
80
|
|
|
|
81
|
|
|
return $jwe->toCompactJSON(0); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string[] |
86
|
|
|
*/ |
87
|
|
|
public function getSupportedSignatureAlgorithms() |
88
|
|
|
{ |
89
|
|
|
return $this->signer->getSupportedSignatureAlgorithms(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string[] |
94
|
|
|
*/ |
95
|
|
|
public function getSupportedKeyEncryptionAlgorithms() |
96
|
|
|
{ |
97
|
|
|
return null === $this->encrypter ? [] : $this->encrypter->getSupportedKeyEncryptionAlgorithms(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string[] |
102
|
|
|
*/ |
103
|
|
|
public function getSupportedContentEncryptionAlgorithms() |
104
|
|
|
{ |
105
|
|
|
return null === $this->encrypter ? [] : $this->encrypter->getSupportedContentEncryptionAlgorithms(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string[] |
110
|
|
|
*/ |
111
|
|
|
public function getSupportedCompressionMethods() |
112
|
|
|
{ |
113
|
|
|
return null === $this->encrypter ? [] : $this->encrypter->getSupportedCompressionMethods(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|