Overview
Theverify_grant_token() function verifies a Grantex grant token locally using the published JSON Web Key Set (JWKS). This approach has zero runtime dependency on Grantex infrastructure — the only network call is to fetch the JWKS from the issuer.
This is ideal for service-side verification where latency matters and you want to avoid an API round-trip for every request.
Usage
Import
Options
VerifyGrantTokenOptions configures how the token is verified:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
jwks_uri | str | Yes | — | URL of the JWKS endpoint (e.g. https://api.grantex.dev/.well-known/jwks.json). |
required_scopes | list[str] | None | No | None | If set, verification fails if the token is missing any of these scopes. |
clock_tolerance | int | No | 0 | Seconds of leeway for exp and iat clock skew. |
audience | str | None | No | None | Expected aud claim. If None, audience is not validated. |
Response
verify_grant_token() returns a VerifiedGrant frozen dataclass:
| Field | Type | Description |
|---|---|---|
token_id | str | The JWT jti claim (unique token identifier). |
grant_id | str | The grant identifier (grnt claim, falls back to jti). |
principal_id | str | The user/principal who authorized the grant (sub). |
agent_did | str | The agent’s DID (agt claim). |
developer_id | str | The developer who owns the agent (dev claim). |
scopes | tuple[str, ...] | The granted permission scopes. |
issued_at | int | Unix timestamp when the token was issued. |
expires_at | int | Unix timestamp when the token expires. |
parent_agent_did | str | None | Parent agent DID (delegation chains only). |
parent_grant_id | str | None | Parent grant ID (delegation chains only). |
delegation_depth | int | None | Delegation depth (0 = root grant). |
Algorithm
The algorithm is fixed to RS256 per SPEC section 11. Tokens signed with any other algorithm are rejected. This cannot be overridden.Examples
Basic Verification
Require Specific Scopes
With Audience and Clock Tolerance
Verifying Delegated Tokens
Delegated tokens include additional claims for the delegation chain:Error Handling
verify_grant_token() raises GrantexTokenError in the following cases:
- The token header uses an algorithm other than RS256
- The JWKS endpoint is unreachable or returns invalid data
- No matching RSA key is found in the JWKS
- The token signature is invalid
- The token is expired
- Required claims (
jti,sub,agt,dev,scp,iat,exp) are missing - Required scopes are not present in the token