Completed
Push — master ( 517e4f...f67525 )
by Florent
02:24
created

JKUFinder::findJWKSet()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 10
nc 4
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 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\Finder;
13
14
/**
15
 */
16
class JKUFinder implements JWKSetFinderInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function findJWKSet(array $header)
22
    {
23
        if (!isset($header['jku'])) {
24
            return;
25
        }
26
27
        $content = $this->downloadContent($header['jku']);
28
        if (null === $content) {
29
            return;
30
        }
31
        $content = json_decode($content, true);
32
        if (!is_array($content) || !array_key_exists('keys', $content)) {
33
            return;
34
        }
35
36
        return $content;
37
    }
38
39
    /**
40
     * @param string $url
41
     *
42
     * @return string|void
43
     */
44
    protected function downloadContent($url)
45
    {
46
        // The URL must be a valid URL and scheme must be https
47
        if (false === filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED) || 'https://' !== substr($url, 0, 8)) {
48
            return;
49
        }
50
51
        $ch = curl_init();
52
        curl_setopt_array($ch, [
53
            CURLOPT_RETURNTRANSFER => true,
54
            CURLOPT_SSL_VERIFYPEER => true,
55
            CURLOPT_SSL_VERIFYHOST => 2,
56
            CURLOPT_URL            => $url,
57
        ]);
58
        $content = curl_exec($ch);
59
        curl_close($ch);
60
61
        return $content;
62
    }
63
}
64