-
Notifications
You must be signed in to change notification settings - Fork 559
feat(oauth): First-party CLI client and per-client scope policy #8029
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1494407
feat(oauth): First-party CLI client and per-client scope policy
khvn26 aa33ddc
slop / docs
khvn26 f43b649
fix(oauth): Migration rollback deletes a pre-existing CLI application
khvn26 e72355c
refactor(oauth): Freeze CLI application metadata in the data migration
khvn26 eaf68b6
feat(oauth): Enable refresh token reuse protection
khvn26 8b7acdf
test(oauth): Assert every seeded field survives an existing application
khvn26 fcf7ee3
more slop / docs
khvn26 1b93aa6
test(oauth): Pin literal endpoint paths in authorize view tests
khvn26 e4c7f0a
rename the scope and remove trace of "management api"
khvn26 4380518
test(oauth): Rename admin-api scope test functions to match
khvn26 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| FLAGSMITH_CLI_CLIENT_ID = "flagsmith-cli" | ||
|
|
||
| SCOPE_MCP = "mcp" | ||
| SCOPE_ADMIN_API = "admin-api" | ||
|
|
||
| FIRST_PARTY_CLIENT_IDS = frozenset({FLAGSMITH_CLI_CLIENT_ID}) | ||
| FIRST_PARTY_SCOPES = frozenset({SCOPE_ADMIN_API}) | ||
| THIRD_PARTY_SCOPES = frozenset({SCOPE_MCP}) |
37 changes: 37 additions & 0 deletions
37
api/oauth2_metadata/migrations/0001_create_flagsmith_cli_application.py
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,37 @@ | ||
| from django.apps.registry import Apps | ||
| from django.db import migrations | ||
| from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
|
|
||
|
|
||
|
|
||
| def create_flagsmith_cli_application( | ||
| apps: Apps, | ||
| schema_editor: BaseDatabaseSchemaEditor, | ||
| ) -> None: | ||
| Application = apps.get_model("oauth2_provider", "Application") | ||
| Application.objects.get_or_create( | ||
| client_id="flagsmith-cli", | ||
| defaults={ | ||
| "name": "Flagsmith CLI", | ||
| "client_type": "public", | ||
| "authorization_grant_type": "authorization-code", | ||
| "client_secret": "", | ||
| "redirect_uris": "http://127.0.0.1/callback http://[::1]/callback", | ||
| "skip_authorization": True, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("oauth2_provider", "0012_add_token_checksum"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| create_flagsmith_cli_application, | ||
| migrations.RunPython.noop, | ||
| ), | ||
| ] |
Empty file.
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,51 @@ | ||
| from typing import Any | ||
|
|
||
| from oauth2_provider.models import Application | ||
| from oauth2_provider.scopes import SettingsScopes | ||
|
|
||
| from oauth2_metadata.constants import ( | ||
| FIRST_PARTY_CLIENT_IDS, | ||
| FIRST_PARTY_SCOPES, | ||
| THIRD_PARTY_SCOPES, | ||
| ) | ||
|
|
||
|
|
||
| class FlagsmithScopes(SettingsScopes): # type: ignore[misc] | ||
| """Per-client scope issuance policy.""" | ||
|
|
||
| def get_allowed_scopes( | ||
| self, | ||
| application: Application | None = None, | ||
| ) -> frozenset[str]: | ||
| if application is not None: | ||
| if application.client_id in FIRST_PARTY_CLIENT_IDS: | ||
| return FIRST_PARTY_SCOPES | ||
| return THIRD_PARTY_SCOPES | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| def get_available_scopes( | ||
| self, | ||
| application: Application | None = None, | ||
| request: Any = None, | ||
| *args: Any, | ||
| **kwargs: Any, | ||
| ) -> list[str]: | ||
| """What may a client request.""" | ||
| allowed_scopes = self.get_allowed_scopes(application) | ||
| scopes: list[str] = super().get_available_scopes( | ||
| application, request, *args, **kwargs | ||
| ) | ||
| return [scope for scope in scopes if scope in allowed_scopes] | ||
|
|
||
| def get_default_scopes( | ||
| self, | ||
| application: Application | None = None, | ||
| request: Any = None, | ||
| *args: Any, | ||
| **kwargs: Any, | ||
| ) -> list[str]: | ||
| """What does a client get when it doesn't request for any particular scopes.""" | ||
| allowed_scopes = self.get_allowed_scopes(application) | ||
| scopes: list[str] = super().get_default_scopes( | ||
| application, request, *args, **kwargs | ||
| ) | ||
| return [scope for scope in scopes if scope in allowed_scopes] | ||
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,63 @@ | ||
| from django.contrib.auth.hashers import check_password | ||
| from django_test_migrations.migrator import Migrator | ||
|
|
||
|
|
||
| def test_0001__fresh_install__creates_flagsmith_cli_application( | ||
| migrator: Migrator, | ||
| ) -> None: | ||
| # Given | ||
| old_state = migrator.apply_initial_migration(("oauth2_metadata", None)) | ||
| OldApplication = old_state.apps.get_model("oauth2_provider", "Application") | ||
| OldApplication.objects.filter(client_id="flagsmith-cli").delete() | ||
|
|
||
| # When | ||
| new_state = migrator.apply_tested_migration( | ||
| ("oauth2_metadata", "0001_create_flagsmith_cli_application") | ||
| ) | ||
|
|
||
| # Then | ||
| Application = new_state.apps.get_model("oauth2_provider", "Application") | ||
| application = Application.objects.get(client_id="flagsmith-cli") | ||
| assert application.name == "Flagsmith CLI" | ||
| assert application.client_type == "public" | ||
| assert application.authorization_grant_type == "authorization-code" | ||
| # The client_secret field hashes on save; the stored value must be the | ||
| # hash of an empty secret (public client, token_endpoint_auth "none"). | ||
| assert check_password("", application.client_secret) | ||
| assert ( | ||
| application.redirect_uris == "http://127.0.0.1/callback http://[::1]/callback" | ||
| ) | ||
| assert application.skip_authorization is True | ||
|
|
||
|
|
||
| def test_0001__application_already_exists__does_not_overwrite( | ||
| migrator: Migrator, | ||
| ) -> None: | ||
| # Given | ||
| old_state = migrator.apply_initial_migration(("oauth2_metadata", None)) | ||
| OldApplication = old_state.apps.get_model("oauth2_provider", "Application") | ||
| OldApplication.objects.filter(client_id="flagsmith-cli").delete() | ||
| OldApplication.objects.create( | ||
| client_id="flagsmith-cli", | ||
| name="Pre-existing Application", | ||
| client_type="confidential", | ||
| authorization_grant_type="client-credentials", | ||
| client_secret="pre-existing-secret", | ||
| redirect_uris="https://example.com/callback", | ||
| skip_authorization=False, | ||
| ) | ||
|
|
||
| # When | ||
| new_state = migrator.apply_tested_migration( | ||
| ("oauth2_metadata", "0001_create_flagsmith_cli_application") | ||
| ) | ||
|
|
||
| # Then | ||
| Application = new_state.apps.get_model("oauth2_provider", "Application") | ||
| application = Application.objects.get(client_id="flagsmith-cli") | ||
| assert application.name == "Pre-existing Application" | ||
| assert application.client_type == "confidential" | ||
| assert application.authorization_grant_type == "client-credentials" | ||
| assert check_password("pre-existing-secret", application.client_secret) | ||
| assert application.redirect_uris == "https://example.com/callback" | ||
| assert application.skip_authorization is False | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.