jwt

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package jwt provides structures and functions to build/parse JWT, read/parse public and private keys.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlgInvalid = errors.New("algorithm is invalid")
	ErrAlgNone    = errors.New("algorithm is none")
)
View Source
var ErrClaimsInvalid = errors.New("claims is invalid")

Functions

func GetSigningMethod

func GetSigningMethod(alg string) (jwt.SigningMethod, error)

GetSigningMethod gets jwt.SigningMethod by alg. It returns jwt.SigningMethod or nil if alg is invalid or "none".

func NewBuilder

func NewBuilder(params Params) (*builder, error)

NewBuilder gets signing method by params and creates new builder. It returns pointer to a builder instance or nil if params.Algorithm is incorrect.

func NewParser

func NewParser(params Params) (*parser, error)

NewParser gets signing method by params and creates new parser. It returns pointer to a parser instance or nil if params.Algorithm is incorrect.

func ParsePrivateKey

func ParsePrivateKey(data []byte, alg string) (any, error)

ParsePrivateKey creates keyParser and parses private key. It returns private key or nil if parsing failed.

func ParsePublicKey

func ParsePublicKey(data []byte, alg string) (any, error)

ParsePublicKey creates keyParser and parses public key. It returns public key or nil if parsing failed.

func ReadPrivateKey

func ReadPrivateKey(path string, alg string) (any, error)

ReadPrivateKey reads private key and parses it. It returns private key or nil if reading/parsing failed.

func ReadPublicKey

func ReadPublicKey(path string, alg string) (any, error)

ReadPublicKey reads public key and parses it. It returns public key or nil if reading/parsing failed

Types

type Builder

type Builder interface {
	// Build creates new Claims, JWT string and signing it.
	// It returns JWT string or empty string if signing failed.
	Build(subject string, age time.Duration, fingerprint string, roles []string) (string, error)
}

Builder is interface implemented by types that can build JWT.

type Claims

type Claims struct {
	Fingerprint string   `json:"fingerprint"`
	Roles       []string `json:"roles"`
	jwt.RegisteredClaims
}

Claims represents custom claims. The jwt.RegisteredClaims embedded in it.

type Params

type Params struct {
	Issuer    string
	Algorithm string
	Key       any
}

Params represents params to parse/build JWT.

type Parser

type Parser interface {
	// Parse parses JWT string.
	// It returns pointer to a Claims or nil if parsing failed.
	Parse(tokenString string) (*Claims, error)
}

Parser is interface implemented by types that can parse JWT.

Jump to

Keyboard shortcuts

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