Completed
Push — master ( 40ab14...05f952 )
by Florent
10:27 queued 08:01
created

X5UFinder::findJWKSet()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 30
rs 6.7273
cc 7
eloc 18
nc 7
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
use Jose\KeyConverter\KeyConverter;
15
16
/**
17
 */
18
class X5UFinder implements JWKSetFinderInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function findJWKSet(array $header)
24
    {
25
        if (!isset($header['x5u'])) {
26
            return;
27
        }
28
29
        $content = $this->downloadContent($header['x5u']);
30
        if (null === $content) {
31
            return;
32
        }
33
34
        $content = json_decode($content, true);
35
        if (!is_array($content)) {
36
            return;
37
        }
38
39
        $jwk_set = ['keys'];
40
        foreach ($content as $kid => $cert) {
41
            $jwk = KeyConverter::loadKeyFromCertificate($cert);
42
            if (null === $jwk) {
43
                break;
44
            }
45
            if (is_string($kid)) {
46
                $jwk['kid'] = $kid;
47
            }
48
            $jwk_set['keys'][] = $jwk;
49
        }
50
51
        return $jwk_set;
52
    }
53
54
    /**
55
     * @param string $url
56
     *
57
     * @return string|void
58
     */
59
    protected function downloadContent($url)
60
    {
61
        // The URL must be a valid URL and scheme must be https
62
        if (false === filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED) || 'https://' !== substr($url, 0, 8)) {
63
            return;
64
        }
65
66
        $ch = curl_init();
67
        curl_setopt_array($ch, [
68
            CURLOPT_RETURNTRANSFER => true,
69
            CURLOPT_SSL_VERIFYPEER => true,
70
            CURLOPT_SSL_VERIFYHOST => 2,
71
            CURLOPT_URL            => $url,
72
        ]);
73
        $content = curl_exec($ch);
74
        curl_close($ch);
75
76
        return $content;
77
    }
78
}
79