-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuth.test.php
184 lines (143 loc) · 6.3 KB
/
OAuth.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace Tests;
require_once __DIR__ . "/OAuth.php";
use \PHPUnit\Framework\TestCase;
use \rogoss\OAuth\TokenException;
use \rogoss\OAuth\Token;
use \rogoss\OAuth\OAuth;
class TestOAuth extends TestCase
{
const SECRET = "abcdefghijk";
public function setUp(): void
{
# Turn on error reporting
ini_set("display_errors", "on");
ini_set("error_reporting", "E_ALL");
}
public function testOAuthInstanciation_WithToShortKey()
{
$this->expectException('\rogoss\OAuth\TokenException');
$this->expectExceptionCode(TokenException::SECRET_TO_SHORT);
OAuth::withSecret("123");
}
public function testOAuthInstanciation()
{
$this->assertInstanceOf('\rogoss\OAuth\OAuth', OAuth::blank(), "instantiation without secret");
$this->assertInstanceOf('\rogoss\OAuth\OAuth', OAuth::withSecret(self::SECRET), "instantiation with valid secret");
}
public function testGeneratorInstanciation()
{
$this->assertInstanceOf(
'\rogoss\OAuth\iTokenGenerator',
OAuth::withSecret(self::SECRET)->NewToken(),
);
}
function testEmptyTokenGeneration()
{
$oGenerator = OAuth::withSecret(self::SECRET)->NewToken();
$this->assertNotEmpty($sEmptyToken = $oGenerator->finalize());
$this->assertIsArray(
$aTokenJSON = json_decode($sEmptyToken, true),
"Token did not Parse in JSON"
);
$this->assertArrayHasKey("s", $aTokenJSON, "Token misses signature");
$this->assertNotEmpty($aTokenJSON['s'], "signature should not have been empty");
$this->assertArrayHasKey("m", $aTokenJSON, "Token misses Key");
$this->assertEmpty($aTokenJSON['m'], "mode should be empty, but is not");
$this->assertEquals(2, count($aTokenJSON), "Token should only contain 2 values");
}
function testEmptyTokenValidity()
{
$sEmptyToken = $this->newEmptyToken();
$oToken = Token::withSecret(self::SECRET);
$this->assertInstanceOf('\rogoss\OAuth\Token', $oToken, 'Token::blank() should have returned an instance of \rogoss\OAuth\Token');
$oPrimedToken = $oToken->parseToken($sEmptyToken);
$this->assertInstanceOf('\rogoss\OAuth\Token', $oPrimedToken, '$oToken->parseToken($sEmptyToken) should have returned an instance of \rogoss\OAuth\Token');
$this->assertSame($oToken, $oPrimedToken, '$oPrimedToken should be a reference to $oToken, not a Copy');
}
function testTokenWithManipulation()
{
$this->_tokenModeManipTest_1(hash("sha256", "hello"), "s");
$this->_tokenModeManipTest_1("a");
$this->_tokenModeManipTest_1("t");
$this->_tokenModeManipTest_1("c");
}
function testTokenBuilderFullParams()
{
$oTokenBase = OAuth::withSecret(self::SECRET)->NewToken();
$this->assertInstanceOf('\rogoss\OAuth\iTokenGenerator', $oTokenBase, 'NewToken did not return iTokenGenerator');
$oToken = $oTokenBase->setAgent("Agent A");
$this->assertSame($oToken, $oTokenBase, "setAgent did not return itself");
$oToken = $oToken->setTTL(3600);
$this->assertSame($oToken, $oTokenBase, "setTTL did not return itself");
$oToken = $oToken->setContent("Hello World");
$this->assertSame($oToken, $oTokenBase, "setContent did not return itself");
$sToken = $oToken->finalize(true);
$this->assertMatchesRegularExpression('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $sToken, "Generated token does not seem to be base64");
$oDecodedTokenBase = OAuth::withSecret(self::SECRET)->LoadToken($sToken, true);
$this->assertInstanceOf('\rogoss\OAuth\Token', $oDecodedTokenBase, 'Parsed Token did not have the correct class');
$oDecodedToken = $oDecodedTokenBase->requiresAgent("Agent A");
$this->assertSame($oDecodedToken, $oDecodedTokenBase, "requiresAgent did not return itself");
$oDecodedToken = $oDecodedTokenBase->requiresNoneExpired();
$this->assertSame($oDecodedToken, $oDecodedTokenBase, "requiresAgent did not return itself");
$this->assertEquals("Hello World", $oDecodedToken->getContent(), "unexpected token content");
}
function testTokenWithManipulatedAgent()
{
$this->_tokenModeManipTest_2("a", hash("sha256", "yo"));
}
function testTokenWithManipulatedTTL()
{
$this->_tokenModeManipTest_2("ttl", time() + 7200);
}
function testTokenWithManipulatedContent()
{
$this->_tokenModeManipTest_2("c", "A,B,C");
}
function testTokenWithTimeout()
{
$sToken = $this->newFullToken(1);
sleep(1);
$this->expectException('\rogoss\OAuth\TokenException');
$this->expectExceptionCode(TokenException::TOKEN_EXPIRED);
OAuth::withSecret(self::SECRET)
->LoadToken($sToken)
->requiresNoneExpired();
}
// BM: Private Helpers
//======================================================================
private function newEmptyToken(): string
{
$sToken = OAuth::withSecret(self::SECRET)->NewToken()->finalize();
$this->assertNotEmpty($sToken, "this token should not have been empty");
return $sToken;
}
private function newFullToken($iTTL = 2000): string
{
return OAuth::withSecret(self::SECRET)
->NewToken()
->setAgent("Agent A")
->setTTL($iTTL)
->setContent("A,B")
->finalize();
}
private function _tokenModeManipTest_1($sMode, $sTokenField = "m")
{
$aToken = json_decode($this->newEmptyToken(), true);
$aToken[$sTokenField] = $sMode;
$sToken = json_encode($aToken);
$this->expectException('\rogoss\OAuth\TokenException');
$this->expectExceptionCode(TokenException::INVALID_TOKEN_STRUCT);
OAuth::withSecret(self::SECRET)->LoadToken($sToken, false);
}
private function _tokenModeManipTest_2($sTokenField, $mValue)
{
$sRawToken = $this->newFullToken();
$aToken = json_decode($sRawToken, true);
$aToken[$sTokenField] = $mValue;
$sToken = json_encode($aToken);
$this->expectException('\rogoss\OAuth\TokenException');
$this->expectExceptionCode(TokenException::INVALID_TOKEN_STRUCT);
OAuth::withSecret(self::SECRET)->LoadToken($sToken);
}
}