jwt

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenMalformed        = errors.New("token is malformed")
	ErrTokenUnverifiable     = errors.New("token is unverifiable")
	ErrTokenSignatureInvalid = errors.New("token signature is invalid")
	ErrSignatureInvalid      = errors.New("signature is invalid")
	ErrInvalidKeyType        = errors.New("key is of invalid type")
)

Functions

This section is empty.

Types

type Keyfunc

type Keyfunc func(*Token) (interface{}, error)

Keyfunc is used by Parse to supply the key for verification. The function receives the parsed but unverified Token (e.g. to read "alg" from header).

type MapClaims

type MapClaims map[string]interface{}

MapClaims is a claims type that uses map[string]interface{} for JSON decoding. Used as the default claims type for parsing and creating tokens.

type SigningMethod

type SigningMethod interface {
	Verify(signingString string, sig []byte, key interface{}) error
	Sign(signingString string, key interface{}) ([]byte, error)
	Alg() string
}

SigningMethod is used to sign and verify tokens.

var SigningMethodHS256 SigningMethod = signingMethodHS256

SigningMethodHS256 is the HMAC-SHA256 signing method.

type SigningMethodHMAC

type SigningMethodHMAC struct {
	Name string
}

SigningMethodHMAC implements HS256.

func (*SigningMethodHMAC) Alg

func (m *SigningMethodHMAC) Alg() string

func (*SigningMethodHMAC) Sign

func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) ([]byte, error)

func (*SigningMethodHMAC) Verify

func (m *SigningMethodHMAC) Verify(signingString string, sig []byte, key interface{}) error

type Token

type Token struct {
	Raw       string
	Method    SigningMethod
	Header    map[string]interface{}
	Claims    MapClaims
	Signature []byte
	Valid     bool
}

Token represents a JWT.

func NewWithClaims

func NewWithClaims(method SigningMethod, claims MapClaims) *Token

NewWithClaims creates a new Token with the given signing method and claims.

func Parse

func Parse(tokenString string, keyFunc Keyfunc) (*Token, error)

Parse parses and verifies the JWT and returns the token. Only HS256 signature verification is supported.

func (*Token) EncodeSegment

func (t *Token) EncodeSegment(seg []byte) string

EncodeSegment encodes bytes to base64url without padding.

func (*Token) SignedString

func (t *Token) SignedString(key interface{}) (string, error)

SignedString signs the token and returns the full JWT string.

func (*Token) SigningString

func (t *Token) SigningString() (string, error)

SigningString returns the base64url(header).base64url(claims) string.

Jump to

Keyboard shortcuts

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