Skip to content

Commit 885a219

Browse files
authored
fix(oauth): properly set state when creating the redirect URL (#1592)
1 parent 695f589 commit 885a219

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/auth/src/OAuth/GenericOAuthClient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ public function buildAuthorizationUrl(array $scopes = [], array $options = []):
6262

6363
public function createRedirect(array $scopes = [], array $options = []): Redirect
6464
{
65+
$to = $this->buildAuthorizationUrl();
66+
6567
$this->session->set($this->sessionKey, $this->provider->getState());
6668

67-
return new Redirect($this->buildAuthorizationUrl());
69+
return new Redirect($to);
6870
}
6971

7072
public function getState(): ?string

tests/Integration/Auth/OAuth/GenericOAuthClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Tempest\Auth\OAuth\Config\GitHubOAuthConfig;
88
use Tempest\Auth\OAuth\GenericOAuthClient;
99
use Tempest\Auth\OAuth\OAuthClient;
10+
use Tempest\Http\Session\Session;
1011
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1112

1213
final class GenericOAuthClientTest extends FrameworkIntegrationTestCase
@@ -31,4 +32,24 @@ public function throws_when_no_config(): void
3132

3233
$this->container->get(OAuthClient::class);
3334
}
35+
36+
#[Test]
37+
public function state_is_set_when_redirect_is_created(): void
38+
{
39+
$this->container->config(new GitHubOAuthConfig(
40+
clientId: 'client-id',
41+
clientSecret: 'client-secret', // @mago-expect lint:no-literal-password
42+
redirectTo: '/oauth/callback',
43+
scopes: ['user:email'],
44+
));
45+
46+
/** @var GenericOAuthClient $oauth */
47+
$oauth = $this->container->get(OAuthClient::class);
48+
49+
$oauth->createRedirect();
50+
51+
$session = $this->container->get(Session::class);
52+
53+
$this->assertNotNull($session->get($oauth->sessionKey));
54+
}
3455
}

0 commit comments

Comments
 (0)