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
|
|
|
use Assert\Assertion; |
15
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class DownloadedJWKSet. |
19
|
|
|
*/ |
20
|
|
|
abstract class DownloadedJWKSet extends BaseJWKSet implements JWKSetInterface |
21
|
|
|
{ |
22
|
|
|
use JWKSetPEM; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $url; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var null|\Psr\Cache\CacheItemPoolInterface |
31
|
|
|
*/ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $ttl; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $allow_unsecured_connection; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* DownloadedJWKSet constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $url |
48
|
|
|
* @param \Psr\Cache\CacheItemPoolInterface|null $cache |
49
|
|
|
* @param int $ttl |
50
|
|
|
* @param bool $allow_unsecured_connection |
51
|
|
|
* @param bool $allow_http_connection |
52
|
|
|
*/ |
53
|
|
|
public function __construct($url, CacheItemPoolInterface $cache = null, $ttl = 86400, $allow_unsecured_connection = false, $allow_http_connection = false) |
54
|
|
|
{ |
55
|
|
|
Assertion::boolean($allow_unsecured_connection); |
56
|
|
|
Assertion::boolean($allow_http_connection); |
57
|
|
|
Assertion::integer($ttl); |
58
|
|
|
Assertion::min($ttl, 0); |
59
|
|
|
Assertion::false(false === filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED), 'Invalid URL.'); |
60
|
|
|
$allowed_protocols = ['https']; |
61
|
|
|
if (true === $allow_http_connection) { |
62
|
|
|
$allowed_protocols[] = 'http'; |
63
|
|
|
} |
64
|
|
|
Assertion::inArray(mb_substr($url, 0, mb_strpos($url, '://', 0, '8bit'), '8bit'), $allowed_protocols, sprintf('The provided sector identifier URI is not valid: scheme must be one of the following: %s.', json_encode($allowed_protocols))); |
65
|
|
|
|
66
|
|
|
$this->url = $url; |
67
|
|
|
$this->cache = $cache; |
68
|
|
|
$this->ttl = $ttl; |
69
|
|
|
$this->allow_unsecured_connection = $allow_unsecured_connection; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function addKey(JWKInterface $key) |
76
|
|
|
{ |
77
|
|
|
//Not available |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function prependKey(JWKInterface $key) |
84
|
|
|
{ |
85
|
|
|
//Not available |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function removeKey($index) |
92
|
|
|
{ |
93
|
|
|
//Not available |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function getContent() |
100
|
|
|
{ |
101
|
|
|
$cache_key = sprintf('JWKFactory-Content-%s', hash('sha512', $this->url)); |
102
|
|
|
if (null !== $this->cache) { |
103
|
|
|
$item = $this->cache->getItem($cache_key); |
104
|
|
|
if (!$item->isHit()) { |
105
|
|
|
$content = $this->downloadContent(); |
106
|
|
|
$item->set($content); |
107
|
|
|
if (0 !== $this->ttl) { |
108
|
|
|
$item->expiresAfter($this->ttl); |
109
|
|
|
} |
110
|
|
|
$this->cache->save($item); |
111
|
|
|
|
112
|
|
|
return $content; |
113
|
|
|
} else { |
114
|
|
|
return $item->get(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->downloadContent(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @throws \InvalidArgumentException |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
private function downloadContent() |
127
|
|
|
{ |
128
|
|
|
$params = [ |
129
|
|
|
CURLOPT_RETURNTRANSFER => true, |
130
|
|
|
CURLOPT_URL => $this->url, |
131
|
|
|
]; |
132
|
|
|
if (false === $this->allow_unsecured_connection) { |
133
|
|
|
$params[CURLOPT_SSL_VERIFYPEER] = true; |
134
|
|
|
$params[CURLOPT_SSL_VERIFYHOST] = 2; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$ch = curl_init(); |
138
|
|
|
curl_setopt_array($ch, $params); |
139
|
|
|
$content = curl_exec($ch); |
140
|
|
|
curl_close($ch); |
141
|
|
|
|
142
|
|
|
Assertion::false(false === $content, 'Unable to get content.'); |
143
|
|
|
|
144
|
|
|
return $content; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|