2022-10-05 16:02:09 +08:00
|
|
|
package test
|
|
|
|
|
2022-10-06 00:01:30 +08:00
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"gofiber.study.skcks.cn/common/errorx"
|
|
|
|
"gofiber.study.skcks.cn/common/response"
|
|
|
|
"gofiber.study.skcks.cn/global"
|
|
|
|
)
|
2022-10-05 16:02:09 +08:00
|
|
|
|
|
|
|
func RegisterController(app *fiber.App) {
|
2022-10-06 00:01:30 +08:00
|
|
|
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))
|
|
|
|
})
|
2022-10-05 16:02:09 +08:00
|
|
|
}
|