token

package
v2.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Index

Constants

View Source
const (
	JWTClaimsExp = "exp"
	JWTClaimsSub = "sub"
)

jwt claims RFC 7519 https://tools.ietf.org/html/rfc7519#section-4.1.2

Variables

View Source
var (
	ErrInvalidExp = errors.New("expire time is illegal")
)

token pkg common errors

Functions

func Sign

func Sign(claims map[string]interface{}, secret interface{}, opts ...Option) (string, error)

Sign gen token

func Verify

func Verify(tokenString string, f SecretFunc, opts ...Option) (map[string]interface{}, error)

Verify return claims

Types

type Manager

type Manager interface {
	Sign(claims map[string]interface{}, secret interface{}, option ...Option) (string, error)
	Verify(tokenString string, f SecretFunc, opts ...Option) (map[string]interface{}, error)
}

Manager manages token

var DefaultManager Manager = &jwtTokenManager{}

DefaultManager can be replaced

type Option

type Option func(options *Options)

Option is option

func WithExpTime

func WithExpTime(exp string) Option

WithExpTime generate a token which expire after a duration for example 5s,1m,24h

func WithSigningMethod

func WithSigningMethod(m SigningMethod) Option

WithSigningMethod specify the sign method

type Options

type Options struct {
	Expire        string
	SigningMethod SigningMethod
}

Options is options

type SecretFunc

type SecretFunc func(claims interface{}, method SigningMethod) (interface{}, error)

SecretFunc is a callback function to supply the key for verification. The function receives the parsed, but unverified claims in Token. This allows you to use properties in the claims of the token (such as `username`) to identify which key to use.

type SigningMethod

type SigningMethod int
const (
	RS256 SigningMethod = 1
	RS512 SigningMethod = 2
	HS256 SigningMethod = 3
)

const

Jump to

Keyboard shortcuts

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