diff --git a/controller/test/test.go b/controller/test/test.go index 62a48ce..d9cc2c4 100644 --- a/controller/test/test.go +++ b/controller/test/test.go @@ -1,7 +1,67 @@ package test -import "github.com/gofiber/fiber/v2" +import ( + "github.com/gofiber/fiber/v2" + "gofiber.study.skcks.cn/common/errorx" + "gofiber.study.skcks.cn/common/response" + "gofiber.study.skcks.cn/global" +) func RegisterController(app *fiber.App) { - app.Group("/test") + group := app.Group("/test") + jwtEncode(group) + jwtDecode(group) +} + +// jwtEncode jwt token 生成测试 +// +// @Summary jwt token 生成测试 +// @Description jwt token 生成测试 +// @Tags Test +// @Accept json +// @Produce json +// @Param userClaims body global.UserClaims true "userClaims" +// @Success 200 {object} response.Response{data=string} +// @Failure default {object} errorx.CodeErrorResponse +// @Router /test/jwt [post] +func jwtEncode(r fiber.Router) { + r.Add(fiber.MethodPost, "/jwt", func(ctx *fiber.Ctx) error { + claims := global.UserClaims{} + err := ctx.BodyParser(&claims) + + if err != nil { + return ctx.JSON(errorx.NewDefaultError(err.Error())) + } + + token, err := global.GetToken(claims) + if err != nil { + return ctx.JSON(errorx.NewDefaultError(err.Error())) + } + + return ctx.JSON(response.NewResponse(token)) + }) +} + +// jwtDecode jwt token 解析测试 +// +// @Summary jwt token 解析测试 +// @Description jwt token 解析测试 +// @Tags Test +// @Accept json +// @Produce json +// @Param token query string true "token" +// @Success 200 {object} response.Response{data=global.UserClaims} +// @Failure default {object} errorx.CodeErrorResponse +// @Router /test/jwt [get] +func jwtDecode(r fiber.Router) { + r.Add(fiber.MethodGet, "/jwt", func(ctx *fiber.Ctx) error { + token := ctx.Query("token") + + claims, err := global.ParseToken(token) + if err != nil { + return ctx.JSON(errorx.NewDefaultError(err.Error())) + } + + return ctx.JSON(response.NewResponse(claims)) + }) } diff --git a/docs/docs.go b/docs/docs.go index 803d8d4..eebd9fc 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -379,6 +379,106 @@ const docTemplate = `{ } } }, + "/test/jwt": { + "get": { + "description": "jwt token 解析测试", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Test" + ], + "summary": "jwt token 解析测试", + "parameters": [ + { + "type": "string", + "description": "token", + "name": "token", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/global.UserClaims" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "post": { + "description": "jwt token 生成测试", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Test" + ], + "summary": "jwt token 生成测试", + "parameters": [ + { + "description": "userClaims", + "name": "userClaims", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/global.UserClaims" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + }, "/user/account": { "get": { "security": [ @@ -474,6 +574,17 @@ const docTemplate = `{ } } }, + "global.UserClaims": { + "type": "object", + "properties": { + "account": { + "type": "string" + }, + "id": { + "type": "string" + } + } + }, "models.User": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 15761fa..2adebd2 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -371,6 +371,106 @@ } } }, + "/test/jwt": { + "get": { + "description": "jwt token 解析测试", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Test" + ], + "summary": "jwt token 解析测试", + "parameters": [ + { + "type": "string", + "description": "token", + "name": "token", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/global.UserClaims" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + }, + "post": { + "description": "jwt token 生成测试", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Test" + ], + "summary": "jwt token 生成测试", + "parameters": [ + { + "description": "userClaims", + "name": "userClaims", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/global.UserClaims" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + }, "/user/account": { "get": { "security": [ @@ -466,6 +566,17 @@ } } }, + "global.UserClaims": { + "type": "object", + "properties": { + "account": { + "type": "string" + }, + "id": { + "type": "string" + } + } + }, "models.User": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2f42415..262d686 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -27,6 +27,13 @@ definitions: description: Original registered route path type: string type: object + global.UserClaims: + properties: + account: + type: string + id: + type: string + type: object models.User: properties: account: @@ -295,6 +302,66 @@ paths: summary: 获取所有路由 tags: - Routes + /test/jwt: + get: + consumes: + - application/json + description: jwt token 解析测试 + parameters: + - description: token + in: query + name: token + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/response.Response' + - properties: + data: + $ref: '#/definitions/global.UserClaims' + type: object + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: jwt token 解析测试 + tags: + - Test + post: + consumes: + - application/json + description: jwt token 生成测试 + parameters: + - description: userClaims + in: body + name: userClaims + required: true + schema: + $ref: '#/definitions/global.UserClaims' + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/response.Response' + - properties: + data: + type: string + type: object + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: jwt token 生成测试 + tags: + - Test /user/account: get: consumes: diff --git a/global/datasources.go b/global/datasources.go index 9f54bfb..d00efe0 100644 --- a/global/datasources.go +++ b/global/datasources.go @@ -15,7 +15,7 @@ func SyncModelToDataSource() { logger.Log.Infof("[*] 同步数据库/表结构") for _, model := range modelArr { - err := DataSources.Sync(model) + err := DataSources.Sync2(model) logger.Log.Infof("[√] 同步 %s 表 数据结构 成功", model.TableName()) if err != nil { diff --git a/global/jwt.go b/global/jwt.go index ca2ded1..34f1eb8 100644 --- a/global/jwt.go +++ b/global/jwt.go @@ -9,11 +9,14 @@ import ( var JwtConfig *config.JwtConfig +// UserClaims +// @Param id body string true +// @Param account body string true type UserClaims struct { Id string `json:"id"` Account string `json:"account"` - jwt.RegisteredClaims + jwt.RegisteredClaims `swaggerignore:"true"` } func GetToken(claims UserClaims) (string, error) { @@ -24,12 +27,15 @@ func GetToken(claims UserClaims) (string, error) { claims.ExpiresAt = jwt.NewNumericDate(time.Unix(expire, 0)) token := jwt.New(jwt.SigningMethodHS256) + + token.Claims = claims + return token.SignedString([]byte(JwtConfig.Secret)) } func ParseToken(tokenStr string) (*UserClaims, error) { token, err := jwt.ParseWithClaims(tokenStr, &UserClaims{}, func(token *jwt.Token) (interface{}, error) { - return JwtConfig.Secret, nil + return []byte(JwtConfig.Secret), nil }) if err != nil {