feat(plugin): remove external user login authentication route

This commit is contained in:
LinkinStar 2023-01-09 18:43:52 +08:00
parent 06db2bd091
commit ec01c3e047
5 changed files with 46 additions and 90 deletions

View File

@ -120,7 +120,9 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
roleService := role2.NewRoleService(roleRepo)
userRoleRelService := role2.NewUserRoleRelService(userRoleRelRepo, roleService)
userCommon := usercommon.NewUserCommon(userRepo, userRoleRelService, authService)
userService := service.NewUserService(userRepo, userActiveActivityRepo, activityRepo, emailService, authService, serviceConf, siteInfoCommonService, userRoleRelService, userCommon)
userExternalLoginRepo := user_external_login.NewUserExternalLoginRepo(dataData)
userExternalLoginService := user_external_login2.NewUserExternalLoginService(userRepo, userCommon, userExternalLoginRepo, emailService, siteInfoCommonService, userActiveActivityRepo)
userService := service.NewUserService(userRepo, userActiveActivityRepo, activityRepo, emailService, authService, serviceConf, siteInfoCommonService, userRoleRelService, userCommon, userExternalLoginService)
captchaRepo := captcha.NewCaptchaRepo(dataData)
captchaService := action.NewCaptchaService(captchaRepo)
uploaderService := uploader.NewUploaderService(serviceConf, siteInfoCommonService)
@ -212,8 +214,6 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService, dataData, siteInfoCommonService)
templateController := controller.NewTemplateController(templateRenderController, siteInfoCommonService)
templateRouter := router.NewTemplateRouter(templateController, templateRenderController, siteInfoController)
userExternalLoginRepo := user_external_login.NewUserExternalLoginRepo(dataData)
userExternalLoginService := user_external_login2.NewUserExternalLoginService(userRepo, userCommon, userExternalLoginRepo, emailService, siteInfoCommonService, userActiveActivityRepo)
connectorController := controller.NewConnectorController(siteInfoCommonService, emailService, userExternalLoginService)
pluginAPIRouter := router.NewPluginAPIRouter(connectorController)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware, templateRouter, pluginAPIRouter)

View File

@ -5,7 +5,6 @@ import (
"net/http"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/plugin"
_ "github.com/answerdev/answer/internal/plugin/connector"
"github.com/answerdev/answer/internal/schema"
@ -13,7 +12,6 @@ import (
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/answerdev/answer/internal/service/user_external_login"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
)
@ -82,9 +80,9 @@ func (cc *ConnectorController) ConnectorRedirect(connector plugin.Connector) (fn
return
}
if len(resp.AccessToken) > 0 {
ctx.Redirect(http.StatusFound, fmt.Sprintf("/index?token=%s", resp.AccessToken))
ctx.Redirect(http.StatusFound, fmt.Sprintf("/users/oauth?access_token=%s", resp.AccessToken))
} else {
ctx.Redirect(http.StatusFound, fmt.Sprintf("/binding?binding_key=%s", resp.BindingKey))
ctx.Redirect(http.StatusFound, fmt.Sprintf("/users/confirm-email?binding_key=%s", resp.BindingKey))
}
}
}
@ -121,20 +119,3 @@ func (cc *ConnectorController) ExternalLoginBindingUserSendEmail(ctx *gin.Contex
resp, err := cc.userExternalService.ExternalLoginBindingUserSendEmail(ctx, req)
handler.HandleResponse(ctx, err, resp)
}
func (cc *ConnectorController) ExternalLoginBindingUser(ctx *gin.Context) {
req := &schema.ExternalLoginBindingUserReq{}
if handler.BindAndCheck(ctx, req) {
return
}
req.Content = cc.emailService.VerifyUrlExpired(ctx, req.Code)
if len(req.Content) == 0 {
handler.HandleResponse(ctx, errors.Forbidden(reason.EmailVerifyURLExpired),
&schema.ForbiddenResp{Type: schema.ForbiddenReasonTypeURLExpired})
return
}
resp, err := cc.userExternalService.ExternalLoginBindingUser(ctx, req)
handler.HandleResponse(ctx, err, resp)
}

View File

@ -28,5 +28,4 @@ func (pr *PluginAPIRouter) RegisterConnector(r *gin.Engine) {
})
r.GET("/answer/api/v1/connector/info", connectorController.ConnectorsInfo)
r.POST("/answer/api/v1/connector/binding/email", connectorController.ExternalLoginBindingUserSendEmail)
r.POST("/answer/api/v1/connector/binding", connectorController.ExternalLoginBindingUser)
}

View File

