|
5 | 5 | namespace Redbitcz\DebugModeTests\Plugin;
|
6 | 6 |
|
7 | 7 | use Firebase\JWT\JWT;
|
| 8 | +use LogicException; |
8 | 9 | use Redbitcz\DebugMode\Plugin\SignedUrl;
|
| 10 | +use Redbitcz\DebugMode\Plugin\SignedUrlVerificationException; |
9 | 11 | use Tester\Assert;
|
10 | 12 |
|
11 | 13 | require __DIR__ . '/../bootstrap.php';
|
@@ -89,6 +91,103 @@ public function testVerifyRequest(): void
|
89 | 91 | $expected = [0, 1, 1600000600];
|
90 | 92 | Assert::equal($expected, $parsed);
|
91 | 93 | }
|
| 94 | + |
| 95 | + public function testSignInvalidUrl() |
| 96 | + { |
| 97 | + Assert::exception(function () { |
| 98 | + $url = (string)base64_decode('Ly8Eijrg+qawZw=='); |
| 99 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256'); |
| 100 | + $plugin->signUrl($url, 1600000600); |
| 101 | + }, LogicException::class); |
| 102 | + } |
| 103 | + |
| 104 | + public function testSignRelativeUrl() |
| 105 | + { |
| 106 | + Assert::exception(function () { |
| 107 | + $url = '/login?email=foo@bar.cz'; |
| 108 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256'); |
| 109 | + $plugin->signUrl($url, 1600000600); |
| 110 | + }, LogicException::class); |
| 111 | + } |
| 112 | + |
| 113 | + public function testVerifyPostRequest(): void |
| 114 | + { |
| 115 | + $audience = 'test.' . __FUNCTION__; |
| 116 | + $timestamp = 1600000000; |
| 117 | + $url = 'https://host.tld/path?query=value'; |
| 118 | + |
| 119 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256', $audience); |
| 120 | + $plugin->setTimestamp($timestamp); |
| 121 | + $tokenUrl = $plugin->signUrl($url, 1600000600); |
| 122 | + |
| 123 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256', $audience); |
| 124 | + $plugin->setTimestamp($timestamp); |
| 125 | + JWT::$timestamp = $timestamp; |
| 126 | + Assert::exception(function () use ($plugin, $tokenUrl) { |
| 127 | + $plugin->verifyRequest(false, $tokenUrl, 'POST'); |
| 128 | + }, SignedUrlVerificationException::class, 'HTTP method doesn\'t match signed HTTP method'); |
| 129 | + } |
| 130 | + |
| 131 | + public function testVerifyInvalidRequest(): void |
| 132 | + { |
| 133 | + Assert::exception(function () { |
| 134 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256'); |
| 135 | + $url = (string)base64_decode('Ly8Eijrg+qawZw=='); |
| 136 | + $plugin->verifyRequest(false, $url, 'GET'); |
| 137 | + }, SignedUrlVerificationException::class, 'Url is invalid'); |
| 138 | + } |
| 139 | + |
| 140 | + public function testVerifyInvalidUrl() |
| 141 | + { |
| 142 | + Assert::exception(function () { |
| 143 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256'); |
| 144 | + $plugin->verifyUrl('https://host.tld/path?query=value'); |
| 145 | + }, SignedUrlVerificationException::class, 'No token in URL'); |
| 146 | + } |
| 147 | + |
| 148 | + public function testVerifyUrlWithSuffix(): void |
| 149 | + { |
| 150 | + $timestamp = 1600000000; |
| 151 | + $url = 'https://host.tld/path?query=value'; |
| 152 | + |
| 153 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256'); |
| 154 | + $plugin->setTimestamp($timestamp); |
| 155 | + $tokenUrl = $plugin->signUrl($url, 1600000600); |
| 156 | + |
| 157 | + $tokenUrl .= '&fbclid=123456789'; |
| 158 | + |
| 159 | + Assert::exception( |
| 160 | + function () use ($timestamp, $tokenUrl) { |
| 161 | + $plugin = new SignedUrl(self::KEY_HS256, 'HS256'); |
| 162 | + $plugin->setTimestamp($timestamp); |
| 163 | + JWT::$timestamp = $timestamp; |
| 164 | + $plugin->verifyUrl($tokenUrl); |
| 165 | + }, |
| 166 | + SignedUrlVerificationException::class, |
| 167 | + 'URL contains unallowed queries after Signing Token' |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + public function testVerifyUrlWithSuffixRedirect(): void |
| 172 | + { |
| 173 | + $timestamp = 1600000000; |
| 174 | + $expected = 'https://host.tld/path?query=value&_debug=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjei5yZWRiaXQuZGVidWcudXJsIiwiYXVkIjoidGVzdC50ZXN0U2lnbiIsImlhdCI6MTYwMDAwMDAwMCwiZXhwIjoxNjAwMDAwNjAwLCJzdWIiOiJodHRwczpcL1wvaG9zdC50bGRcL3BhdGg_cXVlcnk9dmFsdWUiLCJtZXRoIjoiZ2V0IiwibW9kIjowLCJ2YWwiOjF9.61Z0pPW3lJN2WDoUhOfsZ4m16Q3hjtVFJep_t_qoQ5c'; |
| 175 | + |
| 176 | + $tokenUrl = $expected . '&fbclid=123456789'; |
| 177 | + |
| 178 | + // Mock plugin without redirect |
| 179 | + $plugin = new class(self::KEY_HS256, 'HS256', 'test.testSign') extends SignedUrl { |
| 180 | + protected function sendRedirectResponse(string $canonicalUrl): void |
| 181 | + { |
| 182 | + $expected = 'https://host.tld/path?query=value&_debug=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjei5yZWRiaXQuZGVidWcudXJsIiwiYXVkIjoidGVzdC50ZXN0U2lnbiIsImlhdCI6MTYwMDAwMDAwMCwiZXhwIjoxNjAwMDAwNjAwLCJzdWIiOiJodHRwczpcL1wvaG9zdC50bGRcL3BhdGg_cXVlcnk9dmFsdWUiLCJtZXRoIjoiZ2V0IiwibW9kIjowLCJ2YWwiOjF9.61Z0pPW3lJN2WDoUhOfsZ4m16Q3hjtVFJep_t_qoQ5c'; |
| 183 | + Assert::equal($canonicalUrl, $expected); |
| 184 | + } |
| 185 | + }; |
| 186 | + |
| 187 | + $plugin->setTimestamp($timestamp); |
| 188 | + JWT::$timestamp = $timestamp; |
| 189 | + $plugin->verifyUrl($tokenUrl, true); |
| 190 | + } |
92 | 191 | }
|
93 | 192 |
|
94 | 193 | (new SignUrlTest())->run();
|
0 commit comments