-
Notifications
You must be signed in to change notification settings - Fork 838
feat: Dynamic Client Registration Protocol (RFC 7591 / RFC 7592) #1667
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
zacharypodbela
wants to merge
1
commit into
django-oauth:master
Choose a base branch
from
ForaTravel:feature/dynamic-client-registration
base: master
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
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,158 @@ | ||
| Dynamic Client Registration | ||
| =========================== | ||
|
|
||
| Django OAuth Toolkit includes support for the OAuth 2.0 Dynamic Client Registration Protocol | ||
| (`RFC 7591 <https://datatracker.ietf.org/doc/html/rfc7591>`_) and the OAuth 2.0 Dynamic Client | ||
| Registration Management Protocol (`RFC 7592 <https://datatracker.ietf.org/doc/html/rfc7592>`_). | ||
|
|
||
| These views are automatically included in ``base_urlpatterns`` when you use | ||
| ``include("oauth2_provider.urls")``. | ||
|
|
||
|
|
||
| Endpoints | ||
| --------- | ||
|
|
||
| POST /o/register/ | ||
| ~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Creates a new OAuth2 application (RFC 7591). Authentication is controlled by | ||
| ``DCR_REGISTRATION_PERMISSION_CLASSES``. | ||
|
|
||
| **Request body (JSON):** | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| { | ||
| "redirect_uris": ["https://example.com/callback"], | ||
| "grant_types": ["authorization_code"], | ||
| "client_name": "My Application", | ||
| "token_endpoint_auth_method": "client_secret_basic" | ||
| } | ||
|
|
||
| **Response (201):** | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| { | ||
| "client_id": "abc123", | ||
| "client_secret": "...", | ||
| "redirect_uris": ["https://example.com/callback"], | ||
| "grant_types": ["authorization_code", "refresh_token"], | ||
| "token_endpoint_auth_method": "client_secret_basic", | ||
| "client_name": "My Application", | ||
| "registration_access_token": "...", | ||
| "registration_client_uri": "https://example.com/o/register/abc123/" | ||
| } | ||
|
|
||
| GET/PUT/DELETE /o/register/{client_id}/ | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Read, update, or delete the client configuration (RFC 7592). Requires a | ||
| ``Bearer {registration_access_token}`` header issued during registration. | ||
|
|
||
| - **GET** — returns current client metadata (same format as registration response) | ||
| - **PUT** — accepts the same JSON body as POST; updates the application | ||
| - **DELETE** — deletes the application and all associated tokens; returns 204 | ||
|
|
||
|
|
||
| Field Mapping | ||
| ------------- | ||
|
|
||
| +-------------------------------------+-----------------------------------+----------------------------------+ | ||
| | RFC 7591 field | DOT Application field | Notes | | ||
| +=====================================+===================================+==================================+ | ||
| | ``redirect_uris`` (array) | ``redirect_uris`` (space-joined) | | | ||
| +-------------------------------------+-----------------------------------+----------------------------------+ | ||
| | ``client_name`` | ``name`` | | | ||
| +-------------------------------------+-----------------------------------+----------------------------------+ | ||
| | ``grant_types`` (array) | ``authorization_grant_type`` | ``refresh_token`` is ignored; | | ||
| | | | only one non-refresh grant type | | ||
| | | | is supported per application | | ||
| +-------------------------------------+-----------------------------------+----------------------------------+ | ||
| | ``token_endpoint_auth_method: none``| ``client_type = "public"`` | | | ||
| +-------------------------------------+-----------------------------------+----------------------------------+ | ||
| | ``token_endpoint_auth_method: ...`` | ``client_type = "confidential"`` | Default | | ||
| +-------------------------------------+-----------------------------------+----------------------------------+ | ||
|
|
||
|
|
||
| Configuration | ||
| ------------- | ||
|
|
||
| Add the following keys to ``OAUTH2_PROVIDER`` in your Django settings. All are optional and have | ||
| sensible defaults. | ||
|
|
||
| ``DCR_ENABLED`` | ||
| Set to ``True`` to activate the Dynamic Client Registration endpoints. | ||
| When ``False`` (the default), both endpoints return ``404`` even though the | ||
| URL patterns are always registered. | ||
|
|
||
| Default: ``False`` | ||
|
|
||
| ``DCR_REGISTRATION_PERMISSION_CLASSES`` | ||
| A tuple of importable class paths whose instances are instantiated and called as | ||
| ``instance.has_permission(request) -> bool``. All classes must pass (AND logic). | ||
|
|
||
| Default: ``("oauth2_provider.dcr.IsAuthenticatedDCRPermission",)`` | ||
|
|
||
| Built-in classes: | ||
|
|
||
| * ``oauth2_provider.dcr.IsAuthenticatedDCRPermission`` — requires Django session authentication. | ||
| * ``oauth2_provider.dcr.AllowAllDCRPermission`` — open registration; no authentication required. | ||
|
|
||
| ``DCR_REGISTRATION_SCOPE`` | ||
| The scope string stored on the registration ``AccessToken`` used to protect the RFC 7592 | ||
| management endpoints. | ||
|
|
||
| Default: ``"oauth2_provider:registration"`` | ||
|
|
||
| ``DCR_REGISTRATION_TOKEN_EXPIRE_SECONDS`` | ||
| Number of seconds until the registration access token expires, or ``None`` for a | ||
| far-future expiry (year 9999, effectively non-expiring). | ||
|
|
||
| Default: ``None`` | ||
|
|
||
| ``DCR_ROTATE_REGISTRATION_TOKEN_ON_UPDATE`` | ||
| When ``True``, a PUT request to the management endpoint revokes the current registration | ||
| access token and issues a new one, returning it in the response. | ||
|
|
||
| Default: ``True`` | ||
|
|
||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| Open registration (no auth required): | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| OAUTH2_PROVIDER = { | ||
| "DCR_REGISTRATION_PERMISSION_CLASSES": ("oauth2_provider.dcr.AllowAllDCRPermission",), | ||
| } | ||
|
|
||
| Custom permission class (e.g. initial-access token): | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # myapp/permissions.py | ||
| class InitialAccessTokenPermission: | ||
| def has_permission(self, request) -> bool: | ||
| token = request.META.get("HTTP_AUTHORIZATION", "").removeprefix("Bearer ").strip() | ||
| return MyInitialToken.objects.filter(token=token, active=True).exists() | ||
|
|
||
| # settings.py | ||
| OAUTH2_PROVIDER = { | ||
| "DCR_REGISTRATION_PERMISSION_CLASSES": ("myapp.permissions.InitialAccessTokenPermission",), | ||
| } | ||
|
|
||
| Smoke test with ``curl``: | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| # Register (open mode) | ||
| curl -X POST https://example.com/o/register/ \\ | ||
| -H "Content-Type: application/json" \\ | ||
| -d '{"redirect_uris":["https://app.example.com/cb"],"grant_types":["authorization_code"]}' | ||
|
|
||
| # Read configuration | ||
| curl https://example.com/o/register/{client_id}/ \\ | ||
| -H "Authorization: Bearer {registration_access_token}" | ||
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,20 @@ | ||
| """ | ||
| Permission classes for the Dynamic Client Registration endpoint (RFC 7591). | ||
|
|
||
| Each class must implement ``has_permission(request) -> bool``. | ||
| Configure via ``OAUTH2_PROVIDER["DCR_REGISTRATION_PERMISSION_CLASSES"]``. | ||
| """ | ||
|
|
||
|
|
||
| class IsAuthenticatedDCRPermission: | ||
| """Allow registration only to session-authenticated users (default).""" | ||
|
|
||
| def has_permission(self, request) -> bool: | ||
| return bool(request.user and request.user.is_authenticated) | ||
|
|
||
|
|
||
| class AllowAllDCRPermission: | ||
| """Allow registration to anyone (open registration).""" | ||
|
|
||
| def has_permission(self, request) -> bool: | ||
| return True |
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 |
|---|---|---|
|
|
@@ -57,5 +57,13 @@ | |
| path("logout/", views.RPInitiatedLogoutView.as_view(), name="rp-initiated-logout"), | ||
| ] | ||
|
|
||
| dcr_urlpatterns = [ | ||
| path("register/", views.DynamicClientRegistrationView.as_view(), name="dcr-register"), | ||
| path( | ||
| "register/<str:client_id>/", | ||
| views.DynamicClientRegistrationManagementView.as_view(), | ||
| name="dcr-register-management", | ||
| ), | ||
| ] | ||
|
Comment on lines
+60
to
+67
|
||
|
|
||
| urlpatterns = base_urlpatterns + management_urlpatterns + oidc_urlpatterns | ||
| urlpatterns = base_urlpatterns + management_urlpatterns + oidc_urlpatterns + dcr_urlpatterns | ||
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.
This is inaccurate relative to the implementation: the DCR routes are added via
urlpatterns = ... + dcr_urlpatterns, not included inbase_urlpatterns. Please adjust the wording to avoid pointing readers to the wrong internal URL list (e.g., say the endpoints are included wheninclude(\"oauth2_provider.urls\")is used, without namingbase_urlpatterns).