tokeninjector

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(userID string, userName string, roleID uint64, expiredAt time.Time, secretKey []byte) (string, error)

Marshal creates a token string from the user id, user name, role id, and expiration time. The token string is encrypted with the secret key and encoded in base64.

func TokenHandler

func TokenHandler(
	secretKey []byte,
	cookieName string,
	contextBasicMethodKey string,
	contextBearerMethodKey string,
	nextFunc http.HandlerFunc,
) (http.HandlerFunc, error)

TokenHandler is a middleware that extracts the user ID from the request and adds it to the request context. The token is extracted from the cookie and the header.

  • secretKey: the secret key used to crypt and decrypt the token.
  • cookieName: the name of the cookie that contains the token.
  • contextBasicMethodKey: the key used to store the basic method token in the context.
  • contextBearerMethodKey: the key used to store the bearer method token in the context.
  • nextFunc: the next handler in the chain.

IMPORTANT: does not return an error if the user ID is not found.

func Unmarshal

func Unmarshal(data string, secretKey []byte) (userID string, userName string, roleID uint64, expiredAt time.Time, err error)

Unmarshal extracts the user id, user name, role id, and expiration time from the token string. The token string is decoded from base64 and decrypted with the secret key.

Types

type Token

type Token interface {
	UserID() string
	UserName() string
	UserRoleID() uint64
	ExpiredAt() time.Time
}

Token is an interface that contains the methods for getting the user id, user name, role id, and expiration time.

func ExtractToken

func ExtractToken(ctx context.Context) (Token, error)

ExtractToken extracts the token from the context. If the token is not found, an error is returned.

Jump to

Keyboard shortcuts

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