blob: 2d901b5019128a2a92bfb571da8d4fccda9d10dc [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3namespace OAuth2\Storage;
4
5class AuthorizationCodeTest extends BaseTest
6{
7 /** @dataProvider provideStorage */
8 public function testGetAuthorizationCode(AuthorizationCodeInterface $storage)
9 {
10 if ($storage instanceof NullStorage) {
11 $this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());
12
13 return;
14 }
15
16 // nonexistant client_id
17 $details = $storage->getAuthorizationCode('faketoken');
18 $this->assertFalse($details);
19
20 // valid client_id
21 $details = $storage->getAuthorizationCode('testtoken');
22 $this->assertNotNull($details);
23 }
24
25 /** @dataProvider provideStorage */
26 public function testSetAuthorizationCode(AuthorizationCodeInterface $storage)
27 {
28 if ($storage instanceof NullStorage) {
29 $this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());
30
31 return;
32 }
33
34 // assert code we are about to add does not exist
35 $code = $storage->getAuthorizationCode('newcode');
36 $this->assertFalse($code);
37
38 // add new code
39 $expires = time() + 20;
40 $success = $storage->setAuthorizationCode('newcode', 'client ID', 'SOMEUSERID', 'http://example.com', $expires);
41 $this->assertTrue($success);
42
43 $code = $storage->getAuthorizationCode('newcode');
44 $this->assertNotNull($code);
45 $this->assertArrayHasKey('authorization_code', $code);
46 $this->assertArrayHasKey('client_id', $code);
47 $this->assertArrayHasKey('user_id', $code);
48 $this->assertArrayHasKey('redirect_uri', $code);
49 $this->assertArrayHasKey('expires', $code);
50 $this->assertEquals($code['authorization_code'], 'newcode');
51 $this->assertEquals($code['client_id'], 'client ID');
52 $this->assertEquals($code['user_id'], 'SOMEUSERID');
53 $this->assertEquals($code['redirect_uri'], 'http://example.com');
54 $this->assertEquals($code['expires'], $expires);
55
56 // change existing code
57 $expires = time() + 42;
58 $success = $storage->setAuthorizationCode('newcode', 'client ID2', 'SOMEOTHERID', 'http://example.org', $expires);
59 $this->assertTrue($success);
60
61 $code = $storage->getAuthorizationCode('newcode');
62 $this->assertNotNull($code);
63 $this->assertArrayHasKey('authorization_code', $code);
64 $this->assertArrayHasKey('client_id', $code);
65 $this->assertArrayHasKey('user_id', $code);
66 $this->assertArrayHasKey('redirect_uri', $code);
67 $this->assertArrayHasKey('expires', $code);
68 $this->assertEquals($code['authorization_code'], 'newcode');
69 $this->assertEquals($code['client_id'], 'client ID2');
70 $this->assertEquals($code['user_id'], 'SOMEOTHERID');
71 $this->assertEquals($code['redirect_uri'], 'http://example.org');
72 $this->assertEquals($code['expires'], $expires);
73
74 // add new code with scope having an empty string value
75 $expires = time() + 20;
76 $success = $storage->setAuthorizationCode('newcode', 'client ID', 'SOMEUSERID', 'http://example.com', $expires, '');
77 $this->assertTrue($success);
78 }
79
80 /** @dataProvider provideStorage */
81 public function testExpireAccessToken(AccessTokenInterface $storage)
82 {
83 if ($storage instanceof NullStorage) {
84 $this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());
85
86 return;
87 }
88
89 // create a valid code
90 $expires = time() + 20;
91 $success = $storage->setAuthorizationCode('code-to-expire', 'client ID', 'SOMEUSERID', 'http://example.com', time() + 20);
92 $this->assertTrue($success);
93
94 // verify the new code exists
95 $code = $storage->getAuthorizationCode('code-to-expire');
96 $this->assertNotNull($code);
97
98 $this->assertArrayHasKey('authorization_code', $code);
99 $this->assertEquals($code['authorization_code'], 'code-to-expire');
100
101 // now expire the code and ensure it's no longer available
102 $storage->expireAuthorizationCode('code-to-expire');
103 $code = $storage->getAuthorizationCode('code-to-expire');
104 $this->assertFalse($code);
105 }
106}