jwt

package
v1.17.3 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	User    = "User"
	Login   = "Login"
	Mfa     = "Mfa"
	U2F     = "U2F"
	Refresh = "Refresh"
	Access  = "Access"
)

Variables

This section is empty.

Functions

func GenerateAccessToken added in v1.17.2

func GenerateAccessToken(uid uint, appCode, payload string) (string, error)

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 GenerateRefreshToken added in v1.17.2

func GenerateRefreshToken(uid uint, appCode, payload string, valid time.Duration) (string, error)

func GenerateToken

func GenerateToken(claims Claims) (string, error)

func HeaderToken added in v1.17.3

func HeaderToken(c *gin.Context, Type string) (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)

func TokenWithType added in v1.17.3

func TokenWithType(Type, token string) string

Types

type AccessToken added in v1.17.2

type AccessToken struct {
	RefreshToken
}

func ParseAccessToken added in v1.17.2

func ParseAccessToken(token string) (*AccessToken, bool, error)

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 RefreshToken added in v1.17.2

type RefreshToken struct {
	TypedClaims
	UID     uint   `json:"uid"`
	AppCode string `json:"appCode"`
	Payload string `json:"payload,omitempty"`
}

func ParseRefreshToken added in v1.17.2

func ParseRefreshToken(token string) (*RefreshToken, bool, error)

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