|
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\Object; |
|
13
|
|
|
|
|
14
|
|
|
use Assert\Assertion; |
|
15
|
|
|
use Base64Url\Base64Url; |
|
16
|
|
|
use Jose\Factory\JWKFactory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class StorableJWK. |
|
20
|
|
|
*/ |
|
21
|
|
|
class StorableJWK implements StorableJWKInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Jose\Object\JWKInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $jwk; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $filename; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $parameters; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* RotatableJWK constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $filename |
|
42
|
|
|
* @param array $parameters |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($filename, array $parameters) |
|
45
|
|
|
{ |
|
46
|
|
|
Assertion::directory(dirname($filename), 'The selected directory does not exist.'); |
|
47
|
|
|
Assertion::writeable(dirname($filename), 'The selected directory is not writable.'); |
|
48
|
|
|
$this->filename = $filename; |
|
49
|
|
|
$this->parameters = $parameters; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getFilename() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->filename; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getAll() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->getJWK()->getAll(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function get($key) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->getJWK()->get($key); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function has($key) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->getJWK()->has($key); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public function thumbprint($hash_algorithm) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->getJWK()->thumbprint($hash_algorithm); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function toPublic() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->getJWK()->toPublic(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function jsonSerialize() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getJWK()->jsonSerialize(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return \Jose\Object\JWKInterface |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getJWK() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->loadJWK(); |
|
114
|
|
|
|
|
115
|
|
|
return $this->jwk; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function loadJWK() |
|
119
|
|
|
{ |
|
120
|
|
|
if (file_exists($this->filename)) { |
|
121
|
|
|
$content = file_get_contents($this->filename); |
|
122
|
|
|
if (false === $content) { |
|
123
|
|
|
$this->createJWK(); |
|
124
|
|
|
} |
|
125
|
|
|
$content = json_decode($content, true); |
|
126
|
|
|
if (!is_array($content)) { |
|
127
|
|
|
$this->createJWK(); |
|
128
|
|
|
} |
|
129
|
|
|
$this->jwk = new JWK($content); |
|
130
|
|
|
} else { |
|
131
|
|
|
$this->createJWK(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
protected function createJWK() |
|
137
|
|
|
{ |
|
138
|
|
|
$data = JWKFactory::createKey($this->parameters)->getAll(); |
|
139
|
|
|
$data['kid'] = Base64Url::encode(random_bytes(64)); |
|
140
|
|
|
$this->jwk = JWKFactory::createFromValues($data); |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
$this->save(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
protected function save() |
|
147
|
|
|
{ |
|
148
|
|
|
file_put_contents($this->getFilename(), json_encode($this->jwk)); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.