jwt

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EVMAddressClaim carries the authenticated EVM address.
	EVMAddressClaim = "evm_address"
	// CantonPartyClaim carries the user's Canton party id. It mirrors the standard
	// "sub" claim but is named explicitly so external verifiers (e.g. the indexer)
	// can read the party without relying on "sub" semantics.
	CantonPartyClaim = "canton_party_id"
)

Custom JWT claims carried alongside the registered claims.

Variables

This section is empty.

Functions

func ParseRSAPrivateKey

func ParseRSAPrivateKey(pemString string) (*rsa.PrivateKey, error)

ParseRSAPrivateKey decodes a PEM-encoded RSA private key in either PKCS#1 ("RSA PRIVATE KEY") or PKCS#8 ("PRIVATE KEY") form.

func RequireAuth

func RequireAuth(validator TokenValidator, audience string) func(http.Handler) http.Handler

RequireAuth returns middleware that rejects requests without a valid bearer token for the expected audience and populates the request context with the authenticated identity (EVM address + Canton party) from the token claims.

Types

type Claims

type Claims struct {
	EVMAddress    string `json:"evm_address"`
	CantonPartyID string `json:"canton_party_id"`
	gojwt.RegisteredClaims
}

Claims are the claims minted at login. The Canton party id is both the subject and an explicit canton_party_id claim; the EVM address rides alongside so handlers can resolve either identity from the token.

type Issuer

type Issuer struct {
	// contains filtered or unexported fields
}

Issuer mints RS256 JWTs for authenticated users.

func NewIssuer

func NewIssuer(key *rsa.PrivateKey, kid, issuer, audience string, ttl time.Duration) *Issuer

NewIssuer creates an Issuer that signs with key, advertising it under kid.

func (*Issuer) Issue

func (i *Issuer) Issue(evmAddress, cantonPartyID string) (string, time.Time, error)

Issue mints a signed token for the (evmAddress, cantonPartyID) identity and returns it alongside its expiry time.

func (*Issuer) JWKS

func (i *Issuer) JWKS() JWKS

JWKS returns the public signing key as a JSON Web Key Set for publication.

func (*Issuer) KeyID

func (i *Issuer) KeyID() string

KeyID returns the kid advertised for the signing key.

func (*Issuer) PublicKey

func (i *Issuer) PublicKey() *rsa.PublicKey

PublicKey returns the public half of the signing key, for JWKS publication and in-process validation.

type JWK

type JWK struct {
	Kid string `json:"kid"`
	Kty string `json:"kty"`
	Alg string `json:"alg"`
	Use string `json:"use"`
	N   string `json:"n"`
	E   string `json:"e"`
}

JWK is a single JSON Web Key (RSA public key) per RFC 7517.

type JWKS

type JWKS struct {
	Keys []JWK `json:"keys"`
}

JWKS is a JSON Web Key Set, the document served at /.well-known/jwks.json.

type SIWEVerifier

type SIWEVerifier struct {
	// contains filtered or unexported fields
}

SIWEVerifier validates EIP-4361 login messages against the server's expected domain, uri, and chain id, and recovers the signer. It does not track nonces: it returns the message's nonce so the caller can enforce single use against its own store, keeping this a stateless primitive.

func NewSIWEVerifier

func NewSIWEVerifier(domain, uri string, chainID int) *SIWEVerifier

NewSIWEVerifier binds a verifier to the expected domain/uri/chain.

func (*SIWEVerifier) Verify

func (v *SIWEVerifier) Verify(rawMessage, signature string) (addr common.Address, nonce string, err error)

Verify parses and validates a SIWE message and its signature. On success it returns the recovered checksummed EVM address and the message's nonce; the caller is responsible for enforcing nonce single use. It performs no state mutation, so a failed verification leaves any nonce untouched.

type TokenValidator

type TokenValidator interface {
	ValidateToken(token string) (gojwt.MapClaims, error)
}

TokenValidator verifies a bearer token and returns its claims. Satisfied by Validator.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator verifies RS256 JWTs. Keys come either from a single in-process public key (NewValidatorWithKey) or from a remote JWKS endpoint fetched on demand and cached by key id (NewValidator).

func NewValidator

func NewValidator(jwksURL, issuer string) *Validator

NewValidator creates a Validator that fetches signing keys from a JWKS endpoint. If the issuer is non-empty, tokens must carry a matching "iss" claim.

func NewValidatorWithKey

func NewValidatorWithKey(kid string, pub *rsa.PublicKey, issuer string) *Validator

NewValidatorWithKey creates a Validator that trusts a single in-process RSA public key, keyed by kid, and performs no network fetch. The token issuer's own process uses this to validate the tokens it mints; external services validate the same tokens via the published JWKS URL using NewValidator.

func (*Validator) IsConfigured

func (v *Validator) IsConfigured() bool

IsConfigured reports whether the validator can obtain keys (in-process or JWKS).

func (*Validator) ValidateToken

func (v *Validator) ValidateToken(tokenString string) (gojwt.MapClaims, error)

ValidateToken parses and verifies a token, returning its claims. It enforces the RS256 signing method, a known key id, standard time claims, and (when configured) the expected issuer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL