mirror of https://gitee.com/answerdev/answer.git
feat(plugin): add dispatcher for connector login plugin
This commit is contained in:
parent
5e6fb1096e
commit
1ec3f2b704
|
@ -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)) {
|
func (cc *ConnectorController) ConnectorLogin(connector plugin.Connector) (fn func(ctx *gin.Context)) {
|
||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
general, err := cc.siteInfoService.GetSiteGeneral(ctx)
|
general, err := cc.siteInfoService.GetSiteGeneral(ctx)
|
||||||
|
@ -68,13 +103,15 @@ func (cc *ConnectorController) ConnectorRedirect(connector plugin.Connector) (fn
|
||||||
ctx.Redirect(http.StatusFound, "/50x")
|
ctx.Redirect(http.StatusFound, "/50x")
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
log.Errorf("connector received failed: %v", err)
|
log.Errorf("connector received failed: %v", err)
|
||||||
ctx.Redirect(http.StatusFound, "/50x")
|
ctx.Redirect(http.StatusFound, "/50x")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Infof("connector received: %+v", userInfo)
|
log.Debugf("connector received: %+v", userInfo)
|
||||||
u := &schema.ExternalLoginUserInfoCache{
|
u := &schema.ExternalLoginUserInfoCache{
|
||||||
Provider: connector.ConnectorSlugName(),
|
Provider: connector.ConnectorSlugName(),
|
||||||
ExternalID: userInfo.ExternalID,
|
ExternalID: userInfo.ExternalID,
|
||||||
|
|
|
@ -2,7 +2,6 @@ package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/answerdev/answer/internal/controller"
|
"github.com/answerdev/answer/internal/controller"
|
||||||
"github.com/answerdev/answer/plugin"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,12 +19,8 @@ func NewPluginAPIRouter(
|
||||||
|
|
||||||
func (pr *PluginAPIRouter) RegisterUnAuthConnectorRouter(r *gin.RouterGroup) {
|
func (pr *PluginAPIRouter) RegisterUnAuthConnectorRouter(r *gin.RouterGroup) {
|
||||||
connectorController := pr.connectorController
|
connectorController := pr.connectorController
|
||||||
_ = plugin.CallConnector(func(connector plugin.Connector) error {
|
r.GET(controller.ConnectorLoginRouterPrefix+":name", connectorController.ConnectorLoginDispatcher)
|
||||||
connectorSlugName := connector.ConnectorSlugName()
|
r.GET(controller.ConnectorRedirectRouterPrefix+":name", connectorController.ConnectorRedirectDispatcher)
|
||||||
r.GET(controller.ConnectorLoginRouterPrefix+connectorSlugName, connectorController.ConnectorLogin(connector))
|
|
||||||
r.GET(controller.ConnectorRedirectRouterPrefix+connectorSlugName, connectorController.ConnectorRedirect(connector))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
r.GET("/connector/info", connectorController.ConnectorsInfo)
|
r.GET("/connector/info", connectorController.ConnectorsInfo)
|
||||||
r.POST("/connector/binding/email", connectorController.ExternalLoginBindingUserSendEmail)
|
r.POST("/connector/binding/email", connectorController.ExternalLoginBindingUserSendEmail)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue