Completed
Push — master ( d8c018...19ac78 )
by Florent
06:38
created

JWK   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 76
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getAll() 0 4 1
A thumbprint() 0 10 1
A jsonSerialize() 0 4 1
A get() 0 7 2
A has() 0 4 1
A toPublic() 0 7 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 Base64Url\Base64Url;
16
17
/**
18
 * Class JWK.
19
 */
20
final class JWK implements JWKInterface
21
{
22
    /**
23
     * @var array
24
     */
25
    private $values = [];
26
27
    /**
28
     * JWK constructor.
29
     *
30
     * @param array $values
31
     */
32
    public function __construct(array $values = [])
33
    {
34
        Assertion::keyExists($values, 'kty', 'The parameter "kty" is mandatory.');
35
36
        $this->values = $values;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function jsonSerialize()
43
    {
44
        return $this->getAll();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function get($key)
51
    {
52
        if ($this->has($key)) {
53
            return $this->values[$key];
54
        }
55
        throw new \InvalidArgumentException(sprintf('The value identified by "%s" does not exist.', $key));
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function has($key)
62
    {
63
        return array_key_exists($key, $this->getAll());
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getAll()
70
    {
71
        return $this->values;
72
    }
73
74
    public function thumbprint($hash_algorithm)
75
    {
76
        Assertion::inArray($hash_algorithm, hash_algos(), sprintf('Hash algorithm "%s" is not supported', $hash_algorithm));
77
78
        $values = array_intersect_key($this->getAll(), array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k']));
79
        ksort($values);
80
        $input = json_encode($values);
81
82
        return Base64Url::encode(hash($hash_algorithm, $input, true));
83
    }
84
85
    /**
86
     * @return \Jose\Object\JWKInterface
87
     */
88
    public function toPublic()
89
    {
90
        $values = $this->getAll();
91
        $values = array_diff_key($values, array_flip(['p', 'd', 'q', 'dp', 'dq', 'qi']));
92
93
        return new self($values);
94
    }
95
}
96