utils

package
v0.0.0-...-66c7d48 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AccessExpiresAt  = jwt.NewNumericDate(time.Now().Add(15 * time.Minute))
	RefreshExpiresAt = jwt.NewNumericDate(time.Now().Add(24 * time.Hour))
	IssuedAt         = jwt.NewNumericDate(time.Now())
	NotBefore        = jwt.NewNumericDate(time.Now())
	Secret           = os.Getenv("JWT_SECRET")
)

Functions

func ComparePassword

func ComparePassword(hashedPassword, plainPassword string) error

ComparePassword compares the password with the hash password

func ExtractToken

func ExtractToken(tokenString, subject string, claims *Claims) (*jwt.Token, error)

func GenerateLoginResponse

func GenerateLoginResponse(id, username string, accessLevel int) (*LoginResponse, *Token, error)

func GeneratePassword

func GeneratePassword(password string) (string, error)

creates and returns a hash of the give password string

func RandomAlphaNum

func RandomAlphaNum(length int) string

func RandomAlphapet

func RandomAlphapet(length int) string

func RandomInt

func RandomInt(min, max int64) int64

func ValidateToken

func ValidateToken(token *jwt.Token, claims *Claims, subject string) error

Types

type Claims

type Claims struct {
	Username    string
	ID          string
	AccessLevel int
	jwt.RegisteredClaims
}

type LoginResponse

type LoginResponse struct {
	Username     string `json:"username"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
}

type Token

type Token struct {
	AccessToken  *TokenDetail
	RefreshToken *TokenDetail
}

type TokenDetail

type TokenDetail struct {
	UserID      string
	Username    string
	AccessLevel int
	Token       *string
	TokenID     string
	ExpiresAt   time.Time
	Subject     string
}

func GenerateAccessToken

func GenerateAccessToken(id, username, tokenID string, accessLevel int) (*TokenDetail, error)

func GenerateRefreshToken

func GenerateRefreshToken(id, username, tokenID string, accessLevel int) (*TokenDetail, error)

func VerifyTokenWithClaims

func VerifyTokenWithClaims(tokenString, subject string) (*TokenDetail, error)

Jump to

Keyboard shortcuts

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