jwt

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound represents can not find the value with the key
	ErrNotFound = errors.New("not found")

	// ErrUnauthorized represents the current user doesn't have the right to do this operation
	ErrUnauthorized = errors.New("unauthorized")

	// ErrTokenExpired represents the token is expired
	ErrTokenExpired = errors.New("token expired")

	// ErrIllegalParameter represents the input parameter(s) is illegal
	ErrIllegalParameter = errors.New("illegal parameter")

	// ErrInternalServerError represents the server do sth wrong
	ErrInternalServerError = errors.New("internal server error")
)

Functions

func AuthKeyIDFromContext

func AuthKeyIDFromContext(ctx context.Context) uint64

AuthKeyIDFromContext returns the `auth key's id` previously associated with `ctx`,or `0` if no such `key id` could be found.

func ContextWithAuthKeyID

func ContextWithAuthKeyID(ctx context.Context, keyID uint64) context.Context

ContextWithAuthKeyID returns a new `context.Context` that holds a auth key's id

func ContextWithMachineID

func ContextWithMachineID(ctx context.Context, machineID string) context.Context

ContextWithMachineID returns a new `context.Context` that holds a machine id

func ContextWithUserID

func ContextWithUserID(ctx context.Context, userID uint64) context.Context

ContextWithUserID returns a new `context.Context` that holds a uid

func ExtractToken

func ExtractToken(raw string) (string, string, error)

ExtractToken extract the token from the raw string

func ExtractTokenFromRequest

func ExtractTokenFromRequest(r *http.Request) (string, string, error)

ExtractTokenFromRequest extract the token from http's Authorization header

func Initialize

func Initialize(accessSecret, refreshSecret string, opts ...Option) error

Initialize initialize the jwt

func MachineIDFromContext

func MachineIDFromContext(ctx context.Context) string

MachineIDFromContext returns the `machine id` previously associated with `ctx`, or `empty string if no such `machine id` could be found.

func NewLedisStore

func NewLedisStore()

func Shared

func Shared() *jwt

Shared represents the global jwt object

func UserIDFromContext

func UserIDFromContext(ctx context.Context) uint64

UserIDFromContext returns the `uid` previously associated with `ctx`, or `0` if no such `uid` could be found.

func Valid

func Valid(tokenStr string) (*stdjwt.Token, error)

Valid valid and convert the token string to a object

func VerifyToken

func VerifyToken(token string, accessSecret string) (*stdjwt.Token, error)

VerifyToken parse, validate, and return a token

Types

type AccessDetails

type AccessDetails struct {
	UserID     uint64
	AccessUUID string
	MachineID  string
	AuthKeyID  uint64
}

AccessDetails represents a helper object for manage jwt token

type Option

type Option func(*options)

Option represents a handler for adjust the default option(s)

func AccessTokenTTL

func AccessTokenTTL(seconds uint32) Option

AccessTokenTTL adjust the access token's ttl in seconds

func RefreshTokenTTL

func RefreshTokenTTL(seconds uint32) Option

RefreshTokenTTL adjust the refresh token's ttl in seconds

func WithStore

func WithStore(s Store) Option

WithStore adjust the default store for jwt token

type Store

type Store interface {
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	Del(ctx context.Context, keys ...string) error
	Get(ctx context.Context, key string) (interface{}, error)
}

Store represents something for store the jwt token

type TokenDetails

type TokenDetails struct {
	AccessToken   string
	RefreshToken  string
	AccessUUID    string
	RefreshUUID   string
	AccessExpiry  int64
	RefreshExpiry int64
}

TokenDetails represents a full object about the jwt token

Jump to

Keyboard shortcuts

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