将model从pak包中移出到根目录
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
2022-04-05 18:16:20 +08:00
parent 795028c4bb
commit 513b563728
11 changed files with 54 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ package common
import (
"drive-linked/config"
"github.com/golang-jwt/jwt/v4"
"strings"
"time"
)
@@ -12,13 +13,28 @@ type JwtClaims struct {
}
//TODO:token解密验证
func ValidateLogin(token string) error {
return nil
func ValidateLogin(authorization string) error {
// 取出Bearer后的内容
var tokenString string
if auths := strings.Split(authorization, " "); len(auths) > 1 {
tokenString = auths[1]
} else {
return jwt.ErrInvalidKey
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte(config.Cfg.Security.Jwt.Secret), nil
})
if token != nil && token.Valid {
return nil
}
return err
}
func NewToken(auds ...string) (string, error) {
if len(auds) == 0 {
auds = []string{"nonAudience"}
auds = []string{"non-audience"}
}
// Create the claims
claims := JwtClaims{

View File

@@ -14,3 +14,15 @@ func TestNewToken(t *testing.T) {
}
t.Log(token)
}
func TestValidateLogin(t *testing.T) {
config.SetupConfig()
tokenString, _ := NewToken("eigeen")
err := ValidateLogin(tokenString)
if err == nil {
t.Log("验证通过")
} else {
t.Error(err)
}
}