fix: migrate authlib.jose to joserfc (#1197)#1199
Conversation
authlib.jose emits AuthlibDeprecationWarning and will be incompatible before authlib 2.0.0. Replace with joserfc, the authlib-recommended successor library. Changes: - codecarbon/cli/auth.py: KeySet.import_key_set + jwt.decode + JWTClaimsRegistry().validate() for access-token validation - carbonserver oidc_auth_provider.py: same migration in _decode_token - Add joserfc>=1.0.0 to lib and server dependencies - Update cli auth tests to patch joserfc.jwk.KeySet instead of authlib.jose.JsonWebKey; token.claims now an attribute, not a dict with validate() method
Address review follow-ups on the authlib.jose -> joserfc migration: - tests/cli/test_cli_auth.py: strengthen test_validate_access_token_valid with a realistic claims dict (exp/iat/sub) so JWTClaimsRegistry actually exercises its default validators instead of trivially passing on an empty payload. Add test_validate_access_token_expired_returns_false to pin down the expiry-rejection behaviour through the real registry. - carbonserver/tests/api/service/test_auth_provider.py: add test_decode_token_falls_back_to_jwks_when_fief_fails covering the previously-untested JWKS fallback in OIDCAuthProvider._decode_token — the only joserfc-using path in carbonserver.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1199 +/- ##
=======================================
Coverage 88.90% 88.90%
=======================================
Files 45 45
Lines 4301 4301
=======================================
Hits 3824 3824
Misses 477 477 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
LGTM. |
|
Copilot review :
|
Description
Swaps
authlib.joseforjoserfc— authlib's JOSE-only successor — in the two files that import it:codecarbon/cli/auth.py(_validate_access_token)carbonserver/.../oidc_auth_provider.py(_decode_tokenJWKS fallback)authlibstays a dependency for OAuth2 primitives (OAuth2Session, starletteOAuth, PKCE,generate_token). Onlyauthlib.josemoves.Call translation:
JsonWebKey.import_key_set→KeySet.import_key_setjose_jwt.decode(token, keyset)returns aTokenwith.claimsas a plain dictclaims.validate()→jose_jwt.JWTClaimsRegistry().validate(token.claims)(same defaults:exp/nbf/iat, no leeway)Related Issue
Resolves #1197.
Motivation and Context
codecarbon --versionemitsAuthlibDeprecationWarning: authlib.jose module is deprecated, please use joserfc instead.joserfc is authlib's own recommended replacement (https://jose.authlib.org/en/). Migrating now removes the warning and keeps us compatible past authlib 2.0.How Has This Been Tested?
joserfc/_rfc7519/claims.pyagainstauthlib/jose/rfc7519/claims.pyto confirm same defaults (now=int(time.time()),leeway=0, sameexp/nbf/iatchecks, sameExpiredTokenError).tests/cli/test_cli_auth.py: 18/18. Patch targets updated;test_validate_access_token_validnow uses a realistic claims dict soJWTClaimsRegistryactually runs; addedtest_validate_access_token_expired_returns_false.carbonserver/tests/api/service/test_auth_provider.py: 2/2. Addedtest_decode_token_falls_back_to_jwks_when_fief_fails— the JWKS path had no prior coverage.uv run task test-api-unit: 86 passed, 1 skipped, 1 xfailed.uv run codecarbon --version: warning gone.Types of changes
AI Usage Disclosure
authlib; AI helped with the joserfc API translation and test scaffolding.Checklist