-
Notifications
You must be signed in to change notification settings - Fork 46
Feature - Show the last OAuth provider used #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nathanmota-dev
wants to merge
9
commits into
he4rt:4.x
Choose a base branch
from
nathanmota-dev:feat/remember-oauth
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
349cd8f
feat(auth): remember last OAuth provider
nathanmota-dev 8412ca3
test(auth): cover last OAuth provider marker
nathanmota-dev 05f8406
refactor(auth): reuse OAuth callback URL
nathanmota-dev c5b23cd
refactor(auth): load OAuth marker script on demand
nathanmota-dev 8aa0d62
Merge branch '4.x' into feat/remember-oauth
danielhe4rt 086d56a
fix(panel-app): persist OAuth provider across login callback
nathanmota-dev 28b6375
chore: remove Docker build definitions
nathanmota-dev 353b404
Revert "chore: remove Docker build definitions"
nathanmota-dev 4f1bb10
Merge branch '4.x' into feat/remember-oauth
nathanmota-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
app-modules/identity/tests/Feature/Auth/OAuthControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Contracts\OAuthClientContract; | ||
| use Filament\Facades\Filament; | ||
| use He4rt\Identity\Auth\Actions\HandleOAuthCallbackAction; | ||
| use He4rt\Identity\Auth\DTOs\OAuthAccessDTO; | ||
| use He4rt\Identity\Auth\DTOs\OAuthStateDTO; | ||
| use He4rt\Identity\Auth\DTOs\OAuthUserDTO; | ||
| use He4rt\Identity\Auth\Enums\OAuthIntent; | ||
| use He4rt\Identity\Auth\Http\Controllers\OAuthController; | ||
| use He4rt\Identity\ExternalIdentity\Enums\IdentityProvider; | ||
| use He4rt\IntegrationGithub\OAuth\GitHubOAuthClient; | ||
| use Illuminate\Support\Facades\Auth; | ||
|
|
||
| function bindControllerGithubClient(): void | ||
| { | ||
| $access = new class('access-token', 'refresh-token', 3_600) extends OAuthAccessDTO | ||
| { | ||
| public static function make(array $payload): self | ||
| { | ||
| return new self('access-token', 'refresh-token', 3_600); | ||
| } | ||
| }; | ||
|
|
||
| $user = new class($access) extends OAuthUserDTO | ||
| { | ||
| public function __construct(OAuthAccessDTO $credentials) | ||
| { | ||
| parent::__construct( | ||
| credentials: $credentials, | ||
| providerId: 'controller-github-id', | ||
| provider: IdentityProvider::GitHub, | ||
| username: 'controller-user', | ||
| name: 'Controller User', | ||
| email: 'controller@example.com', | ||
| avatarUrl: null, | ||
| ); | ||
| } | ||
|
|
||
| public static function make(OAuthAccessDTO $credentials, array $payload): self | ||
| { | ||
| return new self($credentials); | ||
| } | ||
| }; | ||
|
|
||
| app()->instance(GitHubOAuthClient::class, new readonly class($access, $user) implements OAuthClientContract | ||
| { | ||
| public function __construct( | ||
| private OAuthAccessDTO $access, | ||
| private OAuthUserDTO $user, | ||
| ) {} | ||
|
|
||
| public function redirectUrl(?OAuthStateDTO $state = null): string | ||
| { | ||
| return 'https://github.test/oauth'; | ||
| } | ||
|
|
||
| public function auth(string $code): OAuthAccessDTO | ||
| { | ||
| return $this->access; | ||
| } | ||
|
|
||
| public function getAuthenticatedUser(OAuthAccessDTO $credentials): OAuthUserDTO | ||
| { | ||
| return $this->user; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function callGithubCallback(OAuthStateDTO $state): string | ||
| { | ||
| request()->merge([ | ||
| 'state' => (string) $state, | ||
| 'code' => 'auth-code', | ||
| ]); | ||
|
|
||
| return resolve(OAuthController::class) | ||
| ->getAuthenticate('github', resolve(HandleOAuthCallbackAction::class)) | ||
| ->getTargetUrl(); | ||
| } | ||
|
|
||
| test('successful app oauth login marks the provider in the redirect URL', function (): void { | ||
| Filament::setCurrentPanel(Filament::getPanel('app')); | ||
| bindControllerGithubClient(); | ||
|
|
||
| $targetUrl = callGithubCallback(new OAuthStateDTO( | ||
| intent: OAuthIntent::Login, | ||
| provider: IdentityProvider::GitHub, | ||
| panel: 'app', | ||
| returnUrl: '/app?source=oauth', | ||
| )); | ||
|
|
||
| expect($targetUrl) | ||
| ->toContain('source=oauth') | ||
| ->toContain('oauth_provider=github') | ||
| ->and(Auth::check())->toBeTrue(); | ||
| }); | ||
|
|
||
| test('denied app oauth login does not mark a provider in the redirect URL', function (): void { | ||
| $state = new OAuthStateDTO( | ||
| intent: OAuthIntent::Login, | ||
| provider: IdentityProvider::GitHub, | ||
| panel: 'app', | ||
| returnUrl: '/app/login', | ||
| ); | ||
|
|
||
| request()->merge([ | ||
| 'state' => (string) $state, | ||
| 'error' => 'access_denied', | ||
| ]); | ||
|
|
||
| $targetUrl = resolve(OAuthController::class) | ||
| ->getAuthenticate('github', resolve(HandleOAuthCallbackAction::class)) | ||
| ->getTargetUrl(); | ||
|
|
||
| expect($targetUrl) | ||
| ->toContain('/app/login') | ||
| ->not->toContain('oauth_provider='); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| (() => { | ||
| const url = new URL(window.location.href); | ||
| const provider = url.searchParams.get('oauth_provider'); | ||
| const supportedProviders = ['discord', 'github', 'twitch']; | ||
|
|
||
| if (!supportedProviders.includes(provider)) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| window.localStorage.setItem('lastAuthProvider', provider); | ||
| } catch (error) { | ||
| // Ignore browsers that block localStorage access. | ||
| } | ||
|
|
||
| url.searchParams.delete('oauth_provider'); | ||
| window.history.replaceState({}, document.title, url); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app-modules/panel-app/tests/Feature/Auth/LastAuthProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Filament\Facades\Filament; | ||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\PanelApp\Pages\TimelinePage; | ||
|
|
||
| beforeEach(function (): void { | ||
| Filament::setCurrentPanel(Filament::getPanel('app')); | ||
| }); | ||
|
|
||
| test('login reads the last provider without loading the writer asset', function (): void { | ||
| $this->get(Filament::getPanel('app')->getLoginUrl()) | ||
| ->assertSuccessful() | ||
| ->assertSee("window.localStorage.getItem('lastAuthProvider')", escape: false) | ||
| ->assertDontSee('last-auth-provider', escape: false); | ||
| }); | ||
|
|
||
| test('oauth callback landing loads the writer asset for a supported provider', function (): void { | ||
| $this->actingAs(User::factory()->create()); | ||
|
|
||
| $this->get(url()->query(TimelinePage::getUrl(), [ | ||
| 'oauth_provider' => 'github', | ||
| ])) | ||
| ->assertSuccessful() | ||
| ->assertSee('last-auth-provider', escape: false); | ||
| }); | ||
|
|
||
| test('dashboard does not load the writer asset without a supported oauth marker', function (array $query): void { | ||
| $this->actingAs(User::factory()->create()); | ||
|
|
||
| $this->get(url()->query(TimelinePage::getUrl(), $query)) | ||
| ->assertSuccessful() | ||
| ->assertDontSee('last-auth-provider', escape: false); | ||
| })->with([ | ||
| 'without marker' => [[]], | ||
| 'unsupported provider' => [['oauth_provider' => 'devto']], | ||
| ]); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.