gitee.com/h79/goutils@v1.22.10/auth/token/auth.go (about)

     1  package token
     2  
     3  const (
     4  	CSource    = "source"
     5  	CSession   = "session"
     6  	CExtend    = "extend"
     7  	CUid       = "uid"
     8  	CBase64    = "base64"
     9  	CCustom    = "custom"
    10  	CIssuedAt  = "issue_at"
    11  	CExpiresAt = "expire_at"
    12  )
    13  
    14  type Token interface {
    15  	// WithVar key= CSource value= token.Source
    16  	//         key= CSession value= string
    17  	//         key= CExtend value= string
    18  	//         key= CUid value= string
    19  	//         key= CCustom value = interface 自定义数据
    20  	//         key= CIssuedAt value= int64 秒
    21  	//         key= CExpiresAt value= int64 秒
    22  	WithVar(key string, value interface{}) Token
    23  	GetVar(key string) interface{}
    24  	GetToken() string
    25  	Update() (string, error)
    26  	// ExpireIn 根据配置,产生一个过期时间
    27  	ExpireIn() int64
    28  }
    29  
    30  type Factory interface {
    31  	Create(key Key, opts ...Option) (Token, error)
    32  	Decode(tk string, opts ...Option) (Token, error)
    33  	Check(tk string, key Key, opts ...Option) (Token, error)
    34  }
    35  
    36  type Authenticate interface {
    37  	Type() string
    38  	// Create secretKey不是那个密钥,而是 Secret interface 的key,通过这个key值,获取到真实的密钥
    39  	Create(secretKey string, expireSeconds int64, opts ...Option) (Token, error)
    40  	// Check secretKey不是那个密钥,而是 Secret interface 的key,通过这个key值,获取到真实的密钥
    41  	Check(secretKey string, tk string, opts ...Option) (Token, error)
    42  	// Decode not need sign, only parse
    43  	Decode(tk string, opts ...Option) (Token, error)
    44  	// EnableFlag return 0= disable, 1=enable, 2=not exist
    45  	// secretKey不是那个密钥,而是 Secret interface 的key, 通过这个key值,获取到真实的密钥
    46  	EnableFlag(secretKey string) int
    47  	// SetEnabled secretKey不是那个密钥,而是 Secret interface 的key, 通过这个key值,获取到真实的密钥
    48  	SetEnabled(secretKey string, enable bool)
    49  }
    50  
    51  type Route interface {
    52  	Use(a Authenticate) Route
    53  	HasAuth(method string) bool
    54  	Auth(method string) (Authenticate, error)
    55  }
    56  
    57  type Engine interface {
    58  	Client() Route
    59  	Server() Route
    60  }
    61  
    62  type Option struct {
    63  	Base64 bool //是否使用base64编码
    64  }
    65  
    66  func IsBase64Enabled(opts ...Option) bool {
    67  	if len(opts) == 0 {
    68  		// 默认base64
    69  		return true
    70  	}
    71  	for i := range opts {
    72  		if opts[i].Base64 {
    73  			return true
    74  		}
    75  	}
    76  	return false
    77  }