auth

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenTypeAccess  = "access"
	TokenTypeRefresh = "refresh"
)

Variables

View Source
var (
	ErrMissingJwtToken        = errors.New("缺少JWT令牌")
	ErrTokenInvalid           = errors.New("令牌无效")
	ErrTokenExpired           = errors.New("JWT令牌已过期")
	ErrTokenParseFail         = errors.New("解析JWT令牌失败")
	ErrUnSupportSigningMethod = errors.New("不支持的签名方法")
	ErrInvalidTokenType       = errors.New("非访问令牌")
	ErrNilConfig              = errors.New("配置为空")
)

自定义错误

Functions

func FromContext added in v0.0.11

func FromContext(ctx context.Context) (token jwt.Claims, ok bool)

FromContext extract auth info from context

func GenerateSignature

func GenerateSignature(message string, secret string) string

GenerateSignature 生成签名

func GenerateToken added in v0.0.11

func GenerateToken(key string, opts ...Option) (string, error)

func GetCurrentUser added in v0.1.1

func GetCurrentUser(ctx context.Context) (string, error)

GetCurrentUser 获取当前用户名

func GetSigningMethod added in v0.1.1

func GetSigningMethod(method string) jwt.SigningMethod

GetSigningMethod 根据方法名返回JWT签名方法

func NewAuthContext added in v0.1.1

func NewAuthContext(ctx context.Context, claims AuthClaims) context.Context

NewAuthContext 将认证声明添加到上下文中

func NewContext added in v0.0.11

func NewContext(ctx context.Context, info jwt.Claims) context.Context

NewContext put auth info into context

func NewJwt added in v0.1.1

func NewJwt() (*jwtMiddleware, error)

NewJwt 创建新的jwtMiddleware实例

func ParseJwtToken added in v0.0.11

func ParseJwtToken(jwtToken string, opts ...Option) (jwt.Claims, error)

ParseJwtToken 解析和验证令牌

func VerifySignature

func VerifySignature(ak, sk, timeStr, sign string) bool

VerifySignature 验证签名

Types

type AuthClaims added in v0.1.1

type AuthClaims interface {
	jwt.Claims
	GetUsername() string
}

AuthClaims 定义认证声明的接口

func ClaimsFromContext added in v0.1.1

func ClaimsFromContext(ctx context.Context) (AuthClaims, bool)

ClaimsFromContext 从上下文中获取认证声明

type JWTMiddleware

type JWTMiddleware interface {
	Middleware(next http.Handler) http.Handler
	GenerateTokenPair(username string) (*TokenPair, error)
	RefreshToken(refreshToken string) (*TokenPair, error)
}

JWTMiddleware 接口定义

type JwtConfig

type JwtConfig struct {
	configs.TokenConfig
	SigningMethod jwt.SigningMethod
	ErrHandler    func(w http.ResponseWriter, r *http.Request, err error)
}

JwtConfig 定义JWT中间件的配置

type Option added in v0.0.11

type Option func(*options)

Option is jwt option.

func WithClaims added in v0.0.11

func WithClaims(f func() jwt.Claims) Option

func WithKeyFunc added in v0.0.11

func WithKeyFunc(f jwt.Keyfunc) Option

func WithSigningMethod added in v0.0.11

func WithSigningMethod(method jwt.SigningMethod) Option

type TokenClaims

type TokenClaims struct {
	Username  string `json:"username"`
	TokenType string `json:"token_type"` // "access" 或 "refresh"
	jwt.RegisteredClaims
}

TokenClaims 实现 AuthClaims 接口

func (*TokenClaims) GetUsername added in v0.1.1

func (tc *TokenClaims) GetUsername() string

GetUsername 返回用户名

type TokenPair added in v0.1.1

type TokenPair struct {
	AccessToken  string
	RefreshToken string
}

TokenPair 存储访问令牌和刷新令牌

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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