1234567891011121314151617181920212223242526272829303132333435 |
- package headutil
- import (
- "Cold_Logistic/internal/pkg/common/constant"
- "encoding/base64"
- "strings"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/sliceutil"
- "github.com/gin-gonic/gin"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
- )
- // ParseAuthHead 解析请求头中Authorization,返回tokenType, Token
- func ParseAuthHead(c *gin.Context) (tokenType, token string, e error) {
- authHeader := strings.SplitN(c.Request.Header.Get(constant.Authorization), " ", 2)
- if len(authHeader) != 2 || authHeader[1] == "" || !sliceutil.ContainStr([]string{constant.AuthTokenTypeBasic, constant.AuthTokenTypeBearer}, authHeader[0]) {
- return "", "", errors.New("Authorization header format is wrong")
- }
- return authHeader[0], authHeader[1], nil
- }
- // ParseBasicToken 解析BasicToken,返回账号, 密码
- func ParseBasicToken(token string) (username, password string, err error) {
- payload, err := base64.StdEncoding.DecodeString(token)
- if err != nil {
- return "", "", err
- }
- pair := strings.SplitN(string(payload), ":", 2)
- if len(pair) != 2 {
- return "", "", errors.New("Parse Basic Payload Error")
- }
- return pair[0], pair[1], nil
- }
|