|
5 | 5 | use PHPUnit\Framework\Attributes\Test; |
6 | 6 | use PHPUnit\Framework\Attributes\TestWith; |
7 | 7 | use Tempest\Auth\Authentication\Authenticatable; |
| 8 | +use Tempest\Auth\Exceptions\OAuthStateWasInvalid; |
8 | 9 | use Tempest\Auth\OAuth\Config\GitHubOAuthConfig; |
9 | 10 | use Tempest\Auth\OAuth\OAuthClient; |
10 | 11 | use Tempest\Auth\OAuth\OAuthUser; |
@@ -263,6 +264,69 @@ public function abstracted_flow(): void |
263 | 264 | $this->assertEquals('Jane Developer', $user->full_name); |
264 | 265 | $this->assertEquals('janedev', $user->username); |
265 | 266 | } |
| 267 | + |
| 268 | + #[Test] |
| 269 | + public function state_is_cleared_after_authentication(): void |
| 270 | + { |
| 271 | + $this->database->reset(migrate: false); |
| 272 | + $this->database->migrate(CreateMigrationsTable::class, CreateUsersTable::class); |
| 273 | + |
| 274 | + $this->container->config(new GitHubOAuthConfig( |
| 275 | + clientId: 'foo', |
| 276 | + clientSecret: 'bar', // @mago-expect lint:no-literal-password |
| 277 | + redirectTo: '/oauth/github', |
| 278 | + )); |
| 279 | + |
| 280 | + $client = $this->oauth->fake($this->user); |
| 281 | + |
| 282 | + $client->createRedirect(); |
| 283 | + |
| 284 | + $this->assertNotNull($client->getState()); |
| 285 | + |
| 286 | + $request = new GenericRequest( |
| 287 | + method: Method::GET, |
| 288 | + uri: Uri\set_query('/oauth/callback', code: 'auth-code', state: $client->getState()), |
| 289 | + ); |
| 290 | + |
| 291 | + $client->authenticate( |
| 292 | + $request, |
| 293 | + static fn (OAuthUser $user): User => query(User::class)->updateOrCreate( |
| 294 | + ['github_id' => $user->id], |
| 295 | + ['email' => $user->email ?? '', 'full_name' => $user->name ?? '', 'username' => $user->nickname ?? ''], |
| 296 | + ), |
| 297 | + ); |
| 298 | + |
| 299 | + $this->assertNull($client->getState()); |
| 300 | + } |
| 301 | + |
| 302 | + #[Test] |
| 303 | + public function state_is_cleared_even_when_validation_fails(): void |
| 304 | + { |
| 305 | + $this->container->config(new GitHubOAuthConfig( |
| 306 | + clientId: 'foo', |
| 307 | + clientSecret: 'bar', // @mago-expect lint:no-literal-password |
| 308 | + redirectTo: '/oauth/github', |
| 309 | + )); |
| 310 | + |
| 311 | + $client = $this->oauth->fake($this->user); |
| 312 | + |
| 313 | + $client->createRedirect(); |
| 314 | + |
| 315 | + $this->assertNotNull($client->getState()); |
| 316 | + |
| 317 | + $request = new GenericRequest( |
| 318 | + method: Method::GET, |
| 319 | + uri: Uri\set_query('/oauth/callback', code: 'auth-code', state: 'invalid-state'), |
| 320 | + ); |
| 321 | + |
| 322 | + try { |
| 323 | + $client->authenticate($request, static fn (OAuthUser $user): User => new User('', '', '', '')); |
| 324 | + } catch (OAuthStateWasInvalid) { |
| 325 | + // @mago-expect lint:no-empty-catch-clause |
| 326 | + } |
| 327 | + |
| 328 | + $this->assertNull($client->getState()); |
| 329 | + } |
266 | 330 | } |
267 | 331 |
|
268 | 332 | final class User implements Authenticatable |
|
0 commit comments