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\Factory; |
13
|
|
|
|
14
|
|
|
use Assert\Assertion; |
15
|
|
|
use Base64Url\Base64Url; |
16
|
|
|
use Jose\Object\JWKInterface; |
17
|
|
|
use Jose\Object\JWS; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
|
20
|
|
|
final class JWSFactory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param mixed $payload |
24
|
|
|
* |
25
|
|
|
* @return \Jose\Object\JWSInterface |
26
|
|
|
*/ |
27
|
|
|
public static function createEmptyJWS($payload) |
28
|
|
|
{ |
29
|
|
|
$jws = new JWS(); |
30
|
|
|
$jws = $jws->withPayload($payload); |
31
|
|
|
|
32
|
|
|
return $jws; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $payload |
37
|
|
|
* @param string $encoded_payload |
38
|
|
|
* |
39
|
|
|
* @return \Jose\Object\JWSInterface |
40
|
|
|
*/ |
41
|
|
|
public static function createEmptyJWSWithDetachedPayload($payload, &$encoded_payload) |
42
|
|
|
{ |
43
|
|
|
$encoded_payload = Base64Url::encode(is_string($payload) ? $payload : json_encode($payload)); |
44
|
|
|
|
45
|
|
|
return new JWS(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $payload |
50
|
|
|
* @param \Jose\Object\JWKInterface $signature_key |
51
|
|
|
* @param array $protected_headers |
52
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public static function createJWSToCompactJSON($payload, JWKInterface $signature_key, array $protected_headers, LoggerInterface $logger = null) |
57
|
|
|
{ |
58
|
|
|
$jws = self::createJWSAndSign($payload, $signature_key, $protected_headers, [], $logger); |
59
|
|
|
|
60
|
|
|
return $jws->toCompactJSON(0); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $payload |
65
|
|
|
* @param \Jose\Object\JWKInterface $signature_key |
66
|
|
|
* @param array $protected_headers |
67
|
|
|
* @param string $encoded_payload |
68
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public static function createJWSWithDetachedPayloadToCompactJSON($payload, JWKInterface $signature_key, &$encoded_payload, array $protected_headers, LoggerInterface $logger = null) |
73
|
|
|
{ |
74
|
|
|
$jws = self::createJWSWithDetachedPayloadAndSign($payload, $signature_key, $encoded_payload, $protected_headers, [], $logger); |
75
|
|
|
|
76
|
|
|
return $jws->toCompactJSON(0); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $payload |
81
|
|
|
* @param \Jose\Object\JWKInterface $signature_key |
82
|
|
|
* @param array $protected_headers |
83
|
|
|
* @param array $headers |
84
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public static function createJWSToFlattenedJSON($payload, JWKInterface $signature_key, array $protected_headers = [], $headers = [], LoggerInterface $logger = null) |
89
|
|
|
{ |
90
|
|
|
$jws = self::createJWSAndSign($payload, $signature_key, $protected_headers, $headers, $logger); |
91
|
|
|
|
92
|
|
|
return $jws->toFlattenedJSON(0); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $payload |
97
|
|
|
* @param \Jose\Object\JWKInterface $signature_key |
98
|
|
|
* @param string $encoded_payload |
99
|
|
|
* @param array $protected_headers |
100
|
|
|
* @param array $headers |
101
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public static function createJWSWithDetachedPayloadToFlattenedJSON($payload, JWKInterface $signature_key, &$encoded_payload, array $protected_headers = [], $headers = [], LoggerInterface $logger = null) |
106
|
|
|
{ |
107
|
|
|
$jws = self::createJWSWithDetachedPayloadAndSign($payload, $signature_key, $encoded_payload, $protected_headers, $headers, $logger); |
108
|
|
|
|
109
|
|
|
return $jws->toFlattenedJSON(0); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed $payload |
114
|
|
|
* @param \Jose\Object\JWKInterface $signature_key |
115
|
|
|
* @param array $protected_headers |
116
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
117
|
|
|
* |
118
|
|
|
* @return \Jose\Object\JWSInterface |
119
|
|
|
*/ |
120
|
|
|
private static function createJWSAndSign($payload, JWKInterface $signature_key, array $protected_headers = [], $headers = [], LoggerInterface $logger = null) |
121
|
|
|
{ |
122
|
|
|
$complete_headers = array_merge($protected_headers, $headers); |
123
|
|
|
Assertion::keyExists($complete_headers, 'alg', 'No "alg" parameter set in the header'); |
124
|
|
|
$signer = SignerFactory::createSigner([$complete_headers['alg']], $logger); |
125
|
|
|
|
126
|
|
|
$jws = self::createEmptyJWS($payload); |
127
|
|
|
|
128
|
|
|
$signer->addSignature($jws, $signature_key, $protected_headers, $headers); |
129
|
|
|
|
130
|
|
|
return $jws; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param mixed $payload |
135
|
|
|
* @param \Jose\Object\JWKInterface $signature_key |
136
|
|
|
* @param array $protected_headers |
137
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
138
|
|
|
* |
139
|
|
|
* @return \Jose\Object\JWSInterface |
140
|
|
|
*/ |
141
|
|
|
private static function createJWSWithDetachedPayloadAndSign($payload, JWKInterface $signature_key, &$encoded_payload, array $protected_headers = [], $headers = [], LoggerInterface $logger = null) |
142
|
|
|
{ |
143
|
|
|
$complete_headers = array_merge($protected_headers, $headers); |
144
|
|
|
Assertion::keyExists($complete_headers, 'alg', 'No "alg" parameter set in the header'); |
145
|
|
|
$signer = SignerFactory::createSigner([$complete_headers['alg']], $logger); |
146
|
|
|
|
147
|
|
|
$jws = self::createEmptyJWSWithDetachedPayload($payload, $encoded_payload); |
148
|
|
|
|
149
|
|
|
$signer->addSignatureWithDetachedPayload($jws, $signature_key, $encoded_payload, $protected_headers, $headers); |
150
|
|
|
|
151
|
|
|
return $jws; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|