Failed Conditions
Push — master ( 6aefc6...e9b4ba )
by Florent
02:15
created

JWK::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Core;
15
16
use Base64Url\Base64Url;
17
18
/**
19
 * Class JWK.
20
 */
21
final class JWK implements \JsonSerializable
22
{
23
    /**
24
     * @var array
25
     */
26
    private $values = [];
27
28
    /**
29
     * JWK constructor.
30
     *
31
     * @param array $values
32
     */
33
    private function __construct(array $values)
34
    {
35
        $this->values = $values;
36
    }
37
38
    /**
39
     * @param array $values
40
     *
41
     * @return JWK
42
     */
43
    public static function create(array $values): self
44
    {
45
        if (!array_key_exists('kty', $values)) {
46
            throw new \InvalidArgumentException('The parameter "kty" is mandatory.');
47
        }
48
49
        return new self($values);
50
    }
51
52
    /**
53
     * @param string $json
54
     *
55
     * @return JWK
56
     */
57
    public static function createFromJson(string $json): self
58
    {
59
        $data = json_decode($json, true);
60
        if (!is_array($data)) {
61
            throw new \InvalidArgumentException('Invalid argument.');
62
        }
63
64
        return self::create($data);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function jsonSerialize()
71
    {
72
        return $this->values;
73
    }
74
75
    /**
76
     * Get the value with a specific key.
77
     *
78
     * @param string $key The key
79
     *
80
     * @throws \InvalidArgumentException
81
     *
82
     * @return mixed|null The value
83
     */
84
    public function get(string $key)
85
    {
86
        if (!$this->has($key)) {
87
            throw new \InvalidArgumentException(sprintf('The value identified by "%s" does not exist.', $key));
88
        }
89
90
        return $this->values[$key];
91
    }
92
93
    /**
94
     * Returns true if the JWK has the value identified by.
95
     *
96
     * @param string $key The key
97
     *
98
     * @return bool
99
     */
100
    public function has(string $key): bool
101
    {
102
        return array_key_exists($key, $this->values);
103
    }
104
105
    /**
106
     * Get all values stored in the JWK object.
107
     *
108
     * @return array Values of the JWK object
109
     */
110
    public function all(): array
111
    {
112
        return $this->values;
113
    }
114
115
    /**
116
     * Returns the thumbprint of the key.
117
     *
118
     * @see https://tools.ietf.org/html/rfc7638
119
     *
120
     * @param string $hash_algorithm
121
     *
122
     * @throws \InvalidArgumentException
123
     *
124
     * @return string
125
     */
126
    public function thumbprint(string $hash_algorithm): string
127
    {
128
        if (!in_array($hash_algorithm, hash_algos())) {
129
            throw new \InvalidArgumentException(sprintf('The hash algorithm "%s" is not supported.', $hash_algorithm));
130
        }
131
132
        $values = array_intersect_key($this->values, array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k']));
133
        ksort($values);
134
        $input = json_encode($values, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
135
136
        return Base64Url::encode(hash($hash_algorithm, $input, true));
137
    }
138
139
    /**
140
     * @return JWK
141
     */
142
    public function toPublic(): self
143
    {
144
        $values = array_diff_key($this->values, array_flip(['p', 'd', 'q', 'dp', 'dq', 'qi']));
145
146
        return new self($values);
147
    }
148
}
149