aboutsummaryrefslogtreecommitdiffstats
path: root/library/oauth2/test/OAuth2/OpenID/Storage/AuthorizationCodeTest.php
blob: bdfb085e3c3251b72e18d100efed83b921be4a2d (plain) (blame)
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
<?php

namespace OAuth2\OpenID\Storage;

use OAuth2\Storage\BaseTest;
use OAuth2\Storage\NullStorage;

class AuthorizationCodeTest extends BaseTest
{
    /** @dataProvider provideStorage */
    public function testCreateAuthorizationCode($storage)
    {
        if ($storage instanceof NullStorage) {
            $this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());

            return;
        }

        if (!$storage instanceof AuthorizationCodeInterface) {
            return;
        }

        // assert code we are about to add does not exist
        $code = $storage->getAuthorizationCode('new-openid-code');
        $this->assertFalse($code);

        // add new code
        $expires = time() + 20;
        $scope = null;
        $id_token = 'fake_id_token';
        $success = $storage->setAuthorizationCode('new-openid-code', 'client ID', 'SOMEUSERID', 'http://example.com', $expires, $scope, $id_token);
        $this->assertTrue($success);

        $code = $storage->getAuthorizationCode('new-openid-code');
        $this->assertNotNull($code);
        $this->assertArrayHasKey('authorization_code', $code);
        $this->assertArrayHasKey('client_id', $code);
        $this->assertArrayHasKey('user_id', $code);
        $this->assertArrayHasKey('redirect_uri', $code);
        $this->assertArrayHasKey('expires', $code);
        $this->assertEquals($code['authorization_code'], 'new-openid-code');
        $this->assertEquals($code['client_id'], 'client ID');
        $this->assertEquals($code['user_id'], 'SOMEUSERID');
        $this->assertEquals($code['redirect_uri'], 'http://example.com');
        $this->assertEquals($code['expires'], $expires);
        $this->assertEquals($code['id_token'], $id_token);

        // change existing code
        $expires = time() + 42;
        $new_id_token = 'fake_id_token-2';
        $success = $storage->setAuthorizationCode('new-openid-code', 'client ID2', 'SOMEOTHERID', 'http://example.org', $expires, $scope, $new_id_token);
        $this->assertTrue($success);

        $code = $storage->getAuthorizationCode('new-openid-code');
        $this->assertNotNull($code);
        $this->assertArrayHasKey('authorization_code', $code);
        $this->assertArrayHasKey('client_id', $code);
        $this->assertArrayHasKey('user_id', $code);
        $this->assertArrayHasKey('redirect_uri', $code);
        $this->assertArrayHasKey('expires', $code);
        $this->assertEquals($code['authorization_code'], 'new-openid-code');
        $this->assertEquals($code['client_id'], 'client ID2');
        $this->assertEquals($code['user_id'], 'SOMEOTHERID');
        $this->assertEquals($code['redirect_uri'], 'http://example.org');
        $this->assertEquals($code['expires'], $expires);
        $this->assertEquals($code['id_token'], $new_id_token);
    }

        /** @dataProvider provideStorage */
    public function testRemoveIdTokenFromAuthorizationCode($storage)
    {
        // add new code
        $expires = time() + 20;
        $scope = null;
        $id_token = 'fake_id_token_to_remove';
        $authcode = 'new-openid-code-'.rand();
        $success = $storage->setAuthorizationCode($authcode, 'client ID', 'SOMEUSERID', 'http://example.com', $expires, $scope, $id_token);
        $this->assertTrue($success);

        // verify params were set
        $code = $storage->getAuthorizationCode($authcode);
        $this->assertNotNull($code);
        $this->assertArrayHasKey('id_token', $code);
        $this->assertEquals($code['id_token'], $id_token);

        // remove the id_token
        $success = $storage->setAuthorizationCode($authcode, 'client ID', 'SOMEUSERID', 'http://example.com', $expires, $scope, null);

        // verify the "id_token" is now null
        $code = $storage->getAuthorizationCode($authcode);
        $this->assertNotNull($code);
        $this->assertArrayHasKey('id_token', $code);
        $this->assertEquals($code['id_token'], null);
    }
}