forked from microsoft/moodle-auth_oidc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjwt_test.php
123 lines (104 loc) · 3.78 KB
/
jwt_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package auth_oidc
* @author James McQuillan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2014 onwards Microsoft, Inc. (http://microsoft.com/)
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Tests jwt
*
* @group auth_oidc
* @group office365
*/
class auth_oidc_jwt_testcase extends \advanced_testcase {
/**
* Perform setup before every test. This tells Moodle's phpunit to reset the database after every test.
*/
protected function setUp() {
parent::setUp();
$this->resetAfterTest(true);
}
/**
* Dataprovider for test_decode.
*
* @return array Array of arrays of test parameters.
*/
public function dataprovider_decode() {
$tests = [];
$tests['emptytest'] = [
'', '', ['Exception', 'Empty or non-string JWT received.']
];
$tests['nonstringtest'] = [
100, '', ['Exception', 'Empty or non-string JWT received.']
];
$tests['malformed1'] = [
'a', '', ['Exception', 'Malformed JWT received.']
];
$tests['malformed2'] = [
'a.b', '', ['Exception', 'Malformed JWT received.']
];
$tests['malformed3'] = [
'a.b.c.d', '', ['Exception', 'Malformed JWT received.']
];
$tests['badheader1'] = [
'h.p.s', '', ['Exception', 'Could not read JWT header']
];
$header = base64_encode(json_encode(['key' => 'val']));
$tests['invalidheader1'] = [
$header.'.p.s', '', ['Exception', 'Invalid JWT header']
];
$header = base64_encode(json_encode(['alg' => 'ROT13']));
$tests['badalg1'] = [
$header.'.p.s', '', ['Exception', 'JWS Alg or JWE not supported']
];
$header = base64_encode(json_encode(['alg' => 'RS256']));
$payload = 'p';
$tests['badpayload1'] = [
$header.'.'.$payload.'.s', '', ['Exception', 'Could not read JWT payload.']
];
$header = base64_encode(json_encode(['alg' => 'RS256']));
$payload = base64_encode('nothing');
$tests['badpayload2'] = [
$header.'.'.$payload.'.s', '', ['Exception', 'Could not read JWT payload.']
];
$header = ['alg' => 'RS256'];
$payload = ['payload' => 'found'];
$headerenc = base64_encode(json_encode($header));
$payloadenc = base64_encode(json_encode($payload));
$expected = [$header, $payload];
$tests['goodpayload1'] = [
$headerenc.'.'.$payloadenc.'.s', $expected, []
];
return $tests;
}
/**
* Test decode.
*
* @dataProvider dataprovider_decode
*/
public function test_decode($encodedjwt, $expectedresult, $expectedexception) {
if (!empty($expectedexception)) {
$this->expectException($expectedexception[0]);
$this->expectExceptionMessage($expectedexception[1]);
}
$actualresult = \auth_oidc\jwt::decode($encodedjwt);
$this->assertEquals($expectedresult, $actualresult);
}
}