feat: let ResourceProtector take a token_validator in __init__ - #925
Open
pryzmatical wants to merge 1 commit into
Open
feat: let ResourceProtector take a token_validator in __init__#925pryzmatical wants to merge 1 commit into
pryzmatical wants to merge 1 commit into
Conversation
ResourceProtector() always needed a follow-up .register_token_validator(...) call before it was usable, even for the common case of a single validator. Added an optional token_validator kwarg to __init__ that just calls register_token_validator() internally, so ResourceProtector(token_validator=MyBearerTokenValidator()) works in one line. register_token_validator() is unchanged and still how you add more than one validator (e.g. a second token type). Flask and Django both subclass the base ResourceProtector without overriding __init__, so this covers both integrations without touching either subclass. OAuth1's ResourceProtector is a separate, unrelated class (query_client/query_token/exists_nonce callbacks, no register_token_validator concept), so it's out of scope here. Updated the Flask and Django resource-server docs to show the new form, and added a changelog entry. Closes authlib#603
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #603. That issue's been open a while with no response, so wanted to flag up front: I haven't gotten explicit sign-off on this direction from a maintainer — happy to adjust or drop this if the API isn't wanted as-is.
What this does
ResourceProtector()always needed a follow-up.register_token_validator(...)call before it was usable, even for the common single-validator case. This adds an optionaltoken_validatorkwarg to__init__that just callsregister_token_validator()internally:register_token_validator()itself is untouched — it's still how you add more than one validator (e.g. supporting a second token type alongside bearer).Scope
Flask's and Django's
ResourceProtectorsubclasses don't override__init__, so they inherit this for free — no changes needed in either integration module. I checked OAuth1'sResourceProtectortoo: it's a completely separate class (query_client/query_token/exists_noncecallbacks, noregister_token_validatorconcept at all), so this doesn't apply there and I left it alone.Changes
authlib/oauth2/rfc6749/resource_protector.py— the__init__changetests/core/test_oauth2/test_resource_protector.py— new file (there wasn't a dedicated unit test forResourceProtectoritself, only Flask/Django integration-level coverage). Covers: no-validator behavior is unchanged, the constructor path andregister_token_validator()end up in the same state, and additional validators can still be registered afterward.docs/oauth2/resource-server/flask.rst,docs/oauth2/resource-server/django.rst— updated the getting-started examples to the new formdocs/upgrades/changelog.rst— entry under UnreleasedTesting
599 passed, 2 skipped (pre-existing, unrelated) — ran against the full suite, not just the new test file, to make sure nothing else assumes
ResourceProtector.__init__takes no arguments.ruff checkandruff format --checkboth clean.