将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 (
|
import (
|
||||||
"drive-linked/config"
|
"drive-linked/config"
|
||||||
"github.com/golang-jwt/jwt/v4"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,13 +13,28 @@ type JwtClaims struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO:token解密验证
|
//TODO:token解密验证
|
||||||
func ValidateLogin(token string) error {
|
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 nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewToken(auds ...string) (string, error) {
|
func NewToken(auds ...string) (string, error) {
|
||||||
if len(auds) == 0 {
|
if len(auds) == 0 {
|
||||||
auds = []string{"nonAudience"}
|
auds = []string{"non-audience"}
|
||||||
}
|
}
|
||||||
// Create the claims
|
// Create the claims
|
||||||
claims := JwtClaims{
|
claims := JwtClaims{
|
||||||
|
|
|
@ -14,3 +14,15 @@ func TestNewToken(t *testing.T) {
|
||||||
}
|
}
|
||||||
t.Log(token)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req dto.QueryUser
|
var req dto.QueryUserParams
|
||||||
err = json.Unmarshal(body, &req)
|
err = json.Unmarshal(body, &req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
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{
|
res := serializer.Response{
|
||||||
Code: code,
|
Code: code,
|
||||||
Msg: msg,
|
Msg: msg,
|
||||||
|
@ -45,15 +45,11 @@ func (r *Response) Error(code int32, msg string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 参数类型错误
|
// 参数类型错误
|
||||||
func (r *Response) ErrInvalidParamType() (err error) {
|
func (r *Response) ErrBadRequest() {
|
||||||
res := serializer.Response{
|
r.Error(http.StatusBadRequest, "请求参数错误")
|
||||||
Code: http.StatusBadRequest,
|
}
|
||||||
Msg: "参数类型错误",
|
|
||||||
Data: nil,
|
// 未登录/未授权错误
|
||||||
}
|
func (r *Response) ErrUnauthorized() {
|
||||||
_, err = r.Ctx.JSON(res)
|
r.Error(http.StatusUnauthorized, "未登录")
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"drive-linked/pkg/model"
|
"drive-linked/model"
|
||||||
"github.com/jinzhu/copier"
|
"github.com/jinzhu/copier"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ type UserProfile struct {
|
||||||
Roles string `json:"roles"`
|
Roles string `json:"roles"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueryUser struct {
|
type QueryUserParams struct {
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,22 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"drive-linked/pkg/common"
|
"drive-linked/pkg/common"
|
||||||
|
"drive-linked/pkg/dto"
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SignRequired(ctx iris.Context) {
|
func SignRequired(ctx iris.Context) {
|
||||||
auth := ctx.GetHeader("Authorization")
|
auth := ctx.GetHeader("Authorization")
|
||||||
//TODO:更详细的判断,包括请求格式是否正确
|
//TODO:更详细的判断,包括请求格式是否正确
|
||||||
|
|
||||||
|
// 验证token
|
||||||
err := common.ValidateLogin(auth)
|
err := common.ValidateLogin(auth)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ctx.Values().Set("logged_in", true)
|
ctx.Values().Set("logged_in", true)
|
||||||
}
|
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
|
} else {
|
||||||
|
ctx.Values().Set("logged_in", false)
|
||||||
|
resp := dto.NewResponse(ctx)
|
||||||
|
resp.ErrUnauthorized()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package serializer
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Code int32 `json:"code"`
|
Code int `json:"code"`
|
||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
Data interface{} `json:"data"`
|
Data interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"drive-linked/model"
|
||||||
"drive-linked/pkg/dto"
|
"drive-linked/pkg/dto"
|
||||||
"drive-linked/pkg/model"
|
|
||||||
"drive-linked/pkg/serializer"
|
"drive-linked/pkg/serializer"
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
|
@ -17,6 +17,8 @@ func Router() *iris.Application {
|
||||||
// v1
|
// v1
|
||||||
v1 := app.Party("/v1")
|
v1 := app.Party("/v1")
|
||||||
v1.PartyFunc("/users", func(users iris.Party) {
|
v1.PartyFunc("/users", func(users iris.Party) {
|
||||||
|
// 需要登录
|
||||||
|
users.Use(middleware.SignRequired)
|
||||||
// 用户详细信息
|
// 用户详细信息
|
||||||
users.Get("/profile/{name:string}", controller.UserProfile)
|
users.Get("/profile/{name:string}", controller.UserProfile)
|
||||||
users.Post("/profile", controller.UserProfile)
|
users.Post("/profile", controller.UserProfile)
|
||||||
|
|
Loading…
Reference in New Issue