@@ -72,14 +72,14 @@ discard block |
||
72 | 72 | |
73 | 73 | $claim_set = null; |
74 | 74 | |
75 | - if(!is_null($payload) && $payload->isClaimSet() && $payload instanceof IJWSPayloadClaimSetSpec) { |
|
75 | + if (!is_null($payload) && $payload->isClaimSet() && $payload instanceof IJWSPayloadClaimSetSpec) { |
|
76 | 76 | $header->addHeader(new JOSEHeaderParam(RegisteredJOSEHeaderNames::Type, new StringOrURI('JWT'))); |
77 | 77 | $claim_set = $payload->getClaimSet(); |
78 | 78 | } |
79 | 79 | |
80 | 80 | parent::__construct($header, $claim_set); |
81 | 81 | |
82 | - if(!is_null($payload)) |
|
82 | + if (!is_null($payload)) |
|
83 | 83 | $this->setPayload($payload); |
84 | 84 | |
85 | 85 | $this->signature = $signature; |
@@ -100,21 +100,18 @@ discard block |
||
100 | 100 | */ |
101 | 101 | public function toCompactSerialization() |
102 | 102 | { |
103 | - if(!is_null($this->jwk->getId())) |
|
103 | + if (!is_null($this->jwk->getId())) |
|
104 | 104 | $this->header->addHeader(new JOSEHeaderParam(RegisteredJOSEHeaderNames::KeyID, $this->jwk->getId())); |
105 | 105 | |
106 | - if($this->jwk instanceof IAsymmetricJWK) |
|
106 | + if ($this->jwk instanceof IAsymmetricJWK) |
|
107 | 107 | { |
108 | 108 | // we should add the public key on the header |
109 | 109 | $public_key = clone $this->jwk; |
110 | 110 | |
111 | - $this->header->addHeader |
|
112 | - ( |
|
113 | - new JOSEHeaderParam |
|
114 | - ( |
|
111 | + $this->header->addHeader( |
|
112 | + new JOSEHeaderParam( |
|
115 | 113 | RegisteredJOSEHeaderNames::JSONWebKey, |
116 | - new JsonValue |
|
117 | - ( |
|
114 | + new JsonValue( |
|
118 | 115 | $public_key->setVisibility(JSONWebKeyVisibility::PublicOnly) |
119 | 116 | ) |
120 | 117 | ) |
@@ -134,32 +131,32 @@ discard block |
||
134 | 131 | public function sign() |
135 | 132 | { |
136 | 133 | |
137 | - if(is_null($this->jwk)) |
|
134 | + if (is_null($this->jwk)) |
|
138 | 135 | throw new JWSInvalidJWKException; |
139 | 136 | |
140 | - if($this->jwk->getKeyUse()->getString() !== JSONWebKeyPublicKeyUseValues::Signature) |
|
137 | + if ($this->jwk->getKeyUse()->getString() !== JSONWebKeyPublicKeyUseValues::Signature) |
|
141 | 138 | throw new JWSInvalidJWKException(sprintf('use %s not supported.', $this->jwk->getKeyUse()->getString())); |
142 | 139 | |
143 | 140 | $alg = DigitalSignatures_MACs_Registry::getInstance()->get($this->header->getAlgorithm()->getString()); |
144 | 141 | |
145 | - if(is_null($alg)) |
|
146 | - throw new JWSNotSupportedAlgorithm(sprintf('alg %s.',$this->header->getAlgorithm()->getString())); |
|
142 | + if (is_null($alg)) |
|
143 | + throw new JWSNotSupportedAlgorithm(sprintf('alg %s.', $this->header->getAlgorithm()->getString())); |
|
147 | 144 | |
148 | - $secured_input_bytes = JOSEHeaderSerializer::serialize($this->header) . IBasicJWT::SegmentSeparator .$this->getEncodedPayload(); |
|
145 | + $secured_input_bytes = JOSEHeaderSerializer::serialize($this->header).IBasicJWT::SegmentSeparator.$this->getEncodedPayload(); |
|
149 | 146 | |
150 | - $key = $this->jwk->getKey(JSONWebKeyKeyOperationsValues::ComputeDigitalSignatureOrMAC); |
|
147 | + $key = $this->jwk->getKey(JSONWebKeyKeyOperationsValues::ComputeDigitalSignatureOrMAC); |
|
151 | 148 | |
152 | - if($alg instanceof DigitalSignatureAlgorithm) |
|
149 | + if ($alg instanceof DigitalSignatureAlgorithm) |
|
153 | 150 | { |
154 | 151 | $this->signature = $alg->sign($key, $secured_input_bytes); |
155 | 152 | } |
156 | - else if($alg instanceof MAC_Algorithm ) |
|
153 | + else if ($alg instanceof MAC_Algorithm) |
|
157 | 154 | { |
158 | 155 | $this->signature = $alg->digest($key, $secured_input_bytes); |
159 | 156 | } |
160 | 157 | else |
161 | 158 | { |
162 | - throw new JWSNotSupportedAlgorithm(sprintf('alg %s.',$this->header->getAlgorithm()->getString())); |
|
159 | + throw new JWSNotSupportedAlgorithm(sprintf('alg %s.', $this->header->getAlgorithm()->getString())); |
|
163 | 160 | } |
164 | 161 | |
165 | 162 | return $this; |
@@ -171,16 +168,16 @@ discard block |
||
171 | 168 | */ |
172 | 169 | public function getEncodedPayload() |
173 | 170 | { |
174 | - if(is_null($this->payload)) |
|
171 | + if (is_null($this->payload)) |
|
175 | 172 | throw new JWSInvalidPayloadException('payload is not set!'); |
176 | 173 | |
177 | 174 | $enc_payload = ''; |
178 | 175 | |
179 | - if($this->payload instanceof IJWSPayloadClaimSetSpec) |
|
176 | + if ($this->payload instanceof IJWSPayloadClaimSetSpec) |
|
180 | 177 | { |
181 | 178 | $enc_payload = JWTClaimSetSerializer::serialize($this->payload->getClaimSet()); |
182 | 179 | } |
183 | - else if($this->payload instanceof IJWSPayloadRawSpec) |
|
180 | + else if ($this->payload instanceof IJWSPayloadRawSpec) |
|
184 | 181 | { |
185 | 182 | $enc_payload = JWTRawSerializer::serialize($this->payload->getRaw()); |
186 | 183 | } |
@@ -234,27 +231,23 @@ discard block |
||
234 | 231 | */ |
235 | 232 | public function verify($original_alg) |
236 | 233 | { |
237 | - if(is_null($this->jwk)) |
|
234 | + if (is_null($this->jwk)) |
|
238 | 235 | throw new JWSInvalidJWKException; |
239 | 236 | |
240 | - if($this->jwk->getKeyUse()->getString() !== JSONWebKeyPublicKeyUseValues::Signature) |
|
241 | - throw new JWSInvalidJWKException |
|
242 | - ( |
|
243 | - sprintf |
|
244 | - ( |
|
237 | + if ($this->jwk->getKeyUse()->getString() !== JSONWebKeyPublicKeyUseValues::Signature) |
|
238 | + throw new JWSInvalidJWKException( |
|
239 | + sprintf( |
|
245 | 240 | 'use %s not supported ', |
246 | 241 | $this->jwk->getKeyUse()->getString() |
247 | 242 | ) |
248 | 243 | ); |
249 | 244 | |
250 | - if(is_null($this->jwk->getAlgorithm())) |
|
245 | + if (is_null($this->jwk->getAlgorithm())) |
|
251 | 246 | throw new InvalidJWKAlgorithm('algorithm intended for use with the key is not set! '); |
252 | 247 | |
253 | - if(!is_null($this->jwk->getId()) && !is_null($this->header->getKeyID()) && $this->header->getKeyID()->getValue() != $this->jwk->getId()->getValue()) |
|
254 | - throw new JWSInvalidJWKException |
|
255 | - ( |
|
256 | - sprintf |
|
257 | - ( |
|
248 | + if (!is_null($this->jwk->getId()) && !is_null($this->header->getKeyID()) && $this->header->getKeyID()->getValue() != $this->jwk->getId()->getValue()) |
|
249 | + throw new JWSInvalidJWKException( |
|
250 | + sprintf( |
|
258 | 251 | 'original kid %s - current kid %s', |
259 | 252 | $this->header->getKeyID()->getValue(), |
260 | 253 | $this->jwk->getId()->getValue() |
@@ -263,34 +256,30 @@ discard block |
||
263 | 256 | |
264 | 257 | $alg = DigitalSignatures_MACs_Registry::getInstance()->get($original_alg); |
265 | 258 | |
266 | - if(is_null($alg)) |
|
259 | + if (is_null($alg)) |
|
267 | 260 | throw new JWSNotSupportedAlgorithm(sprintf('algo %s', $original_alg)); |
268 | 261 | |
269 | 262 | $former_alg = $this->header->getAlgorithm()->getString(); |
270 | 263 | |
271 | - if($former_alg != $original_alg) |
|
272 | - throw new JWSNotSupportedAlgorithm |
|
273 | - ( |
|
274 | - sprintf |
|
275 | - ( |
|
264 | + if ($former_alg != $original_alg) |
|
265 | + throw new JWSNotSupportedAlgorithm( |
|
266 | + sprintf( |
|
276 | 267 | 'former alg %s - original alg %s', |
277 | 268 | $former_alg, |
278 | 269 | $original_alg |
279 | 270 | ) |
280 | 271 | ); |
281 | 272 | |
282 | - if($this->jwk->getAlgorithm()->getValue() !== $original_alg) |
|
283 | - throw new InvalidJWKAlgorithm |
|
284 | - ( |
|
285 | - sprintf |
|
286 | - ( |
|
273 | + if ($this->jwk->getAlgorithm()->getValue() !== $original_alg) |
|
274 | + throw new InvalidJWKAlgorithm( |
|
275 | + sprintf( |
|
287 | 276 | 'mismatch between algorithm intended for use with the key %s and the cryptographic algorithm used to secure the JWS %s', |
288 | 277 | $this->jwk->getAlgorithm()->getValue(), |
289 | 278 | $original_alg |
290 | 279 | ) |
291 | 280 | ); |
292 | 281 | |
293 | - $secured_input_bytes = JOSEHeaderSerializer::serialize($this->header) . IBasicJWT::SegmentSeparator .$this->getEncodedPayload(); |
|
282 | + $secured_input_bytes = JOSEHeaderSerializer::serialize($this->header).IBasicJWT::SegmentSeparator.$this->getEncodedPayload(); |
|
294 | 283 | |
295 | 284 | // use public key / secret |
296 | 285 | $key = $this->jwk->getKey(JSONWebKeyKeyOperationsValues::VerifyDigitalSignatureOrMAC); |
@@ -311,9 +300,9 @@ discard block |
||
311 | 300 | * @param string $signature |
312 | 301 | * @return IJWS |
313 | 302 | */ |
314 | - static public function fromHeaderClaimsAndSignature(IJOSEHeader $header, IJWSPayloadSpec $payload = null , $signature = '') |
|
303 | + static public function fromHeaderClaimsAndSignature(IJOSEHeader $header, IJWSPayloadSpec $payload = null, $signature = '') |
|
315 | 304 | { |
316 | - return new JWS($header, $payload, $signature ); |
|
305 | + return new JWS($header, $payload, $signature); |
|
317 | 306 | } |
318 | 307 | |
319 | 308 | /** |
@@ -321,9 +310,8 @@ discard block |
||
321 | 310 | */ |
322 | 311 | public function take() |
323 | 312 | { |
324 | - $payload = ($this->payload instanceof IJWSPayloadRawSpec) ? $this->payload->getRaw() : $this->claim_set; |
|
325 | - return array |
|
326 | - ( |
|
313 | + $payload = ($this->payload instanceof IJWSPayloadRawSpec) ? $this->payload->getRaw() : $this->claim_set; |
|
314 | + return array( |
|
327 | 315 | $this->header, |
328 | 316 | $payload, |
329 | 317 | $this->signature |
@@ -31,7 +31,7 @@ |
||
31 | 31 | /** |
32 | 32 | * @param string $compact_format |
33 | 33 | */ |
34 | - public function __construct($compact_format){ |
|
34 | + public function __construct($compact_format) { |
|
35 | 35 | $this->compact_format = $compact_format; |
36 | 36 | } |
37 | 37 |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | */ |
26 | 26 | class StringOrURI extends JsonValue { |
27 | 27 | |
28 | - public function getString(){ |
|
28 | + public function getString() { |
|
29 | 29 | return (string)$this->value; |
30 | 30 | } |
31 | 31 | |
@@ -33,11 +33,11 @@ discard block |
||
33 | 33 | * @throws \RuntimeException |
34 | 34 | * @return string |
35 | 35 | */ |
36 | - public function getUri(){ |
|
36 | + public function getUri() { |
|
37 | 37 | |
38 | - if($this->isString()) |
|
38 | + if ($this->isString()) |
|
39 | 39 | throw new \RuntimeException('current value is not an uri!'); |
40 | - if(filter_var($this->value, FILTER_VALIDATE_URL) === false) |
|
40 | + if (filter_var($this->value, FILTER_VALIDATE_URL) === false) |
|
41 | 41 | throw new \RuntimeException('current value is not an uri!'); |
42 | 42 | |
43 | 43 | return (string)$this->value; |
@@ -46,21 +46,21 @@ discard block |
||
46 | 46 | /** |
47 | 47 | * @return bool |
48 | 48 | */ |
49 | - public function isString(){ |
|
49 | + public function isString() { |
|
50 | 50 | return !$this->isUri(); |
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
54 | 54 | * @return bool |
55 | 55 | */ |
56 | - public function isUri(){ |
|
56 | + public function isUri() { |
|
57 | 57 | return filter_var($this->value, FILTER_VALIDATE_URL) !== false; |
58 | 58 | } |
59 | 59 | |
60 | 60 | /** |
61 | 61 | * @return string |
62 | 62 | */ |
63 | - public function getValue(){ |
|
63 | + public function getValue() { |
|
64 | 64 | return $this->isUri() ? $this->getUri() : $this->getString(); |
65 | 65 | } |
66 | 66 | } |
67 | 67 | \ No newline at end of file |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @param IJWTSnapshot $jwt_snapshot |
32 | 32 | * @return string |
33 | 33 | */ |
34 | - static public function serialize(IJWTSnapshot $jwt_snapshot){ |
|
34 | + static public function serialize(IJWTSnapshot $jwt_snapshot) { |
|
35 | 35 | list($header, $payload, $signature) = $jwt_snapshot->take(); |
36 | 36 | |
37 | 37 | $e_header = JOSEHeaderSerializer::serialize($header); |
@@ -46,18 +46,18 @@ discard block |
||
46 | 46 | * @return array |
47 | 47 | * @throws InvalidJWTException |
48 | 48 | */ |
49 | - static public function deserialize($input){ |
|
49 | + static public function deserialize($input) { |
|
50 | 50 | |
51 | 51 | $e_parts = explode(IBasicJWT::SegmentSeparator, $input); |
52 | 52 | |
53 | - if(count($e_parts) < 2) |
|
53 | + if (count($e_parts) < 2) |
|
54 | 54 | throw new InvalidJWTException(sprintf('%s has only 2 or less encoded parts!')); |
55 | 55 | $e_header = $e_parts[0]; |
56 | 56 | $e_payload = $e_parts[1]; |
57 | - $e_signature = count($e_parts)>2 ? $e_parts[2] : ''; |
|
57 | + $e_signature = count($e_parts) > 2 ? $e_parts[2] : ''; |
|
58 | 58 | $header = JOSEHeaderSerializer::deserialize($e_header); |
59 | 59 | $payload = ($header->getType()->getString() === 'JWT') ? JWTClaimSetSerializer::deserialize($e_payload) : JWTRawSerializer::deserialize($e_payload); |
60 | - $signature = !empty($e_signature) ? JWTRawSerializer::deserialize($e_signature): ''; |
|
60 | + $signature = !empty($e_signature) ? JWTRawSerializer::deserialize($e_signature) : ''; |
|
61 | 61 | return array($header, $payload, $signature); |
62 | 62 | } |
63 | 63 | } |
64 | 64 | \ No newline at end of file |