gauth

package
v0.0.0-...-e027c79 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//TokenKey Redis 存储的hashMap,保存用户令牌
	TokenKey   = "/h/token"
	HeaderAuth = "Authorization"
	BearerVal  = "Bearer "
)

常量定义

Variables

View Source
var (
	ErrPlace   = errors.New("存在异地操作行为: 令牌失效")
	ErrInvalid = errors.New("非法的令牌: 令牌已失效")
)

错误定义

View Source
var DefOption = &Option{
	Debug:        true,
	Secret:       []byte("AYtY0RD5hvN2T0McxjNWfH1MM7PExE0w"),
	ExpirePeriod: time.Hour * 10,
	SingleLogin:  true,
	VerifyIP:     false,
}

DefOption 默认配置

View Source
var GAuth = newAuth()

GAuth 对外暴露,全局唯一对象

Functions

func AESDecrypt

func AESDecrypt(cipherkey, ciphertext []byte) ([]byte, error)

AESDecrypt decrypts a piece of data. The cipherkey argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func AESEncrypt

func AESEncrypt(cipherkey, src []byte) []byte

AESEncrypt encrypts a piece of data. The cipherkey argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func Md5

func Md5(b []byte) string

Md5 returns the MD5 checksum string of the data.

Types

type BindClaimsFunc

type BindClaimsFunc func(claim *Claims) (err error)

BindClaimsFunc 填充载荷

type Claims

type Claims struct {
	jwt.StandardClaims
	ClientIP string `json:"ip,omitempty"`
}

Claims jwt 载荷 请求体结构

type IAuth

type IAuth interface {
	SetOut(out io.Writer)
	SetStore(store IStore)
	SetOption(opt *Option)
	GenerateToken(req IRequester, fn BindClaimsFunc) (tk string, err error)
	Verify(req IRequester) (err error)
	Encrypt(str string) string
	Decrypt(str string) (rs string, err error)
}

IAuth 身份验证接口

type IRequester

type IRequester interface {
	ClientIP() string
	GetHeader(key string) string
	Header(key, value string)
	Set(string, interface{})
}

IRequester jwt验校时的入参对象接口

type IStore

type IStore interface {
	Set(key, field, tkID string) error
	//需要进行原子操作,保证一致性
	Verify(key, field, tkID string) error
}

IStore 接口

func DefaultJWTStore

func DefaultJWTStore() IStore

DefaultJWTStore 单点登录,默认存储

type Option

type Option struct {
	Debug bool
	//加密秘钥
	Secret []byte
	//过期时间
	//比如:过期时间为10小时
	//那么,在令牌签发后的前5个小时之内,不会再签发新的令牌
	//当令牌超过5小时,每次令牌验校成功之后,将会签发新的令牌
	ExpirePeriod time.Duration
	//是否是单点登录
	SingleLogin bool
	//是否验校IP
	VerifyIP bool
}

Option JWT配置

Jump to

Keyboard shortcuts

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