Compare commits

..

2 Commits

Author SHA1 Message Date
513b563728 将model从pak包中移出到根目录
All checks were successful
continuous-integration/drone Build is passing
2022-04-05 18:16:20 +08:00
795028c4bb CI test #6 (+5 squashed commit)
All checks were successful
continuous-integration/drone/push Build is passing
Squashed commit:

[c6787e1] CI test #5

[ad1003e] CI test #4

[3d4607c] CI test #3

[97b1961] CI test #2

[4cab727] CI test #1
2022-04-03 19:45:05 +08:00
12 changed files with 102 additions and 21 deletions

48
.drone.yml Normal file
View File

@@ -0,0 +1,48 @@
kind: pipeline
type: docker
name: compile
steps:
# - name: restore-pkg
# image: drillster/drone-volume-cache
# volumes:
# - name: cache
# path: /cache
# settings:
# restore: true
# mount:
# - $GOPATH/pkg
# - name: test
# image: eigeen/golang-devops:1.18
# commands:
# - go test
- name: build
image: golang:1.18
environments:
- CGO_ENABLED=0
- GOOS=linux
- GOARCH=amd64
commands:
- go build
# - name: rebuild-pkg
# image: drillster/drone-volume-cache
# volumes:
# - name: cache
# path: /cache
# settings:
# rebuild: true
# mount:
# - $GOPATH/pkg
# #当对应条件的时候才会执行
# when:
# status:
# - success
# - failure
# volumes:
# - name: cache
# host:
# path: /tmp/cache

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)
}
}

View File

@@ -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

View File

@@ -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, "未登录")
}

View File

@@ -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"`
}

View File

@@ -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()
}

View File

@@ -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"`
}

View File

@@ -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"

View File

@@ -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)