jwt

package
v1.16.28 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateLoginToken

func GenerateLoginToken(claims LoginRedisClaims) (string, error)

GenerateLoginToken 生成有效期 5 分钟的登录校验 Token

func GenerateMfaToken

func GenerateMfaToken(claims LoginRedisClaims, mfaSecret, appCallback string) (string, error)

GenerateMfaToken 生成绑定 TOTP MFA 中间身份令牌,五分钟有效

func GenerateToken

func GenerateToken(claims Claims) (string, error)

func ParseToken

func ParseToken[C Claims](Type, token string, target C) (claims C, valid bool, err error)

func ParseU2fToken

func ParseU2fToken(token, ip string) (bool, error)

Types

type Claims

type Claims interface {
	jwt.Claims
	GetType() string
}

type LoginRedisClaims

type LoginRedisClaims struct {
	UID       uint     `json:"uid"`
	AvatarUrl string   `json:"avatarUrl"`
	Name      string   `json:"name"`
	IP        string   `json:"ip"`
	Groups    []string `json:"groups"`

	AppID uint `json:"appID"`
}

func ParseLoginToken

func ParseLoginToken(token string) (*LoginRedisClaims, bool, error)

ParseLoginToken 解析后自动销毁

type LoginToken

type LoginToken struct {
	TypedClaims
	// 无意义 ID
	ID uint64 `json:"id"`
}

type MfaRedisClaims

type MfaRedisClaims struct {
	LoginRedisClaims
	Mfa         string `json:"mfa"`
	AppCallback string `json:"appCallback"`
}

func ParseMfaToken

func ParseMfaToken(token string) (*MfaRedisClaims, error)

ParseMfaToken 不会销毁,允许多次验证尝试

type MfaToken

type MfaToken struct {
	TypedClaims
	// 无意义 ID
	ID  uint64 `json:"id"`
	UID uint   `json:"uid"`
}

type TypedClaims

type TypedClaims struct {
	jwt.RegisteredClaims
	Type string `json:"type"`
}

TypedClaims type 字段用于区分不同类型的 token,防止类型窜用导致的安全漏洞

func NewTypedClaims

func NewTypedClaims(Type string, valid time.Duration) TypedClaims

func (*TypedClaims) GetType

func (a *TypedClaims) GetType() string

type U2fToken

type U2fToken struct {
	TypedClaims
	// 无意义 ID
	ID  uint64 `json:"id"`
	UID uint   `json:"uid"`
	IP  string `json:"ip"`
}

func GenerateU2fToken

func GenerateU2fToken(uid uint, ip string) (string, *U2fToken, error)

GenerateU2fToken 生成后台 U2F 身份令牌,五分钟有效

type UserToken

type UserToken struct {
	TypedClaims
	// dao.User.ID
	ID     uint     `json:"id"`
	Name   string   `json:"name"`
	Groups []string `json:"groups,omitempty"`
}

func GenerateUserToken

func GenerateUserToken(uid uint, name string, groups []string, valid time.Duration) (string, *UserToken, error)

GenerateUserToken 生成后台 Token

func ParseUserToken

func ParseUserToken(token string) (*UserToken, bool, error)

Jump to

Keyboard shortcuts

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