Failed Conditions
Push — v7 ( 9bed55...5d1eb6 )
by Florent
02:58
created

Signature::getSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 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\Signature;
15
16
use Assert\Assertion;
17
use Base64Url\Base64Url;
18
use Jose\Component\Core\JWK;
19
20
final class Signature
21
{
22
    /**
23
     * @var null|string
24
     */
25
    private $encodedProtectedHeaders = null;
26
27
    /**
28
     * @var array
29
     */
30
    private $protectedHeaders = [];
31
32
    /**
33
     * @var array
34
     */
35
    private $headers = [];
36
37
    /**
38
     * @var string
39
     */
40
    private $signature;
41
42
    /**
43
     * @var JWK
44
     */
45
    private $signature_key;
46
47
    /**
48
     * Signature constructor.
49
     *
50
     * @param string $signature
51
     * @param null|string $encodedProtectedHeaders
52
     * @param array $headers
53
     */
54
    private function __construct(string $signature, ?string $encodedProtectedHeaders, array $headers)
55
    {
56
        if (null !== $encodedProtectedHeaders) {
57
            $protected_headers = json_decode(Base64Url::decode($encodedProtectedHeaders), true);
58
            Assertion::isArray($protected_headers, 'Unable to decode the protected headers.');
59
            $this->protectedHeaders = $protected_headers;
60
        }
61
        $this->encodedProtectedHeaders = $encodedProtectedHeaders;
62
        $this->signature = $signature;
63
        $this->headers = $headers;
64
    }
65
66
    /**
67
     * @param string      $signature
68
     * @param string|null $encodedProtectedHeaders
69
     * @param array       $headers
70
     *
71
     * @return Signature
72
     */
73
    public static function create(string $signature, ?string $encodedProtectedHeaders, array $headers = []): Signature
74
    {
75
        return new self($signature, $encodedProtectedHeaders, $headers);
76
    }
77
78
    /**
79
     * The protected header associated with the signature.
80
     *
81
     * @return array
82
     */
83
    public function getProtectedHeaders()
84
    {
85
        return $this->protectedHeaders;
86
    }
87
88
    /**
89
     * The unprotected header associated with the signature.
90
     *
91
     * @return array
92
     */
93
    public function getHeaders()
94
    {
95
        return $this->headers;
96
    }
97
98
    /**
99
     * The protected header associated with the signature.
100
     *
101
     *
102
     * @return null|string
103
     */
104
    public function getEncodedProtectedHeaders()
105
    {
106
        return $this->encodedProtectedHeaders;
107
    }
108
109
    /**
110
     * Returns the value of the protected header of the specified key.
111
     *
112
     * @param string $key The key
113
     *
114
     * @return mixed|null Header value
115
     */
116
    public function getProtectedHeader($key)
117
    {
118
        if ($this->hasProtectedHeader($key)) {
119
            return $this->getProtectedHeaders()[$key];
120
        }
121
        throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key));
122
    }
123
124
    /**
125
     * @param string $key The key
126
     *
127
     * @return bool
128
     */
129
    public function hasProtectedHeader($key)
130
    {
131
        return array_key_exists($key, $this->getProtectedHeaders());
132
    }
133
134
    /**
135
     * Returns the value of the unprotected header of the specified key.
136
     *
137
     * @param string $key The key
138
     *
139
     * @return mixed|null Header value
140
     */
141
    public function getHeader($key)
142
    {
143
        if ($this->hasHeader($key)) {
144
            return $this->headers[$key];
145
        }
146
        throw new \InvalidArgumentException(sprintf('The header "%s" does not exist', $key));
147
    }
148
149
    /**
150
     * @param string $key The key
151
     *
152
     * @return bool
153
     */
154
    public function hasHeader($key)
155
    {
156
        return array_key_exists($key, $this->headers);
157
    }
158
159
    /**
160
     * The protected and unprotected header associated with the signature.
161
     *
162
     * @return array
163
     */
164
    public function getAllHeaders()
165
    {
166
        return array_merge(
167
            $this->getProtectedHeaders(),
168
            $this->getHeaders()
169
        );
170
    }
171
172
    /**
173
     * Returns the value of the signature.
174
     *
175
     * @return string
176
     */
177
    public function getSignature()
178
    {
179
        return $this->signature;
180
    }
181
182
    /**
183
     * @return JWK
184
     */
185
    public function getSignatureKey()
186
    {
187
        return $this->signature_key;
188
    }
189
}
190