Compare commits
2 Commits
main
...
513b563728
Author | SHA1 | Date | |
---|---|---|---|
513b563728 | |||
795028c4bb |
48
.drone.yml
Normal file
48
.drone.yml
Normal 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
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user