-
Notifications
You must be signed in to change notification settings - Fork 500
feat: setup-dot-and-metadata-endpoint #7057
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
Zaimwa9
wants to merge
16
commits into
main
Choose a base branch
from
feat/setup-dot-and-as-metadata
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.
+464
−62
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fccaa3a
feat: initial-dot-setup
Zaimwa9 e190637
feat: node-test-application
Zaimwa9 cf0c378
feat: lint
Zaimwa9 02d5fba
feat: registered-metadata-for-task-processing
Zaimwa9 eeb1584
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6780f71
feat: updated-url-default
Zaimwa9 e330174
Merge branch 'feat/setup-dot-and-as-metadata' of github.com:Flagsmith…
Zaimwa9 ccd5ffd
feat: regenerated-open-api-specs
Zaimwa9 262ebda
feat: oauth-only-consumes-authorization-header
Zaimwa9 1f0d84e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ba950be
feat: type-linting
Zaimwa9 a528acc
feat: tested-authentication-and-cleartoken-call
Zaimwa9 eb8ccff
feat: lint
Zaimwa9 74d4fc1
feat: rebased
Zaimwa9 36477da
feat: fixed-dependencies-type-errors
Zaimwa9 ec0b067
feat: regenerate-openapi-specs
Zaimwa9 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
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,8 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class OAuth2MetadataConfig(AppConfig): | ||
| name = "oauth2_metadata" | ||
|
|
||
| def ready(self) -> None: | ||
| from oauth2_metadata import tasks # noqa: F401 |
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,17 @@ | ||
| from typing import Any | ||
|
|
||
| from oauth2_provider.contrib.rest_framework import OAuth2Authentication | ||
| from rest_framework.request import Request | ||
|
|
||
|
|
||
| class OAuth2BearerTokenAuthentication(OAuth2Authentication): # type: ignore[misc] | ||
| """DOT's default OAuth2Authentication also reads the request body | ||
| looking for an access_token, which consumes the stream and breaks | ||
| views that need to read request.body. | ||
| """ | ||
|
|
||
| def authenticate(self, request: Request) -> tuple[Any, Any] | None: | ||
| auth_header = request.META.get("HTTP_AUTHORIZATION", "") | ||
| if not auth_header.startswith("Bearer "): | ||
| return None | ||
| return super().authenticate(request) # type: ignore[no-any-return] |
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,9 @@ | ||
| from datetime import timedelta | ||
|
|
||
| from django.core.management import call_command | ||
| from task_processor.decorators import register_recurring_task | ||
|
|
||
|
|
||
| @register_recurring_task(run_every=timedelta(hours=24)) | ||
| def clear_expired_oauth2_tokens() -> None: | ||
| call_command("cleartokens") |
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 typing import Any | ||
|
|
||
| from django.conf import settings | ||
| from django.http import HttpRequest, JsonResponse | ||
| from django.views.decorators.csrf import csrf_exempt | ||
| from django.views.decorators.http import require_GET | ||
|
|
||
|
|
||
| @csrf_exempt | ||
| @require_GET | ||
| def authorization_server_metadata(request: HttpRequest) -> JsonResponse: | ||
| """RFC 8414 OAuth 2.0 Authorization Server Metadata.""" | ||
| api_url: str = settings.FLAGSMITH_API_URL.rstrip("/") | ||
| frontend_url: str = settings.FLAGSMITH_FRONTEND_URL.rstrip("/") | ||
| oauth2_settings: dict[str, Any] = settings.OAUTH2_PROVIDER | ||
| scopes: dict[str, str] = oauth2_settings.get("SCOPES", {}) | ||
|
|
||
| metadata = { | ||
| "issuer": api_url, | ||
| "authorization_endpoint": f"{frontend_url}/oauth/authorize/", | ||
| "token_endpoint": f"{api_url}/o/token/", | ||
| "registration_endpoint": f"{api_url}/o/register/", | ||
| "revocation_endpoint": f"{api_url}/o/revoke_token/", | ||
| "introspection_endpoint": f"{api_url}/o/introspect/", | ||
| "scopes_supported": list(scopes.keys()), | ||
| "response_types_supported": ["code"], | ||
| "grant_types_supported": ["authorization_code", "refresh_token"], | ||
| "code_challenge_methods_supported": ["S256"], | ||
| "token_endpoint_auth_methods_supported": [ | ||
| "client_secret_basic", | ||
| "client_secret_post", | ||
| "none", | ||
| ], | ||
| "introspection_endpoint_auth_methods_supported": ["none"], | ||
| } | ||
|
|
||
| return JsonResponse(metadata) |
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,81 @@ | ||
| import { createServer } from "node:http"; | ||
| import { randomBytes, createHash } from "node:crypto"; | ||
|
|
||
| const CLIENT_ID = "ZLsLu3hhJI4GlhNsGeFVC3K2U3QBGfXtmc0EcyiG"; | ||
| const REDIRECT_URI = "http://localhost:3000/oauth/callback"; | ||
| const API_URL = "http://localhost:8000"; | ||
| const PORT = 3000; | ||
|
|
||
| // Generate PKCE values | ||
| const codeVerifier = randomBytes(96).toString("base64url").slice(0, 128); | ||
| const codeChallenge = createHash("sha256") | ||
| .update(codeVerifier) | ||
| .digest("base64url"); | ||
|
|
||
| const authorizeUrl = | ||
| `${API_URL}/o/authorize/?` + | ||
| new URLSearchParams({ | ||
| response_type: "code", | ||
| client_id: CLIENT_ID, | ||
| redirect_uri: REDIRECT_URI, | ||
| scope: "mcp", | ||
| code_challenge: codeChallenge, | ||
| code_challenge_method: "S256", | ||
| }); | ||
|
|
||
| const server = createServer(async (req, res) => { | ||
| const url = new URL(req.url, `http://localhost:${PORT}`); | ||
|
|
||
| if (url.pathname === "/oauth/callback") { | ||
| const code = url.searchParams.get("code"); | ||
| const error = url.searchParams.get("error"); | ||
|
|
||
| if (error) { | ||
| res.writeHead(400, { "Content-Type": "text/plain" }); | ||
| res.end(`Error: ${error}\n${url.searchParams.get("error_description")}`); | ||
| return; | ||
| } | ||
|
|
||
| if (!code) { | ||
| res.writeHead(400, { "Content-Type": "text/plain" }); | ||
| res.end("No authorization code received"); | ||
| return; | ||
| } | ||
|
|
||
| console.log(`\nReceived authorization code: ${code}`); | ||
| console.log("Exchanging for token...\n"); | ||
|
|
||
| // Exchange code for token | ||
| const tokenRes = await fetch(`${API_URL}/o/token/`, { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
| body: new URLSearchParams({ | ||
| grant_type: "authorization_code", | ||
| code, | ||
| redirect_uri: REDIRECT_URI, | ||
| client_id: CLIENT_ID, | ||
| code_verifier: codeVerifier, | ||
| }), | ||
| }); | ||
|
|
||
| const tokenData = await tokenRes.json(); | ||
| console.log("Token response:", JSON.stringify(tokenData, null, 2)); | ||
|
|
||
| res.writeHead(200, { "Content-Type": "text/html" }); | ||
| res.end(`<pre>${JSON.stringify(tokenData, null, 2)}</pre>`); | ||
|
|
||
| // Done - shut down | ||
| setTimeout(() => { | ||
| console.log("\nDone. Shutting down."); | ||
| process.exit(0); | ||
| }, 1000); | ||
| } else { | ||
| res.writeHead(302, { Location: authorizeUrl }); | ||
| res.end(); | ||
| } | ||
| }); | ||
|
|
||
| server.listen(PORT, () => { | ||
| console.log(`OAuth test server running on http://localhost:${PORT}`); | ||
| console.log(`\nOpen http://localhost:${PORT} in your browser to start the flow.\n`); | ||
| }); |
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.
Reminder