xref: /dokuwiki/_test/tests/inc/JWTTest.php (revision 7ffd5bd274abfe72d9284c134c58dfa6ee0fdc80)
1403d6a9fSAndreas Gohr<?php
2403d6a9fSAndreas Gohr
3403d6a9fSAndreas Gohrnamespace dokuwiki\test;
4403d6a9fSAndreas Gohr
5403d6a9fSAndreas Gohruse dokuwiki\JWT;
6403d6a9fSAndreas Gohr
7403d6a9fSAndreas Gohrclass JWTTest extends \DokuWikiTest
8403d6a9fSAndreas Gohr{
9403d6a9fSAndreas Gohr
10403d6a9fSAndreas Gohr
11403d6a9fSAndreas Gohr    public function testCreation()
12403d6a9fSAndreas Gohr    {
13403d6a9fSAndreas Gohr        // no token file yet
14403d6a9fSAndreas Gohr        $file = JWT::getStorageFile('test');
15403d6a9fSAndreas Gohr        $this->assertFileNotExists($file);
16403d6a9fSAndreas Gohr
17403d6a9fSAndreas Gohr        // initialize a new token
18403d6a9fSAndreas Gohr        $jwt = JWT::fromUser('test');
19403d6a9fSAndreas Gohr        $this->assertFileExists($file);
20403d6a9fSAndreas Gohr        $this->assertEquals('test', $jwt->getUser());
21403d6a9fSAndreas Gohr        $token = $jwt->getToken();
22403d6a9fSAndreas Gohr        $issued = $jwt->getIssued();
23403d6a9fSAndreas Gohr
24403d6a9fSAndreas Gohr        // validate the token
25403d6a9fSAndreas Gohr        $jwt = JWT::validate($token);
26403d6a9fSAndreas Gohr        $this->assertEquals('test', $jwt->getUser());
27403d6a9fSAndreas Gohr        $this->assertEquals($issued, $jwt->getIssued());
28403d6a9fSAndreas Gohr
29403d6a9fSAndreas Gohr
30403d6a9fSAndreas Gohr        // next access should get the same token
31403d6a9fSAndreas Gohr        $jwt = JWT::fromUser('test');
32403d6a9fSAndreas Gohr        $this->assertEquals($token, $jwt->getToken());
33403d6a9fSAndreas Gohr        $this->assertEquals($issued, $jwt->getIssued());
34403d6a9fSAndreas Gohr
35403d6a9fSAndreas Gohr        // saving should create a new token
36403d6a9fSAndreas Gohr        sleep(1); // make sure we have a new timestamp
37403d6a9fSAndreas Gohr        $jwt->save();
38403d6a9fSAndreas Gohr        $this->assertNotEquals($token, $jwt->getToken());
39403d6a9fSAndreas Gohr        $this->assertNotEquals($issued, $jwt->getIssued());
40403d6a9fSAndreas Gohr    }
41403d6a9fSAndreas Gohr
42403d6a9fSAndreas Gohr    public function testValidationFail()
43403d6a9fSAndreas Gohr    {
44403d6a9fSAndreas Gohr        $this->expectException(\Exception::class);
45403d6a9fSAndreas Gohr        $this->expectExceptionMessage('Invalid JWT signature');
46403d6a9fSAndreas Gohr        JWT::validate('invalid');
47403d6a9fSAndreas Gohr    }
48403d6a9fSAndreas Gohr
49403d6a9fSAndreas Gohr    public function testLoadFail()
50403d6a9fSAndreas Gohr    {
51403d6a9fSAndreas Gohr        $jwt = JWT::fromUser('test');
52403d6a9fSAndreas Gohr        $token = $jwt->getToken();
53403d6a9fSAndreas Gohr        $file = JWT::getStorageFile('test');
54403d6a9fSAndreas Gohr        unlink($file);
55403d6a9fSAndreas Gohr
56403d6a9fSAndreas Gohr        $this->expectException(\Exception::class);
57403d6a9fSAndreas Gohr        $this->expectExceptionMessage('JWT not found, maybe it expired?');
58403d6a9fSAndreas Gohr        JWT::validate($token);
59403d6a9fSAndreas Gohr    }
60403d6a9fSAndreas Gohr
61403d6a9fSAndreas Gohr    public function testLoadExpireFail()
62403d6a9fSAndreas Gohr    {
63403d6a9fSAndreas Gohr        $jwt = JWT::fromUser('test');
64403d6a9fSAndreas Gohr        $token = $jwt->getToken();
65403d6a9fSAndreas Gohr        sleep(1); // make sure we have a new timestamp
66403d6a9fSAndreas Gohr        $jwt->save();
67403d6a9fSAndreas Gohr
68403d6a9fSAndreas Gohr        $this->expectException(\Exception::class);
69403d6a9fSAndreas Gohr        $this->expectExceptionMessage('JWT invalid, maybe it expired?');
70403d6a9fSAndreas Gohr        JWT::validate($token);
71403d6a9fSAndreas Gohr    }
72403d6a9fSAndreas Gohr
73403d6a9fSAndreas Gohr    public function testLogin()
74403d6a9fSAndreas Gohr    {
75403d6a9fSAndreas Gohr        $_SERVER['HTTP_AUTHORIZATION'] =  'Bearer ' . JWT::fromUser('testuser')->getToken();
76403d6a9fSAndreas Gohr
77403d6a9fSAndreas Gohr        $this->assertArrayNotHasKey('REMOTE_USER', $_SERVER);
78403d6a9fSAndreas Gohr        auth_tokenlogin();
79403d6a9fSAndreas Gohr        $this->assertEquals('testuser', $_SERVER['REMOTE_USER']);
80403d6a9fSAndreas Gohr        unset($_SERVER['HTTP_AUTHORIZATION']);
81403d6a9fSAndreas Gohr    }
82*7ffd5bd2SAndreas Gohr
83*7ffd5bd2SAndreas Gohr    public function testLoginAlternativeHeader()
84*7ffd5bd2SAndreas Gohr    {
85*7ffd5bd2SAndreas Gohr        $_SERVER['HTTP_X-DOKUWIKI-TOKEN'] =  JWT::fromUser('testuser')->getToken();
86*7ffd5bd2SAndreas Gohr
87*7ffd5bd2SAndreas Gohr        $this->assertArrayNotHasKey('REMOTE_USER', $_SERVER);
88*7ffd5bd2SAndreas Gohr        auth_tokenlogin();
89*7ffd5bd2SAndreas Gohr        $this->assertEquals('testuser', $_SERVER['REMOTE_USER']);
90*7ffd5bd2SAndreas Gohr        unset($_SERVER['HTTP_X-DOKUWIKI-TOKEN']);
91*7ffd5bd2SAndreas Gohr    }
92403d6a9fSAndreas Gohr}
93