1
|
|
|
<?php namespace jwe\impl; |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 OpenStack Foundation |
4
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
* you may not use this file except in compliance with the License. |
6
|
|
|
* You may obtain a copy of the License at |
7
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
* Unless required by applicable law or agreed to in writing, software |
9
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
10
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11
|
|
|
* See the License for the specific language governing permissions and |
12
|
|
|
* limitations under the License. |
13
|
|
|
**/ |
14
|
|
|
use jwe\IJWE; |
15
|
|
|
use jwe\IJWE_CompactFormatSpecification; |
16
|
|
|
use jwe\IJWE_ParamsSpecification; |
17
|
|
|
use jwe\IJWE_Specification; |
18
|
|
|
use jwk\exceptions\InvalidJWKAlgorithm; |
19
|
|
|
use jwk\exceptions\InvalidJWKType; |
20
|
|
|
use jwk\JSONWebKeyPublicKeyUseValues; |
21
|
|
|
/** |
22
|
|
|
* Class JWEFactory |
23
|
|
|
* @package jwe\impl |
24
|
|
|
*/ |
25
|
|
|
final class JWEFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param IJWE_Specification $spec |
29
|
|
|
* @return IJWE |
30
|
|
|
* @throws InvalidJWKAlgorithm |
31
|
|
|
* @throws InvalidJWKType |
32
|
|
|
*/ |
33
|
|
|
static public function build(IJWE_Specification $spec) |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
if($spec instanceof IJWE_ParamsSpecification) |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
View Code Duplication |
if($spec->getRecipientKey()->getKeyUse()->getString() !== JSONWebKeyPublicKeyUseValues::Encryption) |
|
|
|
|
40
|
|
|
throw new InvalidJWKType |
41
|
|
|
( |
42
|
|
|
sprintf |
43
|
|
|
( |
44
|
|
|
'use %s not supported (should be "enc")', |
45
|
|
|
$spec->getRecipientKey()->getKeyUse()->getString() |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
View Code Duplication |
if($spec->getAlg()->getString() !== $spec->getRecipientKey()->getAlgorithm()->getString()) |
|
|
|
|
50
|
|
|
throw new InvalidJWKAlgorithm |
51
|
|
|
( |
52
|
|
|
sprintf |
53
|
|
|
( |
54
|
|
|
'mismatch between algorithm intended for use with the key %s and the cryptographic algorithm used to encrypt or determine the value of the CEK %s', |
55
|
|
|
$spec->getAlg()->getString(), |
56
|
|
|
$spec->getRecipientKey()->getAlgorithm()->getString() |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$header = new JWEJOSEHeader |
61
|
|
|
( |
62
|
|
|
$spec->getAlg(), |
63
|
|
|
$spec->getEnc(), |
64
|
|
|
$spec->getRecipientKey()->getId() |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
//set zip alg |
68
|
|
|
$zip = $spec->getZip(); |
69
|
|
|
|
70
|
|
|
if(!is_null($zip)) |
71
|
|
|
$header->setCompressionAlgorithm($zip); |
72
|
|
|
|
73
|
|
|
$jwe = JWE::fromHeaderAndPayload($header, $spec->getPayload()); |
74
|
|
|
|
75
|
|
|
$jwe->setRecipientKey($spec->getRecipientKey()); |
76
|
|
|
|
77
|
|
|
return $jwe; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if($spec instanceof IJWE_CompactFormatSpecification) |
81
|
|
|
{ |
82
|
|
|
return JWE::fromCompactSerialization($spec->getCompactFormat()); |
83
|
|
|
} |
84
|
|
|
throw new \RuntimeException('invalid JWE spec!'); |
85
|
|
|
} |
86
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.