Completed
Push — master ( 4c45e0...1a7b7a )
by Дмитрий
02:22
created

JWTTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 92
dl 0
loc 210
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getJWKSet() 0 8 1
A testValidateClaimsNbfScrew() 0 22 1
A testValidateHeaderSuccess() 0 14 1
A testValidateClaimsNbfFail() 0 20 1
A testValidateHeaderNoKid() 0 15 1
A callProtectedMethod() 0 8 1
A encodeJWT() 0 11 1
A getTestHeader() 0 5 1
A testDecodeWrongNumberOfSegments() 0 8 1
A testValidateClaimsSuccess() 0 18 1
A testValidateClaimsExpFail() 0 22 1
A testValidateHeaderNoAlg() 0 15 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace Test\OpenIDConnect;
8
9
use DateTime;
10
use ReflectionClass;
11
use SocialConnect\OpenIDConnect\Exception\InvalidJWT;
12
use SocialConnect\OpenIDConnect\JWT;
13
14
class JWTTest extends \Test\TestCase
15
{
16
    /**
17
     * @return array
18
     */
19
    protected function getJWKSet()
20
    {
21
        return [
22
            [
23
                'kid' => 'testSigKey',
24
                'kty' => 'RS256',
25
                'n' => 'TEST',
26
                'e' => 'TEST'
27
            ]
28
        ];
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    protected function getTestHeader()
35
    {
36
        return [
37
            'alg' => 'RS256',
38
            'kid' => 'testSigKey'
39
        ];
40
    }
41
42
    /**
43
     * @param object $object
44
     * @param string $name
45
     * @param array $params
46
     * @return mixed
47
     * @throws \ReflectionException
48
     */
49
    protected static function callProtectedMethod($object, $name, array $params = [])
50
    {
51
        $class = new ReflectionClass($object);
52
53
        $method = $class->getMethod($name);
54
        $method->setAccessible(true);
55
56
        return $method->invokeArgs($object, $params);
57
    }
58
59
    protected function encodeJWT($payload)
60
    {
61
        $header = $this->getTestHeader();
62
63
        $encodedHeader = json_encode($header);
64
        $b64Header = base64_encode($encodedHeader);
65
66
        $encodedPayload = json_encode($payload);
67
        $b64Payload = base64_encode($encodedPayload);
68
69
        return $b64Header . '.' . $b64Payload . '.' . 'signatureLOL';
70
    }
71
72
    public function testValidateClaimsSuccess()
73
    {
74
        $token = new JWT(
75
            array(
76
                'nbf' => time(),
77
                'iat' => time(),
78
                'exp' => time() + 20,
79
            ),
80
            $this->getTestHeader()
81
        );
82
83
        self::callProtectedMethod(
84
            $token,
85
            'validateClaims'
86
        );
87
88
        // to skip warning
89
        parent::assertTrue(true);
90
    }
91
92
    public function testValidateClaimsNbfFail()
93
    {
94
        $token = new JWT(
95
            array(
96
                'nbf' => $nbf = time() + 10,
97
                'iat' => time(),
98
                'exp' => time() + 20,
99
            ),
100
            $this->getTestHeader()
101
        );
102
103
        parent::expectException(InvalidJWT::class);
104
        parent::expectExceptionMessage(sprintf(
105
            'nbf (Not Fefore) claim is not valid %s',
106
            date(DateTime::RFC3339, $nbf)
107
        ));
108
109
        self::callProtectedMethod(
110
            $token,
111
            'validateClaims'
112
        );
113
    }
114
115
    public function testValidateClaimsNbfScrew()
116
    {
117
        JWT::$screw = 30;
118
119
        $token = new JWT(
120
            array(
121
                'nbf' => $nbf = time() + 10,
0 ignored issues
show
Unused Code introduced by
The assignment to $nbf is dead and can be removed.
Loading history...
122
                'iat' => time(),
123
                'exp' => time() + 20,
124
            ),
125
            $this->getTestHeader()
126
        );
127
128
        self::callProtectedMethod(
129
            $token,
130
            'validateClaims'
131
        );
132
133
        JWT::$screw = 0;
134
135
        // to skip warning
136
        parent::assertTrue(true);
137
    }
138
139
    public function testValidateClaimsExpFail()
140
    {
141
        $token = new JWT(
142
            array(
143
                'nbf' => time(),
144
                'iat' => time(),
145
                'exp' => $exp = time() - 20,
146
            ),
147
            $this->getTestHeader()
148
        );
149
150
        parent::expectException(InvalidJWT::class);
151
        parent::expectExceptionMessage(
152
            sprintf(
153
                'exp (Expiration Time) claim is not valid %s',
154
                date(DateTime::RFC3339, $exp)
155
            )
156
        );
157
158
        self::callProtectedMethod(
159
            $token,
160
            'validateClaims'
161
        );
162
    }
163
164
    public function testValidateHeaderSuccess()
165
    {
166
        $token = new JWT(
167
            [],
168
            $this->getTestHeader()
169
        );
170
171
        self::callProtectedMethod(
172
            $token,
173
            'validateHeader'
174
        );
175
176
        // to skip warning
177
        parent::assertTrue(true);
178
    }
179
180
    public function testValidateHeaderNoAlg()
181
    {
182
        $token = new JWT(
183
            [],
184
            [
185
                'kid' => 'testSigKey'
186
            ]
187
        );
188
189
        parent::expectException(InvalidJWT::class);
190
        parent::expectExceptionMessage('No alg inside header');
191
192
        self::callProtectedMethod(
193
            $token,
194
            'validateHeader'
195
        );
196
    }
197
198
    public function testValidateHeaderNoKid()
199
    {
200
        $token = new JWT(
201
            [],
202
            [
203
                'alg' => 'RS256'
204
            ]
205
        );
206
207
        parent::expectException(InvalidJWT::class);
208
        parent::expectExceptionMessage('No kid inside header');
209
210
        self::callProtectedMethod(
211
            $token,
212
            'validateHeader'
213
        );
214
    }
215
216
    public function testDecodeWrongNumberOfSegments()
217
    {
218
        parent::expectException(InvalidJWT::class);
219
        parent::expectExceptionMessage('Wrong number of segments');
220
221
        JWT::decode(
222
            'lol',
223
            []
224
        );
225
    }
226
}
227