From 1ec3f2b704a89e9e22698fd4ace03826141a421a Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Thu, 16 Mar 2023 15:39:04 +0800 Subject: [PATCH] feat(plugin): add dispatcher for connector login plugin --- internal/controller/connector_controller.go | 41 ++++++++++++++++++++- internal/router/plugin_api_router.go | 9 +---- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/internal/controller/connector_controller.go b/internal/controller/connector_controller.go index e6b2d611..cf86eec3 100644 --- a/internal/controller/connector_controller.go +++ b/internal/controller/connector_controller.go @@ -41,6 +41,41 @@ func NewConnectorController( } } +// ConnectorLoginDispatcher dispatch connector login request to specific connector by slug name +// We can't register specific router for each connector when application start, because the plugin status will be changed by admin. +// If the plugin is disabled, the router should be unavailable. +func (cc *ConnectorController) ConnectorLoginDispatcher(ctx *gin.Context) { + slugName := ctx.Param("name") + var c plugin.Connector + _ = plugin.CallConnector(func(connector plugin.Connector) error { + if connector.ConnectorSlugName() == slugName { + c = connector + } + return nil + }) + if c == nil { + ctx.Redirect(http.StatusFound, "/50x") + return + } + cc.ConnectorLogin(c)(ctx) +} + +func (cc *ConnectorController) ConnectorRedirectDispatcher(ctx *gin.Context) { + slugName := ctx.Param("name") + var c plugin.Connector + _ = plugin.CallConnector(func(connector plugin.Connector) error { + if connector.ConnectorSlugName() == slugName { + c = connector + } + return nil + }) + if c == nil { + ctx.Redirect(http.StatusFound, "/50x") + return + } + cc.ConnectorRedirect(c)(ctx) +} + func (cc *ConnectorController) ConnectorLogin(connector plugin.Connector) (fn func(ctx *gin.Context)) { return func(ctx *gin.Context) { general, err := cc.siteInfoService.GetSiteGeneral(ctx) @@ -68,13 +103,15 @@ func (cc *ConnectorController) ConnectorRedirect(connector plugin.Connector) (fn ctx.Redirect(http.StatusFound, "/50x") return } - userInfo, err := connector.ConnectorReceiver(ctx) + receiverURL := fmt.Sprintf("%s%s%s%s", siteGeneral.SiteUrl, + commonRouterPrefix, ConnectorRedirectRouterPrefix, connector.ConnectorSlugName()) + userInfo, err := connector.ConnectorReceiver(ctx, receiverURL) if err != nil { log.Errorf("connector received failed: %v", err) ctx.Redirect(http.StatusFound, "/50x") return } - log.Infof("connector received: %+v", userInfo) + log.Debugf("connector received: %+v", userInfo) u := &schema.ExternalLoginUserInfoCache{ Provider: connector.ConnectorSlugName(), ExternalID: userInfo.ExternalID, diff --git a/internal/router/plugin_api_router.go b/internal/router/plugin_api_router.go index 5d55863b..31a6ad74 100644 --- a/internal/router/plugin_api_router.go +++ b/internal/router/plugin_api_router.go @@ -2,7 +2,6 @@ package router import ( "github.com/answerdev/answer/internal/controller" - "github.com/answerdev/answer/plugin" "github.com/gin-gonic/gin" ) @@ -20,12 +19,8 @@ func NewPluginAPIRouter( func (pr *PluginAPIRouter) RegisterUnAuthConnectorRouter(r *gin.RouterGroup) { connectorController := pr.connectorController - _ = plugin.CallConnector(func(connector plugin.Connector) error { - connectorSlugName := connector.ConnectorSlugName() - r.GET(controller.ConnectorLoginRouterPrefix+connectorSlugName, connectorController.ConnectorLogin(connector)) - r.GET(controller.ConnectorRedirectRouterPrefix+connectorSlugName, connectorController.ConnectorRedirect(connector)) - return nil - }) + r.GET(controller.ConnectorLoginRouterPrefix+":name", connectorController.ConnectorLoginDispatcher) + r.GET(controller.ConnectorRedirectRouterPrefix+":name", connectorController.ConnectorRedirectDispatcher) r.GET("/connector/info", connectorController.ConnectorsInfo) r.POST("/connector/binding/email", connectorController.ExternalLoginBindingUserSendEmail) }