1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bind.go 3.22 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-12-16 12:25 . jwt优化,可以由外面生成
package api
import (
"gitee.com/h79/goutils/auth/token"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/stringutil"
"strings"
)
const (
KHead = "HEADER"
KToken = "TOKEN"
KUid = "UID"
)
type HeadOption func(ctx Context, header *Header)
func BindHead(ctx Context, option HeadOption) {
header := NewHeader(ReqHead{})
header.ReadFrom(ctx)
if option != nil {
option(ctx, header)
}
header.ClientIP = ctx.HttpContext().ClientIP()
ctx.CacheContext().Set(KHead, header)
}
func WithHeadOption(ctx Context, header *Header) {
header.Version = ctx.HttpContext().Query(CVersion)
header.Source = ctx.HttpContext().Query(CSource)
header.SeqId = ctx.HttpContext().Query(CSeqId)
header.ReqAt = stringutil.StringToInt64(ctx.HttpContext().Query(CTimeAt))
header.App.AppId = ctx.HttpContext().Query(CAppId)
header.App.Terminal = ctx.HttpContext().Query(CTerminal)
header.App.Version = header.Version
header.App.Source = header.Source
}
func BindClientAuth(ctx Context, source token.Source) error {
authenticate := ctx.AuthContext().Client().Auth()
flag := authenticate.EnableFlag(source)
if flag == 2 {
return result.RErrNotFound
}
if flag == 0 {
return result.RErrIgnore
}
logger.Debug("Api: client authentication start")
if err := authenticateCheck(ctx, source, authenticate); err != nil {
logger.Error("Api: client authentication failure, err= '%v'", err)
} else {
logger.Debug("Api: client authentication completed")
}
return nil
}
func BindServerAuth(ctx Context, source token.Source) error {
authenticate := ctx.AuthContext().Server().Auth()
flag := authenticate.EnableFlag(source)
if flag == 2 {
return result.RErrNotFound
}
if flag == 0 {
return result.RErrIgnore
}
logger.Debug("Api: server authentication start")
if err := authenticateCheck(ctx, source, authenticate); err != nil {
logger.Error("Api: server authentication failure, err= '%v'", err)
} else {
logger.Debug("Api: server authentication completed")
}
return nil
}
func GetTokenSource(ctx Context) token.Source {
base := GetBase(ctx)
return token.Source{AppId: base.App.AppId, Source: base.App.Source}
}
func authenticateCheck(ctx Context, source token.Source, authenticate token.Authenticate) error {
tok, err := tokenReq(ctx)
if err != nil {
authError(ctx, err)
return err
}
tk, err := authenticate.Check(source, tok)
if err != nil {
authError(ctx, err)
return err
}
ctx.CacheContext().Set(KUid, tk.GetVar("uid"))
ctx.CacheContext().Set(KToken, tk)
return nil
}
func authError(ctx Context, err error) {
SendStatusNoHead(ctx, DefErrorHttpCode, result.Error(result.ErrAuth, err.Error()), nil)
ctx.HttpContext().Abort()
}
const kToken = "token"
func tokenReq(ctx Context) (string, error) {
var header = ctx.HeaderContext()
tok := header.Get(CToken)
if len(tok) > 6 {
if strings.ToUpper(tok[0:7]) == "BEARER " {
return tok[7:], nil
}
return tok, nil
}
tok = header.Get(kToken)
if len(tok) > 0 {
return tok, nil
}
tok = ctx.HttpContext().Query(kToken)
if len(tok) > 0 {
return tok, nil
}
return "", result.RErrAuth
}
func Bearer(tok string) string {
if len(tok) <= 0 {
return ""
}
if strings.ToUpper(tok[0:7]) == "BEARER " {
return tok
}
return "Bearer " + tok
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.3.56

搜索帮助