1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2017 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
|
|
|
/** |
15
|
|
|
* Class RotatableJWKSet. |
16
|
|
|
*/ |
17
|
|
|
final class RotatableJWKSet extends StorableJWKSet implements RotatableInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Interval at which keys should be rotated. |
21
|
|
|
* |
22
|
|
|
* @var int|null |
23
|
|
|
*/ |
24
|
|
|
private $interval; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* RotatableJWKSet constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $filename |
30
|
|
|
* @param array $parameters |
31
|
|
|
* @param int $nb_keys |
32
|
|
|
* @param int|null $interval |
33
|
|
|
*/ |
34
|
|
|
public function __construct($filename, array $parameters, $nb_keys, $interval = null) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($filename, $parameters, $nb_keys); |
37
|
|
|
|
38
|
|
|
$this->interval = $interval; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function getJWKSet() |
45
|
|
|
{ |
46
|
|
|
// Check if we need to rotate keys upon every interaction with the underlying JWK set |
47
|
|
|
$this->rotateIfNeeded(); |
48
|
|
|
return parent::getJWKSet(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function rotate() |
55
|
|
|
{ |
56
|
|
|
$jwkset = parent::getJWKSet(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
// Remove last key in set |
59
|
|
|
$jwkset->removeKey($jwkset->countKeys() - 1); |
60
|
|
|
|
61
|
|
|
// Prepend new key to set |
62
|
|
|
$jwkset->prependKey($this->createJWK()); |
|
|
|
|
63
|
|
|
|
64
|
|
|
// Save new key set |
65
|
|
|
$this->saveObject($jwkset); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Rotate key set if last modification time is due. |
70
|
|
|
*/ |
71
|
|
|
private function rotateIfNeeded() |
72
|
|
|
{ |
73
|
|
|
if (isset($this->interval) && $this->interval >= 0) { |
74
|
|
|
$modificationTime = $this->getLastModificationTime(); |
75
|
|
|
|
76
|
|
|
if (null === $modificationTime) { |
77
|
|
|
$this->regen(); |
78
|
|
|
} elseif (($modificationTime + $this->interval) < time()) { |
79
|
|
|
$this->rotate(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.