@ -206,49 +206,10 @@ func (us *UserExternalLoginService) ExternalLoginBindingUserSendEmail(
// ExternalLoginBindingUser
// The user clicks on the email link of the bound account and requests the API to bind the user officially
func (us *UserExternalLoginService) ExternalLoginBindingUser(
ctx context.Context, req *schema.ExternalLoginBindingUserReq) (
resp *schema.ExternalLoginBindingUserResp, err error) {
data := &schema.EmailCodeContent{}
err = data.FromJSONString(req.Content)
if err != nil {
return nil, errors.BadRequest(reason.EmailVerifyURLExpired)
}
if data.SourceType != schema.BindingSourceType {
log.Warnf("invalid email source type %s", data.BindingKey)
return nil, errors.BadRequest(reason.EmailVerifyURLExpired)
}
oldUserInfo, exist, err := us.userRepo.GetByUserID(ctx, data.UserID)
if err != nil {
return nil, err
}
if !exist {
return nil, errors.BadRequest(reason.UserNotFound)
}
// If the user's email is not already authenticated, update the email status.
if oldUserInfo.MailStatus == entity.EmailStatusToBeVerified {
err = us.userRepo.UpdateEmailStatus(ctx, data.UserID, entity.EmailStatusAvailable)
if err != nil {
return nil, err
}
if err = us.userActivity.UserActive(ctx, data.UserID); err != nil {
log.Error(err)
}
oldUserInfo.MailStatus = entity.EmailStatusAvailable
}
externalLoginInfo, err := us.userExternalLoginRepo.GetCacheUserExternalLoginInfo(ctx, data.BindingKey)
ctx context.Context, bindingKey string, oldUserInfo *entity.User) (err error) {
externalLoginInfo, err := us.userExternalLoginRepo.GetCacheUserExternalLoginInfo(ctx, bindingKey)
if err != nil || len(externalLoginInfo.ExternalID) == 0 {
return nil, errors.BadRequest(reason.UserNotFound)
return errors.BadRequest(reason.UserNotFound)
}
err = us.bindOldUser(ctx, externalLoginInfo, oldUserInfo)
if err != nil {
return nil, err
}
accessToken, _, err := us.userCommonService.CacheLoginUserInfo(
ctx, oldUserInfo.ID, oldUserInfo.MailStatus, oldUserInfo.Status)
resp = &schema.ExternalLoginBindingUserResp{AccessToken: accessToken}
return resp, err
return us.bindOldUser(ctx, externalLoginInfo, oldUserInfo)
}

View File

@ -20,6 +20,7 @@ import (
"github.com/answerdev/answer/internal/service/service_config"
"github.com/answerdev/answer/internal/service/siteinfo_common"
usercommon "github.com/answerdev/answer/internal/service/user_common"
"github.com/answerdev/answer/internal/service/user_external_login"
"github.com/google/uuid"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
@ -30,15 +31,16 @@ import (
// UserService user service
type UserService struct {
userCommonService *usercommon.UserCommon
userRepo usercommon.UserRepo
userActivity activity.UserActiveActivityRepo
activityRepo activity_common.ActivityRepo
serviceConfig *service_config.ServiceConfig
emailService *export.EmailService
authService *auth.AuthService
siteInfoService *siteinfo_common.SiteInfoCommonService
userRoleService *role.UserRoleRelService
userCommonService *usercommon.UserCommon
userRepo usercommon.UserRepo
userActivity activity.UserActiveActivityRepo
activityRepo activity_common.ActivityRepo
serviceConfig *service_config.ServiceConfig
emailService *export.EmailService
authService *auth.AuthService
siteInfoService *siteinfo_common.SiteInfoCommonService
userRoleService *role.UserRoleRelService
userExternalLoginService *user_external_login.UserExternalLoginService
}
func NewUserService(userRepo usercommon.UserRepo,
@ -50,17 +52,19 @@ func NewUserService(userRepo usercommon.UserRepo,
siteInfoService *siteinfo_common.SiteInfoCommonService,
userRoleService *role.UserRoleRelService,
userCommonService *usercommon.UserCommon,
userExternalLoginService *user_external_login.UserExternalLoginService,
) *UserService {
return &UserService{
userCommonService: userCommonService,
userRepo: userRepo,
userActivity: userActivity,
activityRepo: activityRepo,
emailService: emailService,
serviceConfig: serviceConfig,
authService: authService,
siteInfoService: siteInfoService,
userRoleService: userRoleService,
userCommonService: userCommonService,
userRepo: userRepo,
userActivity: userActivity,
activityRepo: activityRepo,
emailService: emailService,
serviceConfig: serviceConfig,
authService: authService,
siteInfoService: siteInfoService,
userRoleService: userRoleService,
userExternalLoginService: userExternalLoginService,
}
}
@ -427,14 +431,25 @@ func (us *UserService) UserVerifyEmail(ctx context.Context, req *schema.UserVeri
if !has {
return nil, errors.BadRequest(reason.UserNotFound)
}
userInfo.MailStatus = entity.EmailStatusAvailable
err = us.userRepo.UpdateEmailStatus(ctx, userInfo.ID, userInfo.MailStatus)
if err != nil {
return nil, err
if userInfo.MailStatus == entity.EmailStatusToBeVerified {
userInfo.MailStatus = entity.EmailStatusAvailable
err = us.userRepo.UpdateEmailStatus(ctx, userInfo.ID, userInfo.MailStatus)
if err != nil {
return nil, err
}
}
if err = us.userActivity.UserActive(ctx, userInfo.ID); err != nil {
log.Error(err)
}
// In the case of three-party login, the associated users are bound
if len(data.BindingKey) > 0 {
err = us.userExternalLoginService.ExternalLoginBindingUser(ctx, data.BindingKey, userInfo)
if err != nil {
return nil, err
}
}
accessToken, userCacheInfo, err := us.userCommonService.CacheLoginUserInfo(
ctx, userInfo.ID, userInfo.MailStatus, userInfo.Status)
if err != nil {