Repository navigation
Conversation
…explicit and removes the deprecation warning for the authlib.jose library.Signed
064981e to
ff4c63f
Compare
|
@kishore7snehil Can we get a review of this please? it's an irritating warning... |
|
@blochs Can you please resolve the conflicts so I can take a look? |
Preserve upstream token exchange, organization policy, security validations, and test coverage while retaining JWT algorithm configuration, joserfc access-token and DPoP verification, and matching regression coverage.
Done - there weren't any conflicts when I put this up... |
|
@blochs UTs are failing. Can you please check? |
Use Authlib 1.6 JOSE on Python 3.9, where joserfc 1.7+ is not available through Authlib. Keep the non-deprecated joserfc JWT/JWK implementation on Python 3.10+ and preserve algorithm restrictions. Fixes PR auth0#134 Build and Test import failure on Python 3.9.
It's 3.9 as the issue, which I'm pretty sure is EOL. |
| if sys.version_info < (3, 10): | ||
| from authlib.jose import JsonWebKey, JsonWebToken | ||
| else: | ||
| from joserfc import jwk, jwt |
There was a problem hiding this comment.
joserfc is imported directly here but isn't listed in pyproject.toml or requirements.txt. It only arrives through authlib 1.7 and above, and authlib = "^1.0" still allows older versions that don't bring it. On Python 3.10+ with one of those, importing the package would fail.
Should we add joserfc as a dependency for 3.10+, or raise the authlib minimum? WDYT?
There was a problem hiding this comment.
3.9 is well past EOL. I'd raise the minimum?
There was a problem hiding this comment.
Agreed, 3.9 support was dropped on main in #159, so the 3.9 branches here can go after a rebase.
| if sys.version_info < (3, 10): | ||
| claims = self._dpop_jwt.decode(proof, public_key) | ||
| else: | ||
| claims = jwt.decode(proof, public_key, algorithms=self._dpop_algorithms).claims |
There was a problem hiding this comment.
On 3.10+ the decoded proof is used as a dict straight away, but joserfc doesn't check that the payload is a JSON object. A proof with a payload of 5, null, true or a plain string gets past decode and then fails in the claims check with a TypeError (for example "argument of type 'int' is not iterable"). Before this change those cases raised InvalidDpopProofError. verify_request only catches InvalidDpopProofError here, so callers would see the TypeError.
Shall we check the result is a dict right after decode and raise InvalidDpopProofError if not? A test with a non-object payload would help too, next to the existing non-object header tests.
| raise MissingRequiredArgumentError("audience") | ||
|
|
||
| if not isinstance(options.jwt_algorithms, list) or not options.jwt_algorithms or not all( | ||
| isinstance(algorithm, str) and algorithm for algorithm in options.jwt_algorithms |
There was a problem hiding this comment.
jwt_algorithms accepts any non-empty strings, so ["none"] or ["RS256", "none"] is allowed. With that config a token with alg: none and no signature passes verification on both the joserfc and Authlib paths (I tried 3.9 and 3.10). The default ["RS256"] still rejects it, so this only happens if someone opts in.
Can we reject none when the option is validated? WDYT?
| if sys.version_info < (3, 10): | ||
| claims = self._jwt.decode(access_token, public_key) | ||
| else: | ||
| claims = jwt.decode(access_token, public_key, algorithms=self._jwt_algorithms).claims |
There was a problem hiding this comment.
With joserfc, a JWKS key that has its own alg has to match the token's alg. So jwt_algorithms=["RS384", "RS512"] (the example in the new test) fails with unsupported_key_alg when the key says alg: RS256. The same setup works on 3.9 with Authlib, so the option behaves differently by Python version. I haven't tried this against a real tenant's JWKS.
Is this intended? If so, it needs a mention in the docs.
| audience="my-audience", | ||
| jwt_algorithms=["RS384", "RS512"], | ||
| )) | ||
| assert client._jwt_algorithms == ["RS384", "RS512"] |
There was a problem hiding this comment.
This only checks the private _jwt_algorithms attribute, so it doesn't show the option is used when a token is verified. I replaced the decode calls with a hardcoded ["RS256"] on both Python paths and all 470 tests still passed. Dropping the list-type check also left everything green.
Can we add a test that signs a token with RS384 and checks it's accepted only when configured and rejected with the default? generate_token hardcodes RS256, so it would need an alg option.
| token = jwt.encode(header, proof_claims, key) | ||
| else: | ||
| key = jwk.import_key(PRIVATE_EC_JWK) | ||
| registry = JWSRegistry( |
There was a problem hiding this comment.
This registry exists only so tests can set odd headers, and it lives in the installed package.
|
|
||
| from .utils import calculate_jwk_thumbprint, normalize_url_for_htu, sha256_base64url | ||
|
|
||
| # Python 3.9 resolves to Authlib 1.6 (without joserfc); use its JOSE API there. |
There was a problem hiding this comment.
This comment joins two clauses with a semicolon.
| Can be a static list of domain strings or a callable that returns | ||
| allowed domains dynamically. Optional if domain is provided. | ||
| audience: The expected 'aud' claim in the token. | ||
| jwt_algorithms: Allowed access-token signing algorithms (default: ["RS256"]). |
There was a problem hiding this comment.
This entry sits after audience, but the parameter is the last one in the signature. It also doesn't say that an empty list raises ConfigurationError.
|
|
||
| - Fetches OIDC metadata and JWKS if not already cached. | ||
| - Decodes and validates signature (RS256) with the correct key. | ||
| - Decodes and validates signature (allowed algorithms, default RS256) with the correct key. |
There was a problem hiding this comment.
The docstring says "default RS256" without naming the option.
| ApiClient(ApiClientOptions( | ||
| domain="example.us.auth0.com", | ||
| audience="my-audience", | ||
| jwt_algorithms=[], |
There was a problem hiding this comment.
There are no bad-input cases here (a tuple, a string, [None]), unlike the parameterized cases in test_utils.py.
|
@blochs Reviewed the changes and added few comments. PTAL! |
|
@blochs Are you working on this PR? |
| key = jwk.import_key(key_data) | ||
|
|
||
| header = {"alg": "RS256", "kid": PRIVATE_JWK["kid"]} | ||
| header = {"alg": algorithm, "kid": PRIVATE_JWK["kid"]} |
There was a problem hiding this comment.
On 3.10+ the encode call just below only allows the default joserfc algorithms (RS256, ES256 and HS256), so generate_token(algorithm="RS384") fails with "Algorithm of 'RS384' is not recommended". The two RS384 tests in test_api_client.py fail for this reason with the locked joserfc 1.7.4. The decode in verify_access_token already passes algorithms=, so only this helper is affected.
Can we pass algorithms=[algorithm] to encode on the joserfc path? With that change all 490 tests passed for me.
| import sys | ||
|
|
||
| # Authlib JOSE is retained for Python 3.9; newer runtimes use joserfc. | ||
| USE_LEGACY_JOSE = sys.version_info < (3, 10) |
There was a problem hiding this comment.
Python 3.9 support was dropped on main in #159, so this switch has nothing left to serve. Shall we remove this file and use joserfc directly everywhere once the branch is rebased? WDYT?
|
|
||
| # Authlib 1.6 remains the supported JOSE implementation on Python 3.9. | ||
| # joserfc 1.7+ (and Authlib's dependency on it) requires Python 3.10+. | ||
| if USE_LEGACY_JOSE: |
There was a problem hiding this comment.
With 3.9 gone this import can just be from joserfc import jwk, jwt, and the two comments above it about 3.9 can go with it.
|
|
||
| self._dpop_algorithms = ["ES256"] | ||
| self._dpop_jwt = JsonWebToken(self._dpop_algorithms) | ||
| if USE_LEGACY_JOSE: |
There was a problem hiding this comment.
_jwt and _dpop_jwt only exist for the Authlib path, so they can be removed too.
| # Respect the key's declared algorithm on both JOSE backends. | ||
| # A JWKS key for RS256 must not silently verify an RS384 token. | ||
| key_alg = matching_key_dict.get("alg") | ||
| if key_alg is not None and key_alg != alg: |
There was a problem hiding this comment.
Once 3.9 is dropped this check isn't needed, because joserfc already rejects a token whose alg differs from the key's alg (with unsupported_key_alg). It only changes the outcome on the Authlib path. We can keep it if we prefer the shorter SDK message, WDYT?
| [tool.poetry.dependencies] | ||
| python = ">=3.9.2" | ||
| authlib = "^1.0" # For JWT/OIDC features | ||
| authlib = "^1.0" # For JWT/OIDC features and Python 3.9 legacy JOSE |
There was a problem hiding this comment.
This line only exists for the 3.9 fallback. Nothing else in src imports authlib, so we can drop the dependency (and the matching line in requirements.txt) once the fallback is removed. WDYT?
| key; configuring `RS384` does not override a key declared for `RS256`. A tenant | ||
| that issues RS384 tokens must publish an RS384-capable key. | ||
|
|
||
| Python 3.9 uses Authlib's legacy JOSE implementation; Python 3.10+ uses |
There was a problem hiding this comment.
This paragraph describes the 3.9 versus 3.10+ split, which no longer applies. Can we reword it to just describe the joserfc behavior?
| ## Unreleased | ||
|
|
||
| **Changed** | ||
| - Migrated JOSE operations to `joserfc` on Python 3.10+ while retaining Authlib |
There was a problem hiding this comment.
This entry says Authlib is retained on 3.9. Once the fallback is dropped it can say the move to joserfc applies to all supported versions.
| key = jwk.import_key(PRIVATE_EC_JWK) | ||
|
|
||
| key = JsonWebKey.import_key(PRIVATE_EC_JWK) | ||
| # Sign a valid proof first. Malformed-header fixtures are intentionally |
There was a problem hiding this comment.
generate_dpop_proof now changes the header after signing when header_overrides is set, so those proofs have an invalid signature. That works for the header checks today, but it is a behavior change in a module that ships with the package.
| if sys.version_info < (3, 10): | ||
| from authlib.jose import JsonWebKey, JsonWebToken | ||
| else: | ||
| from joserfc import jwk, jwt |
There was a problem hiding this comment.
Agreed, 3.9 support was dropped on main in #159, so the 3.9 branches here can go after a rebase.
Changes
Replaced deprecated
authlib.joseusage with Authlib’s transitivejoserfcdependency.This removes the
AuthlibDeprecationWarningemitted by Authlib 1.7+ while preserving existing JWT, JWK, and DPoP behavior.Additional changes:
ApiClientOptionsnow acceptsjwt_algorithms, defaulting to["RS256"].ES256.joserfc’s stricter header validation.Example:
Alternative approaches considered:
joserfcas a direct dependency, but it remains transitive as requested.References
Testing
Reviewers can run:
Result:
210 passed.The test suite verifies JWT verification, DPoP verification, malformed DPoP headers, configurable algorithms, and existing behavior.
Checklist