create_jwt (sdk/jwt_helper.py)

AP2 · code/sdk/python/ap2/sdk/jwt_helper.py · python · Apache-2.0
auth

Provides utility functions to sign and verify JSON Web Tokens using ES256 signatures and JWK keys for secure authentication.

# Source: https://github.com/google-agentic-commerce/AP2 | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
# Provided via justclaw.it.com; keep this notice with the code.
"""Helper functions for creating and verifying JWTs."""

import json

from typing import Any

from jwcrypto.jwk import JWK
from jwcrypto.jws import JWS


def create_jwt(
    header: dict[str, Any],
    payload: dict[str, Any],
    private_key: JWK,
) -> str:
    """Create a compact JWS (ES256).

    Args:
      header: The JWT header.
      payload: The JWT payload.
      private_key: The JWK private key to sign the JWT with.

    Returns:
      The compact JWS string.
    """
    jws = JWS(json.dumps(payload).encode('utf-8'))
    jws.add_signature(private_key, alg='ES256', prot

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key