-
Notifications
You must be signed in to change notification settings - Fork 557
fix(OAuth): flagsmith login never completes for a new signup
#8281
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
khvn26
wants to merge
3
commits into
main
Choose a base branch
from
fix/cli-login-signup-flow
base: main
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
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from typing import TypedDict | ||
|
|
||
|
|
||
| class ScopeDescription(TypedDict): | ||
| """How a scope is presented to a user asked to consent to it.""" | ||
|
|
||
| label: str | ||
| grants: list[str] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from pytest_django.fixtures import SettingsWrapper | ||
|
|
||
| from oauth2_metadata.constants import SCOPE_ADMIN_API, SCOPE_GRANTS, SCOPE_MCP | ||
| from oauth2_metadata.mappers import map_scopes_to_descriptions | ||
|
|
||
|
|
||
| def test_map_scopes_to_descriptions__described_scopes__returns_labels_and_grants() -> ( | ||
| None | ||
| ): | ||
| # Given | ||
| scopes = [SCOPE_MCP, SCOPE_ADMIN_API] | ||
|
|
||
| # When | ||
| descriptions = map_scopes_to_descriptions(scopes) | ||
|
|
||
| # Then | ||
| assert descriptions == { | ||
| SCOPE_MCP: { | ||
| "label": "MCP access", | ||
| "grants": list(SCOPE_GRANTS[SCOPE_MCP]), | ||
| }, | ||
| SCOPE_ADMIN_API: { | ||
| "label": "Admin API access", | ||
| "grants": list(SCOPE_GRANTS[SCOPE_ADMIN_API]), | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def test_map_scopes_to_descriptions__scope_without_grants__returns_label_only( | ||
| settings: SettingsWrapper, | ||
| ) -> None: | ||
| # Given | ||
| settings.OAUTH2_PROVIDER = { | ||
| **settings.OAUTH2_PROVIDER, | ||
| "SCOPES": {**settings.OAUTH2_PROVIDER["SCOPES"], "read": "Read access"}, | ||
| } | ||
|
|
||
| # When | ||
| descriptions = map_scopes_to_descriptions(["read"]) | ||
|
|
||
| # Then | ||
| assert descriptions == {"read": {"label": "Read access", "grants": []}} | ||
|
|
||
|
|
||
| def test_map_scopes_to_descriptions__unregistered_scope__falls_back_to_its_name() -> ( | ||
| None | ||
| ): | ||
| # Given | ||
| scopes = ["not-a-scope"] | ||
|
|
||
| # When | ||
| descriptions = map_scopes_to_descriptions(scopes) | ||
|
|
||
| # Then | ||
| assert descriptions == {"not-a-scope": {"label": "not-a-scope", "grants": []}} | ||
|
|
||
|
|
||
| def test_map_scopes_to_descriptions__no_scopes__returns_empty() -> None: | ||
| # Given | ||
| scopes: list[str] = [] | ||
|
|
||
| # When | ||
| descriptions = map_scopes_to_descriptions(scopes) | ||
|
|
||
| # Then | ||
| assert descriptions == {} |
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
41 changes: 41 additions & 0 deletions
41
frontend/common/utils/__tests__/pendingAuthorisation.test.ts
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,41 @@ | ||
| import { isPendingAuthorisation } from 'common/utils/pendingAuthorisation' | ||
|
|
||
| describe('isPendingAuthorisation', () => { | ||
| it('matches a consent request with its query', () => { | ||
| expect( | ||
| isPendingAuthorisation( | ||
| '/oauth/authorize?client_id=flagsmith-cli&scope=admin-api&state=abc', | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('matches the trailing-slash form the CLI is sent to', () => { | ||
| expect(isPendingAuthorisation('/oauth/authorize/?client_id=x')).toBe(true) | ||
| }) | ||
|
|
||
| it('matches the bare path', () => { | ||
| expect(isPendingAuthorisation('/oauth/authorize')).toBe(true) | ||
| }) | ||
|
|
||
| it('does not match an identity provider callback', () => { | ||
| expect(isPendingAuthorisation('/oauth/google?code=abc')).toBe(false) | ||
| }) | ||
|
|
||
| it('does not match another page', () => { | ||
| expect(isPendingAuthorisation('/project/1/environment/abc/features')).toBe( | ||
| false, | ||
| ) | ||
| }) | ||
|
|
||
| it('does not match an absolute url wearing the path', () => { | ||
| expect(isPendingAuthorisation('https://evil.example/oauth/authorize')).toBe( | ||
| false, | ||
| ) | ||
| }) | ||
|
|
||
| it('is false when there is no redirect', () => { | ||
| expect(isPendingAuthorisation(undefined)).toBe(false) | ||
| expect(isPendingAuthorisation(null)).toBe(false) | ||
| expect(isPendingAuthorisation('')).toBe(false) | ||
| }) | ||
| }) |
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 @@ | ||
| // The dashboard hosts the OAuth consent screen (it is the authorization | ||
| // endpoint in our RFC 8414 metadata), so a CLI or MCP client that opened it | ||
| // while logged out is now blocked on a loopback callback that only this browser | ||
| // can answer - and it gives up after a few minutes. | ||
| export const AUTHORISE_PATH = '/oauth/authorize' | ||
|
|
||
| /** | ||
| * Whether a stored post-login redirect is a consent request waiting to be | ||
| * answered. Compares the whole path so an absolute URL never matches: the | ||
| * redirect is read from a cookie, and this decides where we send the browser. | ||
| */ | ||
| export const isPendingAuthorisation = ( | ||
| redirect?: string | null, | ||
| ): redirect is string => { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (!redirect) return false | ||
| const path = redirect.split('?')[0] | ||
| return path.replace(/\/+$/, '') === AUTHORISE_PATH | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The immutability of tuples does not justify using them as immutable collections. Please, if you agree, prefer to use tuples when they could be interchanged with their verbose brother,
NamedTuple.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's hard to justify
frozensethere considering we don't need O(1) membership checks here; and using a mutable type likelistmakes things geniunely worse in my opinion.I'm sorry, I'd rather you come to terms with the fact that
tuple[str, ...]is a legitimate type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I continue to disagree, and I'd be glad to argument, though this isn't worth it. I will try not to pick these from now on.