auth

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 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 根据签名方法名获取对应的SigningMethod

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 创建JWT中间件实例

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 定义JWT Claims接口

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 定义JWT中间件接口

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 {
	ID        string `json:"id"`
	Username  string `json:"username"`
	TokenType string `json:"token_type"`
	Data      interface{}
	jwt.RegisteredClaims
}

TokenClaims 实现AuthClaims接口

func (*TokenClaims) GetUsername added in v0.1.1

func (tc *TokenClaims) GetUsername() string

GetUsername 获取Claims中的用户名

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