lestrrat-go/jwx

repository·develop/v4·Indexed 25 days ago

https://github.com/lestrrat-go/jwx

A comprehensive Go module implementing the full spectrum of JOSE (JSON Object Signing and Encryption) technologies, including JWA, JWE, JWK, JWS, and JWT. It provides a library for Go applications and a companion `jwx` command line tool for generating JWKs, signing and verifying JWS messages, and encrypting and decrypting JWE payloads.

Tokens
81.1K
Snippets
139
Records
250
Agent score
80%

What's inside lestrrat-go/jwx

  1. Use JWA for JSON Web Algorithms

    develop/v4
    The github.com/lestrrat-go/jwx/v4/jwa package provides implementations for the various algorithms defined in RFC 7518. Use this package when you need to specify or work with specific cryptographic algorithms for JSON Web Signature (JWS), JSON Web Encryption (JWE), or other JWK-related operations.
  2. Use the jwt package for JSON Web Tokens

    develop/v4

    The jwt package provides an implementation of JSON Web Tokens (JWT) compliant with RFC 7519. It is designed for creating, parsing, and validating JWTs in Go applications.

    For detailed how-to guides and practical code examples, refer to the Working with JWT documentation.

    If you require OpenID Connect (OIDC) support, use the jwt/openid subpackage.

  3. Explore JOSE operations with jwx/v4

    develop/v4

    The github.com/lestrrat-go/jwx/v4 library provides tools for performing Javascript Object Signing and Encryption (JOSE) operations in Go. You can find detailed code examples and guides for specific JOSE components in the documentation:

    • JWT: Working with JSON Web Tokens.
    • JWS: Working with JSON Web Signatures.
    • JWE: Working with JSON Web Encryption.
    • JWK: Working with JSON Web Keys.
    • Extensions: Using extension modules.
    • Global Settings: Configuring global library settings.
    • Framework Integration: Integrating the library with existing frameworks.
  4. Use cloud KMS services for signing and verification

    develop/v4
    To use cloud KMS services (like AWS KMS) with this library, provide an object that implements the standard Go crypto.Signer interface. Any implementation of crypto.Signer is compatible with the jws package.
  5. How composite signatures integrate with jwx v4

    develop/v4

    The compsig module is an additive companion module that plugs into the jwx v4 ecosystem via several registration points. This allows standard jws and jwk operations to support composite algorithms seamlessly.

    Integration Points

    • Algorithm Discovery: Registers algorithms via jwa.RegisterSignatureAlgorithm() so they are found by jwa.LookupSignatureAlgorithm.
    • Key Association: Uses jws.RegisterAlgorithmForKeyType(AKP, alg) so that jwk lookups for the AKP type surface the composite variants.
    • Signing/Verification Logic: Implements dsig.Custom via compSigDsig and maps them to jwsbb using jwsbb.RegisterDsigAlgorithm.
    • JWK Lifecycle: Provides jwk.RegisterKeyExporter and jwk.RegisterKeyImporter to handle conversions between raw *compsig.PrivateKey/PublicKey types and JWK format.
    • High-level API: Provides jws.RegisterSigner and jws.RegisterVerifier wrappers that handle JWK unwrapping and dispatch to the jwsbb engine.
  6. Handle convenience accessor changes in v3 and v4

    develop/v4

    In versions v3 and later, most convenience accessors (e.g., (jwt.Token).Subject, (jws.Signed).Algorithm, or (jwk.Key).Algorithm) have changed their return type. They no longer return the value directly; instead, they return a tuple of (T, bool) to indicate if the value was present.

    If you require a single return value without the boolean check, use the Get(dst) method instead.

    Note: jwk.Key.KeyType is an exception and continues to always return a valid value.

  7. Register a custom JWK key type

    develop/v4

    The library allows extending support for new key types (e.g., vendor-specific algorithms) through four experimental registration points. Note that this extension surface is marked experimental and may undergo backward-incompatible changes.

    To fully support a new key type, you may need to implement one or more of the following:

    1. KeyProbe: Performs partial JSON unmarshaling to extract hint fields used to decide which concrete key type to construct. Use jwk.RegisterProbeField[T] to add new hint fields.
    2. KeyParser: Converts a JSON payload into a jwk.Key using the hints gathered by the probe. Register using jwk.RegisterKeyParser.
    3. KeyImporter[T]: Converts a raw Go crypto value into a jwk.Key. Register using jwk.RegisterKeyImporter.
    4. KeyExporter: Converts a jwk.Key back into a raw Go value. Register using jwk.RegisterKeyExporter, keyed by jwk.KeyKind (case-insensitive).
  8. Access JWS headers

    develop/v4

    A JWT is a payload contained within a JWS envelope. Because JWS headers are part of the envelope and not the payload, they are not accessible via the jwt.Token object.

    To access JWS headers (for example, to determine which key to use for verification), you must use the tools provided in the jws package. If you need to decide on a key based on header fields, consider using a jwt.KeyProvider.

  9. Work with JSON Web Encryption (JWE)

    develop/v4

    JWE (RFC7516) is used to encrypt arbitrary payloads and is implemented in the github.com/lestrrat-go/jwx/v4/jwe package.

    Like JWS, JWE supports two formats:

    1. A format consisting of 5 base64 encoded strings concatenated by a single period (.).
    2. A JSON object format.

    Example of a JWE message:

    eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTE5MkdDTSIsImVwayI6eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6IndMckhLNnBTLXZzdmhQZUNfNTN0ZWpxYzZIZUFsMllRWDRmY1hPNGV1bmciLCJ5IjoiV2V3bFdKazJ4QWJYSXE3WFJ6aVlZa2lxMjJfOF9TQ0VsbTA1Vm1iUGhFWSJ9fQ..7UTcbVpz-Ed1Q0wq.sneVfeTeAvzZNSMGpQ.JNo1BbDaKB-Q1mWaBNmdow
  10. Understand the difference between JWT verification and validation

    develop/v4

    In the context of github.com/lestrrat-go/jwx/v4/jwt, it is important to distinguish between these two processes:

    • Verification: The process of ensuring the integrity of the JWT by checking its signature (e.g., using JWS verification).
    • Validation: The process of checking the contents of the JWT, such as verifying if claims like iss (issuer), sub (subject), or aud (audience) match expected values, and checking if the token has expired.

    Note that jwt.Parse() performs signature verification but does not perform validation by default. To perform validation, you must explicitly call Validate() on the resulting token.

  11. Understand the genjwt (JWT Token Generator) logic

    develop/v4

    The genjwt tool is a code generator used to create JWT (JSON Web Token) related Go code. It generates interfaces, structs, constructors, getters, and methods like Has, Get, Set, Remove, UnmarshalJSON, MarshalJSON, Keys, and a Claims iterator.

    Key technical characteristics of the generated code include:

    • Multi-package support: It can target both jwt and openid packages. If targeting openid, it prepends jwt. to cross-package type references.
    • Specialized Marshaling: It handles specific JWT field types with custom logic:
      • audience: Uses json.MarshalAudience with flattening options.
      • types.NumericDate: Converted to a Unix timestamp via .Unix().
      • []byte: Encoded as a Base64 string.
      • Other types: Standard JSON marshaling.
    • Claims Iteration: Uses Go 1.22+ iter.Seq2 for the Claims() method.
  12. How extension modules work in v4

    develop/v4

    In v4, optional features (like specific signature algorithms or encryption schemes) are provided as standalone modules under the github.com/jwx-go prefix.

    To use an extension, you simply import the module for its side effects. Each module uses an init() function to automatically register its algorithms, key importers, and exporters with the core jwx library.

    Warning: Extension modules will panic during init() if registration fails. This is a design choice to ensure that if an extension is imported but cannot be correctly registered, the application fails immediately at startup rather than failing later during a cryptographic operation.