Completed
Push — master ( 26ae43...5f93f7 )
by Florent
10:44 queued 10:44
created

X5UFinder::convertX5CToJWK()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 17
rs 8.8571
c 1
b 1
f 0
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
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
            $jwk_set['keys'][] = $jwk;
46
        }
47
48
        return $jwk_set;
49
    }
50
51
    /**
52
     * @param string $url
53
     *
54
     * @return string|void
55
     */
56
    protected function downloadContent($url)
57
    {
58
        // The URL must be a valid URL and scheme must be https
59
        if (false === filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED) || 'https://' !== substr($url, 0, 8)) {
60
            return;
61
        }
62
63
        $ch = curl_init();
64
        curl_setopt_array($ch, [
65
            CURLOPT_RETURNTRANSFER => true,
66
            CURLOPT_SSL_VERIFYPEER => true,
67
            CURLOPT_SSL_VERIFYHOST => 2,
68
            CURLOPT_URL            => $url,
69
        ]);
70
        $content = curl_exec($ch);
71
        curl_close($ch);
72
73
        return $content;
74
    }
75
}
76