Completed
Push — develop ( 686594...b5844e )
by Florent
03:11
created

JWSTest::testToJSONFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
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
use Base64Url\Base64Url;
13
use Jose\Factory\CheckerManagerFactory;
14
use Jose\Factory\JWSFactory;
15
use Jose\Object\JWK;
16
use Jose\Object\Signature;
17
18
/**
19
 * Class JWSTest.
20
 *
21
 * @group JWS
22
 * @group Unit
23
 */
24
class JWSTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @expectedException \InvalidArgumentException
28
     * @expectedExceptionMessage  One or more claims are marked as critical, but they are missing or have not been checked (["iss"])
29
     */
30
    public function testJWS()
31
    {
32
        $claims = [
33
            'nbf' => time(),
34
            'iat' => time(),
35
            'exp' => time() + 3600,
36
            'iss' => 'Me',
37
            'aud' => 'You',
38
            'sub' => 'My friend',
39
        ];
40
        $jws = JWSFactory::createJWS($claims);
41
42
        $this->assertTrue($jws->hasClaims());
43
        $this->assertTrue($jws->hasClaim('nbf'));
44
        $this->assertTrue($jws->hasClaim('iss'));
45
        $this->assertEquals('Me', $jws->getClaim('iss'));
46
        $this->assertEquals($claims, json_decode(Base64Url::decode($jws->getEncodedPayload()), true));
47
        $this->assertEquals($claims, $jws->getPayload());
48
        $this->assertEquals($claims, $jws->getClaims());
49
        $this->assertEquals(0, $jws->countSignatures());
50
51
        $jws = $jws->addSignature(new JWK(['kty' => 'none']), ['crit' => ['nbf', 'iat', 'exp', 'iss']]);
52
        $this->assertEquals(1, $jws->countSignatures());
53
54
        $checker_manager = CheckerManagerFactory::createClaimCheckerManager();
55
        $checker_manager->checkJWS($jws, 0);
56
    }
57
58
    /**
59
     * @expectedException \InvalidArgumentException
60
     * @expectedExceptionMessage The signature does not exist.
61
     */
62
    public function testToCompactJSONFailed()
63
    {
64
        $jws = JWSFactory::createJWS([
65
            'nbf' => time(),
66
            'iat' => time(),
67
            'exp' => time() + 3600,
68
            'iss' => 'Me',
69
            'aud' => 'You',
70
            'sub' => 'My friend',
71
        ]);
72
73
        $jws->toCompactJSON(0);
74
    }
75
76
    /**
77
     * @expectedException \InvalidArgumentException
78
     * @expectedExceptionMessage The signature does not exist.
79
     */
80
    public function testToFlattenedJSONFailed()
81
    {
82
        $jws = JWSFactory::createJWS([
83
            'nbf' => time(),
84
            'iat' => time(),
85
            'exp' => time() + 3600,
86
            'iss' => 'Me',
87
            'aud' => 'You',
88
            'sub' => 'My friend',
89
        ]);
90
91
        $jws->toFlattenedJSON(0);
92
    }
93
94
    /**
95
     * @expectedException \InvalidArgumentException
96
     * @expectedExceptionMessage No signature.
97
     */
98
    public function testToJSONFailed()
99
    {
100
        $jws = JWSFactory::createJWS([
101
            'nbf' => time(),
102
            'iat' => time(),
103
            'exp' => time() + 3600,
104
            'iss' => 'Me',
105
            'aud' => 'You',
106
            'sub' => 'My friend',
107
        ]);
108
109
        $jws->toJSON();
110
    }
111
112
    /**
113
     * @expectedException \InvalidArgumentException
114
     * @expectedExceptionMessage The payload does not contain claims.
115
     */
116
    public function testNoClaims()
117
    {
118
        $jws = JWSFactory::createJWS('Hello');
119
120
        $jws->getClaims();
121
    }
122
123
    /**
124
     * @expectedException \InvalidArgumentException
125
     * @expectedExceptionMessage The payload does not contain claim "foo".
126
     */
127
    public function testClaimDoesNotExist()
128
    {
129
        $jws = JWSFactory::createJWS([
130
            'nbf' => time(),
131
            'iat' => time(),
132
            'exp' => time() + 3600,
133
            'iss' => 'Me',
134
            'aud' => 'You',
135
            'sub' => 'My friend',
136
        ]);
137
138
        $jws->getClaim('foo');
139
    }
140
141
    /**
142
     * @expectedException \InvalidArgumentException
143
     * @expectedExceptionMessage The signature contains unprotected headers and cannot be converted into compact JSON
144
     */
145
    public function testSignatureContainsUnprotectedHeaders()
146
    {
147
        $jws = JWSFactory::createJWS('Hello');
148
149
        $jws = $jws->addSignature(new JWK(['kty' => 'none']), [], ['foo' => 'bar']);
150
151
        $jws->toCompactJSON(0);
152
    }
153
154
    /**
155
     * @expectedException \InvalidArgumentException
156
     * @expectedExceptionMessage The header "foo" does not exist
157
     */
158
    public function testSignatureDoesNotContainHeader()
159
    {
160
        $signature = new Signature();
161
162
        $signature->getHeader('foo');
163
    }
164
165
    /**
166
     * @expectedException \InvalidArgumentException
167
     * @expectedExceptionMessage The protected header "foo" does not exist
168
     */
169
    public function testSignatureDoesNotContainProtectedHeader()
170
    {
171
        $signature = new Signature();
172
173
        $signature->getProtectedHeader('foo');
174
    }
175
176
    /*
177
     * @expectedException \InvalidArgumentException
178
     * @expectedExceptionMessage The argument does not contain valid encoded protected headers.
179
     */
180
    /*public function testBadEncodedProtectedHeader()
181
    {
182
        $signature = new Signature();
183
184
        $signature->withEncodedProtectedHeaders('foo');
185
    }*/
186
187
    /*public function testEmptyEncodedProtectedHeader()
188
    {
189
        $signature = new Signature();
190
191
        $signature->withEncodedProtectedHeaders('');
192
193
        $this->assertEquals([], $signature->getProtectedHeaders());
194
    }*/
195
196
    /*public function testEncodedProtectedHeader()
197
    {
198
        $signature = new Signature();
199
200
        $signature = $signature->withEncodedProtectedHeaders(Base64Url::encode(json_encode(['foo' => 'bar'])));
201
        $signature = $signature->withProtectedHeader('plic', 'ploc');
202
203
        $this->assertEquals(['foo' => 'bar', 'plic' => 'ploc'], $signature->getProtectedHeaders());
204
    }*/
205
}
206