|
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 StorableJWKSet. |
|
20
|
|
|
*/ |
|
21
|
|
|
final class StorableJWKSet implements StorableJWKSetInterface |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Jose\Object\JWKSetInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $jwkset; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $filename; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $parameters; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $nb_keys; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* StorableJWKSet constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $filename |
|
47
|
|
|
* @param array $parameters |
|
48
|
|
|
* @param int $nb_keys |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($filename, array $parameters, $nb_keys) |
|
51
|
|
|
{ |
|
52
|
|
|
Assertion::directory(dirname($filename), 'The selected directory does not exist.'); |
|
53
|
|
|
Assertion::writeable(dirname($filename), 'The selected directory is not writable.'); |
|
54
|
|
|
Assertion::integer($nb_keys, 'The key set must contain at least one key.'); |
|
55
|
|
|
Assertion::greaterThan($nb_keys, 0, 'The key set must contain at least one key.'); |
|
56
|
|
|
$this->filename = $filename; |
|
57
|
|
|
$this->parameters = $parameters; |
|
58
|
|
|
$this->nb_keys = $nb_keys; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getFilename() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->filename; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function jsonSerialize() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->getJWKSet()->jsonSerialize(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return \Jose\Object\JWKSetInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
private function getJWKSet() |
|
81
|
|
|
{ |
|
82
|
|
|
if (null === $this->jwkset) { |
|
83
|
|
|
$this->loadJWKSet(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->jwkset; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function loadJWKSet() |
|
90
|
|
|
{ |
|
91
|
|
|
if (file_exists($this->filename)) { |
|
92
|
|
|
$content = file_get_contents($this->filename); |
|
93
|
|
|
if (false === $content) { |
|
94
|
|
|
$this->createJWKSet(); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
$content = json_decode($content, true); |
|
97
|
|
|
if (!is_array($content)) { |
|
98
|
|
|
$this->createJWKSet(); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
$this->jwkset = new JWKSet($content); |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->createJWKSet(); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function createJWKSet() |
|
107
|
|
|
{ |
|
108
|
|
|
/*$data = JWKFactory::createKey($this->parameters)->getAll(); |
|
109
|
|
|
$this->jwkset = JWKFactory::createFromValues($data); |
|
110
|
|
|
|
|
111
|
|
|
file_put_contents( |
|
112
|
|
|
$this->filename, |
|
113
|
|
|
json_encode($this->jwkset) |
|
114
|
|
|
);*/ |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|