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
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class RotatableJWKSet. |
18
|
|
|
*/ |
19
|
|
|
final class RotatableJWKSet extends StorableJWKSet implements RotatableJWKSetInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $ttl; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* RotatableJWKSet constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $filename |
30
|
|
|
* @param array $parameters |
31
|
|
|
* @param int $nb_keys |
32
|
|
|
* @param int $ttl |
33
|
|
|
*/ |
34
|
|
|
public function __construct($filename, array $parameters, $nb_keys, $ttl) |
35
|
|
|
{ |
36
|
|
|
Assertion::integer($ttl); |
37
|
|
|
Assertion::greaterThan($ttl, 0, 'The parameter TTL must be at least 0.'); |
38
|
|
|
$this->ttl = $ttl; |
39
|
|
|
parent::__construct($filename, $parameters, $nb_keys); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return \Jose\Object\JWKSetInterface |
44
|
|
|
*/ |
45
|
|
|
protected function getJWKSet() |
46
|
|
|
{ |
47
|
|
|
$jwkset = parent::getJWKSet(); |
48
|
|
|
if (null !== $this->getFileLastModificationTime()) { |
49
|
|
|
if ($this->getFileLastModificationTime() + $this->ttl < time()) { |
50
|
|
|
$keys = $jwkset->getKeys(); |
51
|
|
|
unset($keys[count($keys) - 1]); |
52
|
|
|
$jwkset = new JWKSet(); |
53
|
|
|
$jwkset->addKey($this->createJWK()); |
|
|
|
|
54
|
|
|
foreach ($keys as $key) { |
55
|
|
|
$jwkset->addKey($key); |
56
|
|
|
} |
57
|
|
|
$this->jwkset = $jwkset; |
58
|
|
|
$this->save(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return parent::getJWKSet(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int|null |
67
|
|
|
*/ |
68
|
|
|
private function getFileLastModificationTime() |
69
|
|
|
{ |
70
|
|
|
if (file_exists($this->getFilename())) { |
71
|
|
|
return filemtime($this->getFilename()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.