Completed
Push — master ( 8bb38a...d8bfd2 )
by Florent
02:36
created

RotatableJWK::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 Jose\Factory\JWKFactory;
16
17
/**
18
 * Class RotatableJWK.
19
 */
20
final class RotatableJWK implements JWKInterface
21
{
22
    /**
23
     * @var \Jose\Object\JWKInterface
24
     */
25
    private $jwk;
26
27
    /**
28
     * @var string
29
     */
30
    private $filename;
31
32
    /**
33
     * @var int
34
     */
35
    private $ttl;
36
37
    /**
38
     * @var int|null
39
     */
40
    private $expires_at = 0;
41
42
    /**
43
     * @var array
44
     */
45
    private $parameters;
46
47
    /**
48
     * RotatableJWK constructor.
49
     *
50
     * @param string $filename
51
     * @param array  $parameters
52
     * @param int    $ttl
53
     */
54
    public function __construct($filename, array $parameters, $ttl = 0)
55
    {
56
        Assertion::directory(dirname($filename), 'The selected directory does not exist.');
57
        Assertion::writeable(dirname($filename), 'The selected directory is not writable.');
58
        Assertion::integer($ttl, 'The parameter must an integer');
59
        Assertion::greaterOrEqualThan($ttl, 0, 'The parameter must be at least 0');
60
        $this->filename = $filename;
61
        $this->parameters = $parameters;
62
        $this->ttl = $ttl;
63
    }
64
65
    /**
66
     * @return \Jose\Object\JWKInterface
67
     */
68
    private function getJWK()
69
    {
70
        if (null === $this->jwk) {
71
            $this->loadJWK();
72
        }
73
        if (0 !== $this->expires_at && $this->expires_at < time()) {
74
            $this->createJWK();
75
        }
76
77
        return $this->jwk;
78
    }
79
80
    private function loadJWK()
81
    {
82
        if (file_exists($this->filename)) {
83
            $content = file_get_contents($this->filename);
84
            if (false === $content) {
85
                $this->createJWK();
86
            }
87
            $content = json_decode($content, true);
88
            if (!is_array($content) || !array_key_exists('expires_at', $content) || !array_key_exists('jwk', $content)) {
89
                $this->createJWK();
90
            }
91
            if (0 !== $content['expires_at'] && $content['expires_at'] < time()) {
92
                $this->createJWK();
93
            }
94
            $this->jwk = new JWK($content['jwk']);
95
        } else {
96
            $this->createJWK();
97
        }
98
    }
99
100
    private function createJWK()
101
    {
102
        $this->jwk = JWKFactory::createKey($this->parameters);
103
        if (0 !== $this->ttl) {
104
            $this->expires_at = time() + $this->ttl;
105
        }
106
        file_put_contents(
107
            $this->filename,
108
            json_encode([
109
                'expires_at' => $this->expires_at,
110
                'jwk' => $this->jwk,
111
            ])
112
        );
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getAll()
119
    {
120
        return $this->getJWK()->getAll();
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function get($key)
127
    {
128
        return $this->getJWK()->get($key);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function has($key)
135
    {
136
        return $this->getJWK()->has($key);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function thumbprint($hash_algorithm)
143
    {
144
        return $this->getJWK()->thumbprint($hash_algorithm);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function toPublic()
151
    {
152
        return $this->getJWK()->toPublic();
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function jsonSerialize()
159
    {
160
        return $this->getJWK()->jsonSerialize();
161
    }
162
}
163