|
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
|
|
|
use Assert\Assertion; |
|
14
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class DownloadedJWKSet. |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class DownloadedJWKSet implements JWKSetInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use BaseJWKSet; |
|
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 removeKey($index) |
|
84
|
|
|
{ |
|
85
|
|
|
//Not available |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getContent() |
|
92
|
|
|
{ |
|
93
|
|
|
$cache_key = sprintf('JWKFactory-Content-%s', hash('sha512', $this->url)); |
|
94
|
|
|
if (null !== $this->cache) { |
|
95
|
|
|
$item = $this->cache->getItem($cache_key); |
|
96
|
|
|
if (!$item->isHit()) { |
|
97
|
|
|
$content = $this->downloadContent(); |
|
98
|
|
|
$item->set($content); |
|
99
|
|
|
if (0 !== $this->ttl) { |
|
100
|
|
|
$item->expiresAfter($this->ttl); |
|
101
|
|
|
} |
|
102
|
|
|
$this->cache->save($item); |
|
103
|
|
|
|
|
104
|
|
|
return $content; |
|
105
|
|
|
} else { |
|
106
|
|
|
return $item->get(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->downloadContent(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @throws \InvalidArgumentException |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
private function downloadContent() |
|
119
|
|
|
{ |
|
120
|
|
|
$params = [ |
|
121
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
122
|
|
|
CURLOPT_URL => $this->url, |
|
123
|
|
|
]; |
|
124
|
|
|
if (false === $this->allow_unsecured_connection) { |
|
125
|
|
|
$params[CURLOPT_SSL_VERIFYPEER] = true; |
|
126
|
|
|
$params[CURLOPT_SSL_VERIFYHOST] = 2; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$ch = curl_init(); |
|
130
|
|
|
curl_setopt_array($ch, $params); |
|
131
|
|
|
$content = curl_exec($ch); |
|
132
|
|
|
curl_close($ch); |
|
133
|
|
|
|
|
134
|
|
|
Assertion::false(false === $content, 'Unable to get content.'); |
|
135
|
|
|
|
|
136
|
|
|
return $content; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|