Completed
Push — v2.0.x ( 45cd5b...255948 )
by Florent
12:36
created

JWS::getSignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 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 Base64Url\Base64Url;
15
16
/**
17
 * Class JWS.
18
 */
19
final class JWS implements JWSInterface
20
{
21
    use JWT;
22
23
    /**
24
     * @var \Jose\Object\SignatureInterface[]
25
     */
26
    private $signatures = [];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getEncodedPayload()
32
    {
33
        return is_string($this->getPayload()) ? Base64Url::encode($this->getPayload()) : null;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getSignatures()
40
    {
41
        return $this->signatures;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getSignature($id)
48
    {
49
        if (isset($this->signatures[$id])) {
50
51
            return $this->signatures[$id];
52
        }
53
        throw new \InvalidArgumentException('The signature does not exist.');
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function addSignature(SignatureInterface $signature)
60
    {
61
        $jws = clone $this;
62
        $jws->signatures[] = $signature;
63
64
        return $jws;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function countSignatures()
71
    {
72
        return count($this->signatures);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function toCompactJSON($id)
79
    {
80
        $signature = $this->getSignature($id);
81
82
        if (!empty($signature->getHeaders())) {
83
            throw new \InvalidArgumentException('The signature contains unprotected headers and cannot be converted into compact JSON');
84
        }
85
86
        return sprintf(
87
            '%s.%s.%s',
88
            $signature->getEncodedProtectedHeaders(),
89
            $this->getEncodedPayload(),
90
            Base64Url::encode($signature->getSignature())
91
        );
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function toFlattenedJSON($id)
98
    {
99
        $signature = $this->getSignature($id);
100
101
        $data = [];
102
        $values = [
103
            'payload' => $this->getEncodedPayload(),
104
            'protected' => $signature->getEncodedProtectedHeaders(),
105
            'header' => $signature->getHeaders(),
106
        ];
107
108
        foreach ($values as $key => $value) {
109
            if (!empty($value)) {
110
                $data[$key] = $value;
111
            }
112
        }
113
        $data['signature'] = Base64Url::encode($signature->getSignature());
114
115
        return json_encode($data);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function toJSON()
122
    {
123
        if (0 === $this->countSignatures()) {
124
            throw new \BadMethodCallException('No signature.');
125
        }
126
127
        $data = [];
128
        if (!empty($this->getEncodedPayload())) {
129
            $data['payload'] = $this->getEncodedPayload();
130
        }
131
132
        $data['signatures'] = [];
133
        foreach ($this->getSignatures() as $signature) {
134
135
            $tmp = ['signature' => Base64Url::encode($signature->getSignature())];
136
            $values = [
137
                'protected' => $signature->getEncodedProtectedHeaders(),
138
                'header'    => $signature->getHeaders(),
139
            ];
140
141
            foreach ($values as $key => $value) {
142
                if (!empty($value)) {
143
                    $tmp[$key] = $value;
144
                }
145
            }
146
            $data['signatures'][] = $tmp;
147
        }
148
149
        return json_encode($data);
150
    }
151
}
152