Subversion Repositories Applications.annuaire

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
536 mathias 1
<?php
2
 
3
class JWTTest extends PHPUnit_Framework_TestCase
4
{
5
    public function testEncodeDecode()
6
    {
7
        $msg = JWT::encode('abc', 'my_key');
8
        $this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
9
    }
10
 
11
    public function testDecodeFromPython()
12
    {
13
        $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
14
        $this->assertEquals(
15
            JWT::decode($msg, 'my_key', array('HS256')),
16
            '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123'
17
        );
18
    }
19
 
20
    public function testUrlSafeCharacters()
21
    {
22
        $encoded = JWT::encode('f?', 'a');
23
        $this->assertEquals('f?', JWT::decode($encoded, 'a', array('HS256')));
24
    }
25
 
26
    public function testMalformedUtf8StringsFail()
27
    {
28
        $this->setExpectedException('DomainException');
29
        JWT::encode(pack('c', 128), 'a');
30
    }
31
 
32
    public function testMalformedJsonThrowsException()
33
    {
34
        $this->setExpectedException('DomainException');
35
        JWT::jsonDecode('this is not valid JSON string');
36
    }
37
 
38
    public function testExpiredToken()
39
    {
40
        $this->setExpectedException('ExpiredException');
41
        $payload = array(
42
            "message" => "abc",
43
            "exp" => time() - 20); // time in the past
44
        $encoded = JWT::encode($payload, 'my_key');
45
        JWT::decode($encoded, 'my_key', array('HS256'));
46
    }
47
 
48
    public function testBeforeValidTokenWithNbf()
49
    {
50
        $this->setExpectedException('BeforeValidException');
51
        $payload = array(
52
            "message" => "abc",
53
            "nbf" => time() + 20); // time in the future
54
        $encoded = JWT::encode($payload, 'my_key');
55
        JWT::decode($encoded, 'my_key', array('HS256'));
56
    }
57
 
58
    public function testBeforeValidTokenWithIat()
59
    {
60
        $this->setExpectedException('BeforeValidException');
61
        $payload = array(
62
            "message" => "abc",
63
            "iat" => time() + 20); // time in the future
64
        $encoded = JWT::encode($payload, 'my_key');
65
        JWT::decode($encoded, 'my_key', array('HS256'));
66
    }
67
 
68
    public function testValidToken()
69
    {
70
        $payload = array(
71
            "message" => "abc",
72
            "exp" => time() + 20); // time in the future
73
        $encoded = JWT::encode($payload, 'my_key');
74
        $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
75
        $this->assertEquals($decoded->message, 'abc');
76
    }
77
 
78
    public function testValidTokenWithList()
79
    {
80
        $payload = array(
81
            "message" => "abc",
82
            "exp" => time() + 20); // time in the future
83
        $encoded = JWT::encode($payload, 'my_key');
84
        $decoded = JWT::decode($encoded, 'my_key', array('HS256', 'HS512'));
85
        $this->assertEquals($decoded->message, 'abc');
86
    }
87
 
88
    public function testValidTokenWithNbf()
89
    {
90
        $payload = array(
91
            "message" => "abc",
92
            "iat" => time(),
93
            "exp" => time() + 20, // time in the future
94
            "nbf" => time() - 20);
95
        $encoded = JWT::encode($payload, 'my_key');
96
        $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
97
        $this->assertEquals($decoded->message, 'abc');
98
    }
99
 
100
    public function testInvalidToken()
101
    {
102
        $payload = array(
103
            "message" => "abc",
104
            "exp" => time() + 20); // time in the future
105
        $encoded = JWT::encode($payload, 'my_key');
106
        $this->setExpectedException('SignatureInvalidException');
107
        $decoded = JWT::decode($encoded, 'my_key2', array('HS256'));
108
    }
109
 
110
    public function testRSEncodeDecode()
111
    {
112
        $privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
113
            'private_key_bits' => 1024,
114
            'private_key_type' => OPENSSL_KEYTYPE_RSA));
115
        $msg = JWT::encode('abc', $privKey, 'RS256');
116
        $pubKey = openssl_pkey_get_details($privKey);
117
        $pubKey = $pubKey['key'];
118
        $decoded = JWT::decode($msg, $pubKey, array('RS256'));
119
        $this->assertEquals($decoded, 'abc');
120
    }
121
 
122
    public function testKIDChooser()
123
    {
124
        $keys = array('1' => 'my_key', '2' => 'my_key2');
125
        $msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
126
        $decoded = JWT::decode($msg, $keys, array('HS256'));
127
        $this->assertEquals($decoded, 'abc');
128
    }
129
 
130
    public function testNoneAlgorithm()
131
    {
132
        $msg = JWT::encode('abc', 'my_key');
133
        $this->setExpectedException('DomainException');
134
        JWT::decode($msg, 'my_key', array('none'));
135
    }
136
 
137
    public function testIncorrectAlgorithm()
138
    {
139
        $msg = JWT::encode('abc', 'my_key');
140
        $this->setExpectedException('DomainException');
141
        JWT::decode($msg, 'my_key', array('RS256'));
142
    }
143
 
144
    public function testMissingAlgorithm()
145
    {
146
        $msg = JWT::encode('abc', 'my_key');
147
        $this->setExpectedException('DomainException');
148
        JWT::decode($msg, 'my_key');
149
    }
150
}