Completed
Push — master ( 7aad34...96e43e )
by Florent
02:35
created

JWS::addSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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