feat(plugin): add dispatcher for connector login plugin

This commit is contained in:
LinkinStars 2023-03-16 15:39:04 +08:00
parent 5e6fb1096e
commit 1ec3f2b704
2 changed files with 41 additions and 9 deletions

View File

@ -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,

View File

@ -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)
}