Skip to content

Add JSON Web Token (HMAC) encode/decode#269

Merged
JE-Chen merged 1 commit into
devfrom
feat/jwt
Jun 20, 2026
Merged

Add JSON Web Token (HMAC) encode/decode#269
JE-Chen merged 1 commit into
devfrom
feat/jwt

Conversation

@JE-Chen

@JE-Chen JE-Chen commented Jun 20, 2026

Copy link
Copy Markdown
Member

Summary

RPA flows constantly need to mint/verify bearer tokens for the APIs they drive, but the framework only had HMAC file signing (action_signing) and an ACME-bound RS256 JWS (acme_v2) — neither produces or validates a compact bearer JWT. This adds a focused, pure-stdlib JWT codec for the HMAC family.

  • encode_jwt(claims, key, *, alg="HS256") — signs a compact header.payload.signature token (HS256/384/512).
  • decode_jwt(token, key, policy=None, *, now=None) — verifies the signature and validates claims against a ClaimsPolicy (exp/nbf with leeway, aud membership, iss match), using an injectable clock.
  • ClaimsPolicy groups the validation knobs (algorithms, audience, issuer, leeway, verify_exp, verify_nbf).

Security (safe by default): rejects alg:"none" and any algorithm not in the caller's allowlist (defeats the algorithm-confusion/downgrade attack), and compares signatures with hmac.compare_digest. RS256/ES256 are intentionally out of scope (need a third-party crypto lib). The minted token feeds straight into the existing http_request(..., auth={"type":"bearer","token":token}).

Pure stdlib (hmac/hashlib/base64/json); injectable now; Qt-free.

Five-layer wiring

  • Headless core: je_auto_control/utils/jwt/
  • Facade: re-exported from __init__.py + __all__
  • Executor: AC_jwt_encode, AC_jwt_decode (returns {ok, claims} / {ok:false, error})
  • MCP: ac_jwt_encode, ac_jwt_decode
  • Script Builder: "JWT: Sign Token" / "JWT: Verify Token" under Security

Tests & docs

  • test/unit_test/headless/test_jwt_batch.py (14 tests incl. alg-confusion, allowlist, exp/nbf/aud/iss, leeway, bad-signature)
  • v61 feature docs (EN + Zh) + toctree registration
  • What's-new sections in all three READMEs

Lint clean: ruff / pylint / bandit / radon (no function CC > 10, ≤5 args). Test keys built at runtime so secret scanners don't trip on literals.

RPA flows need to mint and verify bearer tokens for the APIs they drive,
but the framework only had HMAC file signing and an ACME-bound RS256 JWS.
Add a pure-stdlib HS256/384/512 JWT codec with full claim validation
grouped in a ClaimsPolicy (exp/nbf/aud/iss, injectable clock). Safe by
default: rejects alg=none, enforces an algorithm allowlist against
confusion attacks, and compares signatures with compare_digest. Wired
through the facade, AC_jwt_encode/AC_jwt_decode executor commands, MCP
tools and the Script Builder.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 61 complexity · 2 duplication

Metric Results
Complexity 61
Duplication 2

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@JE-Chen JE-Chen merged commit 7d27357 into dev Jun 20, 2026
16 checks passed
@JE-Chen JE-Chen deleted the feat/jwt branch June 20, 2026 21:03
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant