From 4d4eb9145efb294c3e66b0337574610e05424aa8 Mon Sep 17 00:00:00 2001 From: icechen Date: Fri, 14 Jan 2022 00:40:35 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E9=A3=9E=E4=B9=A6=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/zeus/api/lark/internal/user/user.go | 27 ++++++++++++++++++++----- app/zeus/api/lark/main.go | 2 ++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/zeus/api/lark/internal/user/user.go b/app/zeus/api/lark/internal/user/user.go index 943784b..f74ba83 100644 --- a/app/zeus/api/lark/internal/user/user.go +++ b/app/zeus/api/lark/internal/user/user.go @@ -5,14 +5,12 @@ import ( "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" "git.icechen.cn/monorepo/backend/pkg/rpc" "github.com/gofiber/fiber/v2" - ctxLogger "github.com/luizsuper/ctxLoggers" - "go.uber.org/zap" ) +// LoginHandle 管理员登录 func LoginHandle(c *fiber.Ctx) error { var p = struct { - Code string `json:"code" validate:"required"` - Email string `json:"email" label:"邮箱" validate:"email"` + Code string `json:"code" validate:"required"` }{} err := api.Bind(c, &p) if err != nil { @@ -26,6 +24,25 @@ func LoginHandle(c *fiber.Ctx) error { return err } - ctxLogger.FInfo(c, "response", zap.Any("resp", response)) + return c.JSON(response) +} + +// UserInfoHandle 管理员信息 +func UserInfoHandle(c *fiber.Ctx) error { + var p = struct { + Token string `json:"token"` + }{} + err := api.Bind(c, &p) + if err != nil { + return err + } + + response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Info(c.Context(), &lark.InfoRequest{ + Token: p.Token, + }) + if err != nil { + return err + } + return c.JSON(response) } diff --git a/app/zeus/api/lark/main.go b/app/zeus/api/lark/main.go index c6216de..7173b9a 100755 --- a/app/zeus/api/lark/main.go +++ b/app/zeus/api/lark/main.go @@ -13,9 +13,11 @@ func main() { ErrorHandler: api.FiberMiddleWareErrHandler, }) router.Use(api.FibberMiddleWareRespHandler) + router.Use(api.FibberUserCtxWithTraceHandler) userRouter := router.Group("/user") userRouter.Post("/login", user.LoginHandle) + userRouter.Post("/info", user.UserInfoHandle) err := router.Listen(":8080") if err != nil { From 5cc8efc9e94e97b9d83f83da12063543f8140030 Mon Sep 17 00:00:00 2001 From: icechen Date: Fri, 14 Jan 2022 00:46:06 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=E7=AE=A1=E7=90=86=E5=91=98=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/zeus/service/lark/internal/lark/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/zeus/service/lark/internal/lark/user.go b/app/zeus/service/lark/internal/lark/user.go index 6668acc..622aa2b 100644 --- a/app/zeus/service/lark/internal/lark/user.go +++ b/app/zeus/service/lark/internal/lark/user.go @@ -8,7 +8,7 @@ import ( func (l Lark) Auth(code string) (*authen.UserAccessTokenInfo, error) { return l.userService.Authens.AccessToken(core.WrapContext(context.Background()), &authen.AuthenAccessTokenReqBody{ - GrantType: "", + GrantType: "authorization_code", Code: code, }).Do() } From e033b49ba1e6a08b02e253fde5e91f519d221030 Mon Sep 17 00:00:00 2001 From: icechen Date: Fri, 14 Jan 2022 00:49:01 +0800 Subject: [PATCH 3/4] feat: AutoMigrate user_token table --- app/zeus/service/lark/internal/model/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/zeus/service/lark/internal/model/init.go b/app/zeus/service/lark/internal/model/init.go index 1b40bdd..ccc1d74 100644 --- a/app/zeus/service/lark/internal/model/init.go +++ b/app/zeus/service/lark/internal/model/init.go @@ -5,7 +5,7 @@ import ( ) func init() { - err := orm.DB.AutoMigrate(&UserInfo{}) + err := orm.DB.AutoMigrate(&UserInfo{}, &UserToken{}) if err != nil { panic(err) } From e932171ffa7c8e4e6d2417a8d85be6daefe1ba7e Mon Sep 17 00:00:00 2001 From: icechen Date: Fri, 14 Jan 2022 01:28:43 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=E5=B0=81=E8=A3=85=E4=B8=AD?= =?UTF-8?q?=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/zeus/api/lark/internal/user/user.go | 23 ++++-------------- app/zeus/api/lark/main.go | 3 ++- app/zeus/service/lark/pkg/admin/user.go | 32 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 app/zeus/service/lark/pkg/admin/user.go diff --git a/app/zeus/api/lark/internal/user/user.go b/app/zeus/api/lark/internal/user/user.go index f74ba83..f19a3ee 100644 --- a/app/zeus/api/lark/internal/user/user.go +++ b/app/zeus/api/lark/internal/user/user.go @@ -1,6 +1,7 @@ package user import ( + "git.icechen.cn/monorepo/backend/app/zeus/service/lark/pkg/admin" "git.icechen.cn/monorepo/backend/pkg/api" "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" "git.icechen.cn/monorepo/backend/pkg/rpc" @@ -27,22 +28,8 @@ func LoginHandle(c *fiber.Ctx) error { return c.JSON(response) } -// UserInfoHandle 管理员信息 -func UserInfoHandle(c *fiber.Ctx) error { - var p = struct { - Token string `json:"token"` - }{} - err := api.Bind(c, &p) - if err != nil { - return err - } - - response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Info(c.Context(), &lark.InfoRequest{ - Token: p.Token, - }) - if err != nil { - return err - } - - return c.JSON(response) +// InfoHandle 管理员信息 +func InfoHandle(c *fiber.Ctx) error { + userInfo := admin.GetUserInfo(c) + return c.JSON(userInfo) } diff --git a/app/zeus/api/lark/main.go b/app/zeus/api/lark/main.go index 7173b9a..a005931 100755 --- a/app/zeus/api/lark/main.go +++ b/app/zeus/api/lark/main.go @@ -2,6 +2,7 @@ package main import ( "git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user" + "git.icechen.cn/monorepo/backend/app/zeus/service/lark/pkg/admin" "git.icechen.cn/monorepo/backend/pkg/api" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" @@ -17,7 +18,7 @@ func main() { userRouter := router.Group("/user") userRouter.Post("/login", user.LoginHandle) - userRouter.Post("/info", user.UserInfoHandle) + userRouter.Post("/info", admin.MiddlewareLarkAdmin, user.InfoHandle) err := router.Listen(":8080") if err != nil { diff --git a/app/zeus/service/lark/pkg/admin/user.go b/app/zeus/service/lark/pkg/admin/user.go new file mode 100644 index 0000000..a36835b --- /dev/null +++ b/app/zeus/service/lark/pkg/admin/user.go @@ -0,0 +1,32 @@ +package admin + +import ( + "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" + "git.icechen.cn/monorepo/backend/pkg/rpc" + "github.com/gofiber/fiber/v2" +) + +const AuthorizationHeader = "Authorization" +const UserInfoKey = "user_info" + +type UserInfo = *lark.InfoResponse + +// MiddlewareLarkAdmin 飞书管理员token校验 +func MiddlewareLarkAdmin(c *fiber.Ctx) error { + token := c.Get(AuthorizationHeader) + + response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Info(c.Context(), &lark.InfoRequest{ + Token: token, + }) + if err != nil { + return err + } + + c.Context().SetUserValue(UserInfoKey, response) + return c.Next() +} + +// GetUserInfo 获取用户信息 +func GetUserInfo(c *fiber.Ctx) UserInfo { + return c.Context().UserValue(UserInfoKey).(UserInfo) +}