mjwt

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: LGPL-3.0 Imports: 10 Imported by: 9

README

MJWT

A simple wrapper for JWT. Contains an AccessToken and RefreshToken model.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClaimTypeMismatch = errors.New("claim type mismatch")

Functions

This section is empty.

Types

type BaseTypeClaims

type BaseTypeClaims[T Claims] struct {
	jwt.RegisteredClaims
	ClaimType string
	Claims    T
}

BaseTypeClaims is a wrapper for combining the jwt.RegisteredClaims with a ClaimType and generic Claims data

func ExtractClaims

func ExtractClaims[T Claims](p Verifier, token string) (*jwt.Token, BaseTypeClaims[T], error)

ExtractClaims uses a Verifier to validate the MJWT token and returns the parsed token and BaseTypeClaims

func (*BaseTypeClaims[T]) InternalClaimType

func (b *BaseTypeClaims[T]) InternalClaimType() string

InternalClaimType returns the Type of the generic claim struct

func (*BaseTypeClaims[T]) MarshalJSON

func (b *BaseTypeClaims[T]) MarshalJSON() ([]byte, error)

MarshalJSON converts the internalBaseTypeClaims and generic claim struct into a serialized JSON byte array

func (*BaseTypeClaims[T]) UnmarshalJSON

func (b *BaseTypeClaims[T]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON reads the internalBaseTypeClaims and generic claim struct from a serialized JSON byte array

func (*BaseTypeClaims[T]) Valid

func (b *BaseTypeClaims[T]) Valid() error

Valid checks the InternalClaimType matches and the type claim type

type Claims

type Claims interface {
	jwt.Claims
	Type() string
}

Claims is a wrapper for jwt.Claims and adds a Type method to name internal claim structs

type Signer

type Signer interface {
	Verifier
	GenerateJwt(sub, id string, aud jwt.ClaimStrings, dur time.Duration, claims Claims) (string, error)
	SignJwt(claims jwt.Claims) (string, error)
	Issuer() string
	PrivateKey() *rsa.PrivateKey
}

Signer is used to generate MJWT tokens. Signer can also be used as a Verifier.

func NewMJwtSigner

func NewMJwtSigner(issuer string, key *rsa.PrivateKey) Signer

NewMJwtSigner creates a new defaultMJwtSigner using the issuer name and rsa.PrivateKey

func NewMJwtSignerFromFile

func NewMJwtSignerFromFile(issuer, file string) (Signer, error)

NewMJwtSignerFromFile creates a new defaultMJwtSigner using the path of a rsa.PrivateKey file.

func NewMJwtSignerFromFileOrCreate

func NewMJwtSignerFromFileOrCreate(issuer, file string, random io.Reader, bits int) (Signer, error)

NewMJwtSignerFromFileOrCreate creates a new defaultMJwtSigner using the path of a rsa.PrivateKey file. If the file does not exist then the file is created and a new private key is generated.

type Verifier

type Verifier interface {
	VerifyJwt(token string, claims baseTypeClaim) (*jwt.Token, error)
	PublicKey() *rsa.PublicKey
}

Verifier is used to verify the validity MJWT tokens and extract the claim values.

func NewMJwtVerifier

func NewMJwtVerifier(key *rsa.PublicKey) Verifier

NewMJwtVerifier creates a new defaultMJwtVerifier using the rsa.PublicKey

func NewMJwtVerifierFromFile

func NewMJwtVerifierFromFile(file string) (Verifier, error)

NewMJwtVerifierFromFile creates a new defaultMJwtVerifier using the path of a rsa.PublicKey file

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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