Failed Conditions
Pull Request — master (#144)
by Florent
08:48 queued 04:12
created

DownloadedJWKSet::removeKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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