jwtToken

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Copyright (c) 2020 Steven Victor https://github.com/tienbm90/simple-jwt-auth/blob/master/LICENSE

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("token is invalid")
	ErrExpiredToken = errors.New("token has expired")
)

Functions

func ExtractToken

func ExtractToken(r *http.Request) string

get the token from the request body It takes a request object and returns the token string

func TokenValid

func TokenValid(r *http.Request, secretKey string) error

It takes a request and a secret key, and returns an error if the token is invalid

func VerifyToken

func VerifyToken(r *http.Request, secretKey string) (*jwt.Token, error)

Types

type AccessDetails

type AccessDetails struct {
	TokenUuid string
	UserId    string
	UserName  string
}

func Extract

func Extract(token *jwt.Token) (*AccessDetails, error)

Extract 抽出

func ExtractTokenMetadata

func ExtractTokenMetadata(r *http.Request, secretKey string) (*AccessDetails, error)

ExtractTokenMetadata トークンのメタデータを抽出

type JWTMaker

type JWTMaker struct {
	// contains filtered or unexported fields
}

JWTMaker is a JSON Web Token maker

func (*JWTMaker) CreateToken

func (maker *JWTMaker) CreateToken(username string, duration time.Duration) (string, error)

CreateToken creates a new token for a specific username and duration

func (*JWTMaker) VerifyToken

func (maker *JWTMaker) VerifyToken(token string) (*Payload, error)

VerifyToken checks if the token is valid or not

type Maker

type Maker interface {
	CreateToken(username string, duration time.Duration) (string, error)
	VerifyToken(token string) (*Payload, error)
}

func NewJWTMaker

func NewJWTMaker(secretKey string) (Maker, error)

NewJWTMaker creates a new JWTMaker

type Payload

type Payload struct {
	ID        uuid.UUID `json:"id"`
	Username  string    `json:"username"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

func NewPayload

func NewPayload(username string, duration time.Duration) (*Payload, error)

func (*Payload) Valid

func (payload *Payload) Valid() error

type TokenDetails

type TokenDetails struct {
	AccessToken  string
	RefreshToken string
	TokenUuid    string
	RefreshUuid  string
	AtExpires    int64
	RtExpires    int64
}

func CreateToken

func CreateToken(userID, userName, secretKey, refreshKey string) (*TokenDetails, error)

CreateToken() creates a new token and refresh token, and returns the tokens generated

func NewTokenDetails

func NewTokenDetails() *TokenDetails

Jump to

Keyboard shortcuts

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