将model从pak包中移出到根目录
continuous-integration/drone Build is passing
Details
continuous-integration/drone Build is passing
Details
This commit is contained in:
parent
795028c4bb
commit
513b563728
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func UserProfile(ctx iris.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var req dto.QueryUser
|
||||
var req dto.QueryUserParams
|
||||
err = json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
@ -31,7 +31,7 @@ func (r *Response) Success(data interface{}) {
|
|||
}
|
||||
|
||||
// 失败 统一处理
|
||||
func (r *Response) Error(code int32, msg string) {
|
||||
func (r *Response) Error(code int, msg string) {
|
||||
res := serializer.Response{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
|
@ -45,15 +45,11 @@ func (r *Response) Error(code int32, msg string) {
|
|||
}
|
||||
|
||||
// 参数类型错误
|
||||
func (r *Response) ErrInvalidParamType() (err error) {
|
||||
res := serializer.Response{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: "参数类型错误",
|
||||
Data: nil,
|
||||
}
|
||||
_, err = r.Ctx.JSON(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
func (r *Response) ErrBadRequest() {
|
||||
r.Error(http.StatusBadRequest, "请求参数错误")
|
||||
}
|
||||
|
||||
// 未登录/未授权错误
|
||||
func (r *Response) ErrUnauthorized() {
|
||||
r.Error(http.StatusUnauthorized, "未登录")
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package dto
|
||||
|
||||
import (
|
||||
"drive-linked/pkg/model"
|
||||
"drive-linked/model"
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
|
@ -15,7 +15,7 @@ type UserProfile struct {
|
|||
Roles string `json:"roles"`
|
||||
}
|
||||
|
||||
type QueryUser struct {
|
||||
type QueryUserParams struct {
|
||||
Method string `json:"method"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
|
|
@ -2,15 +2,22 @@ package middleware
|
|||
|
||||
import (
|
||||
"drive-linked/pkg/common"
|
||||
"drive-linked/pkg/dto"
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func SignRequired(ctx iris.Context) {
|
||||
auth := ctx.GetHeader("Authorization")
|
||||
//TODO:更详细的判断,包括请求格式是否正确
|
||||
|
||||
// 验证token
|
||||
err := common.ValidateLogin(auth)
|
||||
if err == nil {
|
||||
ctx.Values().Set("logged_in", true)
|
||||
ctx.Next()
|
||||
} else {
|
||||
ctx.Values().Set("logged_in", false)
|
||||
resp := dto.NewResponse(ctx)
|
||||
resp.ErrUnauthorized()
|
||||
}
|
||||
ctx.Next()
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package serializer
|
|||
import "net/http"
|
||||
|
||||
type Response struct {
|
||||
Code int32 `json:"code"`
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ package service
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"drive-linked/model"
|
||||
"drive-linked/pkg/dto"
|
||||
"drive-linked/pkg/model"
|
||||
"drive-linked/pkg/serializer"
|
||||
"github.com/kataras/iris/v12"
|
||||
"net/http"
|
||||
|
|
|
@ -17,6 +17,8 @@ func Router() *iris.Application {
|
|||
// v1
|
||||
v1 := app.Party("/v1")
|
||||
v1.PartyFunc("/users", func(users iris.Party) {
|
||||
// 需要登录
|
||||
users.Use(middleware.SignRequired)
|
||||
// 用户详细信息
|
||||
users.Get("/profile/{name:string}", controller.UserProfile)
|
||||
users.Post("/profile", controller.UserProfile)
|
||||
|
|
Loading…
Reference in New Issue