Files
exchange_go/common/service/sysservice/authservice/authenticator.go

403 lines
14 KiB
Go
Raw Permalink Normal View History

2025-02-06 11:14:33 +08:00
package authservice
//
//import (
// "go-admin/app/admin/models"
// "go-admin/app/admin/models/sysmodel"
// "go-admin/app/admin/service/aduserdb"
// "go-admin/app/admin/service/otcdb/orderdb"
// "go-admin/common/const/enum/businesstype"
// statuscode "go-admin/common/status_code"
// "go-admin/pkg/googleauthhelper"
// "go-admin/pkg/utility"
//
// log "github.com/go-admin-team/go-admin-core/logger"
// "gorm.io/gorm"
//
// "go.uber.org/zap"
//)
//
//// GetUserAuthSwitch 获取用户验证器开关状态
//func GetUserAuthSwitch(orm *gorm.DB, userID int) (auth sysmodel.UserAuthSwitchStatus, err error) {
// // 获取用户验证器开关
// auth, err = aduserdb.GetUserAuthSwitch(orm, userID)
// if err != nil {
// return
// }
//
// if auth.PhoneAuth != 1 {
// auth.PhoneAuth = 0
// }
// if auth.EmailAuth != 1 {
// auth.EmailAuth = 0
// }
// if auth.GoogleAuth != 1 {
// auth.GoogleAuth = 0
// }
//
// return
//}
//
//// AuthenticatorVerify 已开通验证器校验
//func AuthenticatorVerify(orm *gorm.DB, authenticator sysmodel.Authenticator) int {
// // 是否完全验证
// if authenticator.PhoneAuth == 1 && len(authenticator.SmsCaptcha) == 0 ||
// authenticator.EmailAuth == 1 && len(authenticator.EmailCaptcha) == 0 ||
// authenticator.GoogleAuth == 1 && len(authenticator.GoogleCaptcha) == 0 {
//
// return statuscode.PleasePerformCompleteSecurityVerification
// }
// var phoneCheck sysmodel.CheckCaptcha
// var emailCheck sysmodel.CheckCaptcha
// // 校验验证码
// if authenticator.PhoneAuth == 1 {
// phoneCheck = sysmodel.CheckCaptcha{
// BusinessType: int(authenticator.BusinessType),
// Receive: authenticator.Phone,
// Captcha: authenticator.SmsCaptcha,
// }
// }
// if authenticator.EmailAuth == 1 {
// emailCheck = sysmodel.CheckCaptcha{
// BusinessType: int(authenticator.BusinessType),
// Receive: authenticator.Email,
// Captcha: authenticator.EmailCaptcha,
// }
// }
// var googleSecret string
// var googleCaptcha string
// if authenticator.GoogleAuth == 1 {
// googleSecret = authenticator.GoogleSecret
// googleCaptcha = authenticator.GoogleCaptcha
// }
// // 检验验证码
// code := CheckCaptchaNew(orm, phoneCheck, emailCheck, googleSecret, googleCaptcha)
// return code
//}
//
//// AuthSwitchBefore 身份验证开关前校验(新)
//func AuthSwitchBefore(orm *gorm.DB, auth sysmodel.AuthenticatorSwitch) int {
// // 开启手机、更改手机、开启邮箱、更改邮箱 判断是否被占用
// var user models.AdUser
// var err error
// if auth.BusinessType == businesstype.OpenPhoneAuth || auth.BusinessType == businesstype.ChangePhoneAuth {
// user, err = aduserdb.GetUserByPhone(orm, auth.NewPhoneArea, auth.NewPhone)
// if err != nil {
// return statuscode.ServerError
// }
// if user.Id != 0 && user.Id != auth.UserID {
// return statuscode.ThePhoneHasBeenBound
// }
// } else if auth.BusinessType == businesstype.OpenEmailAuth || auth.BusinessType == businesstype.ChangeEmailAuth {
// user, err = aduserdb.GetUserByEmail(orm, auth.NewEmail) //GetUser("useremail", auth.NewEmail)
// if err != nil {
// return statuscode.ServerError
// }
// if user.Id != 0 && user.Id != auth.UserID {
// return statuscode.TheEmailHasBeenBound
// }
// }
//
// // 获取用户验证器开关状态
// authSwitch, err := GetUserAuthSwitch(orm, auth.UserID)
// if err != nil {
// return statuscode.ServerError
// }
//
// // 关闭验证器 需保证至少两个开启
// if auth.BusinessType == businesstype.ClosePhoneAuth ||
// auth.BusinessType == businesstype.CloseEmailAuth ||
// auth.BusinessType == businesstype.CloseGoogleAuth {
//
// if (authSwitch.PhoneAuth + authSwitch.EmailAuth + authSwitch.GoogleAuth) < 3 {
// return statuscode.AllAuthMustOpen
// }
//
// // OTC 订单状态为1-待付款、3-代放币、7-申诉中时,不允许解绑手机
// if auth.BusinessType == businesstype.ClosePhoneAuth {
// // hc todo 注释
// // 广告
// // sum, err := advertisedb.GetUserAdvertiseSum(auth.UserID)
// // if err != nil {
// // return statuscode.ServerError
// // }
// // if sum > 0 {
// // return statuscode.OTCAdvertiseAreInProgress
// // }
// // 订单
// num, err := orderdb.GetUnfilledOrderCount(orm, auth.UserID)
// if err != nil {
// return statuscode.ServerError
// }
// if num > 0 {
// return statuscode.OTCOrdersAreInProgress
// }
// }
// }
// if user.Id == 0 {
// user, err = aduserdb.GetUserById(orm, auth.UserID) //"id", auth.UserID)
// if err != nil {
// return statuscode.ServerError
// }
// }
//
// // ==================== 1 已开通验证器校验 ==================== //
// validator := sysmodel.Authenticator{
// UserID: auth.UserID,
// PhoneAuth: authSwitch.PhoneAuth,
// EmailAuth: authSwitch.EmailAuth,
// GoogleAuth: authSwitch.GoogleAuth,
// Phone: user.Phone,
// Email: user.UserEmail,
// GoogleSecret: authSwitch.GoogleSecret,
// SmsCaptcha: auth.SmsCaptcha,
// EmailCaptcha: auth.EmailCaptcha,
// GoogleCaptcha: auth.GoogleCaptcha,
// BusinessType: auth.BusinessType,
// }
//
// // 是否完全验证
// if validator.PhoneAuth == 1 && len(validator.SmsCaptcha) == 0 ||
// validator.EmailAuth == 1 && len(validator.EmailCaptcha) == 0 ||
// validator.GoogleAuth == 1 && len(validator.GoogleCaptcha) == 0 {
//
// return statuscode.PleasePerformCompleteSecurityVerification
// }
// var phoneCode models.AdVerifyCode
// if len(validator.SmsCaptcha) > 0 && !CheckOpenVerifyCode(validator.SmsCaptcha) {
// // 校验手机号码发送的验证码
// phoneCheck := sysmodel.CheckCaptcha{
// BusinessType: int(businesstype.WithdrawAuth),
// Receive: validator.Phone,
// Captcha: validator.SmsCaptcha,
// }
// phoneCode = aduserdb.GetVerifyCode(orm, phoneCheck)
// if phoneCode.Id == 0 {
// return statuscode.PhoneCaptchaInvalid
// }
// }
//
// var emailCode models.AdVerifyCode
// if len(validator.EmailCaptcha) > 0 && !CheckOpenVerifyCode(validator.EmailCaptcha) {
// // 校验邮箱号码发送的验证码
// emailCheck := sysmodel.CheckCaptcha{
// BusinessType: int(businesstype.WithdrawAuth),
// Receive: validator.Email,
// Captcha: validator.EmailCaptcha,
// }
// emailCode = aduserdb.GetVerifyCode(orm, emailCheck)
// if emailCode.Id == 0 {
// return statuscode.EmailCaptchaInvalid
// }
// }
// if len(validator.GoogleCaptcha) > 0 {
// // 校验谷歌的验证码
// auth2 := googleauthhelper.VerifyCode(validator.GoogleSecret, utility.StringAsInt32(validator.GoogleCaptcha))
// if !auth2 {
// return statuscode.GoogleCaptchaInvalid
// }
// }
//
// // ==================== 2 新绑定的验证器校验 ==================== //
// var phoneNewCode models.AdVerifyCode
// // 开启手机|更改手机验证 都需要校验新手机
// if auth.BusinessType == businesstype.OpenPhoneAuth || auth.BusinessType == businesstype.ChangePhoneAuth {
// if !CheckOpenVerifyCode(auth.NewPhoneCaptcha) {
// cc := sysmodel.CheckCaptcha{
// BusinessType: int(auth.BusinessType),
// Receive: auth.NewPhone,
// Captcha: auth.NewPhoneCaptcha,
// }
// phoneNewCode = aduserdb.GetVerifyCode(orm, cc)
// if phoneNewCode.Id == 0 {
// return statuscode.NewPhoneCaptchaInvalid
// }
// }
// }
//
// var emailNewCode models.AdVerifyCode
// // 开启邮箱|更改邮箱验证 都需要校验新邮箱
// if auth.BusinessType == businesstype.OpenEmailAuth || auth.BusinessType == businesstype.ChangeEmailAuth {
// if !CheckOpenVerifyCode(auth.NewEmailCaptcha) {
// cc := sysmodel.CheckCaptcha{
// BusinessType: int(auth.BusinessType),
// Receive: auth.NewEmail,
// Captcha: auth.NewEmailCaptcha,
// }
// emailNewCode = aduserdb.GetVerifyCode(orm, cc)
// if emailNewCode.Id == 0 {
// return statuscode.NewEmailCaptchaInvalid
// }
// }
// }
//
// // 开启谷歌验证器 需要验证谷歌
// if auth.BusinessType == businesstype.OpenGoogleAuth {
// newSecret, _ := aduserdb.GetNewGoogleSecret(orm, auth.UserID)
// if ok := googleauthhelper.VerifyCode(newSecret, utility.StringAsInt32(auth.NewGoogleCaptcha)); !ok {
// // tx.Rollback()
// log.Error("google验证码校验失败")
// return statuscode.GoogleCaptchaInvalid
// }
// }
// if phoneCode.Id == 0 && phoneNewCode.Id == 0 && emailCode.Id == 0 && emailNewCode.Id == 0 {
// log.Error("AuthSwitchBefore", zap.String("没验证旧手机、新手机、旧邮箱、新邮箱,估计只验证谷歌验证器",
// "phoneCode.Id ==0 && phoneNewCode.Id ==0 && emailCode.Id ==0&& emailNewCode.Id ==0"))
// return statuscode.OK
// }
//
// var transCode int
// err = orm.Transaction(func(tx *gorm.DB) error {
// // ==================== 1 已开通验证器校验,修改表数据为已验证 ==================== //
// if phoneCode.Id > 0 { // 旧手机验证码修改
// err := aduserdb.UpdateVerifyFlag(tx, phoneCode.Id)
// if err != nil {
// log.Error("AuthSwitchBefore phoneCode", zap.Error(err))
// // _ = tx.Rollback()
// transCode = statuscode.PhoneCaptchaInvalid
// return err
// }
// }
// if emailCode.Id > 0 { // 旧邮箱验证码修改
// err = aduserdb.UpdateVerifyFlag(tx, emailCode.Id)
// if err != nil {
// log.Error("AuthSwitchBefore emailCode", zap.Error(err))
// // _ = tx.Rollback()
// transCode = statuscode.EmailCaptchaInvalid
//
// return err
// }
// }
//
// // ==================== 2 新绑定的验证器校验,修改表数据为已验证 ==================== //
// if phoneNewCode.Id > 0 { // 新手机验证码修改
// err = aduserdb.UpdateVerifyFlag(tx, phoneNewCode.Id)
// if err != nil {
// log.Error("AuthSwitchBefore phoneNewCode", zap.Error(err))
//
// transCode = statuscode.PhoneCaptchaInvalid
//
// return err
// }
// }
// if emailNewCode.Id > 0 { // 新邮箱验证码修改
// err = aduserdb.UpdateVerifyFlag(tx, emailNewCode.Id)
// if err != nil {
// log.Error("AuthSwitchBefore emailNewCode", zap.Error(err))
//
// transCode = statuscode.EmailCaptchaInvalid
//
// return err
// }
// }
//
// return nil
// })
// // 开启事务
// // tx, err := dbhelper.MasterPgdb.Beginx()
// // if err != nil {
// // return statuscode.ServerError
// // }
//
// // // ==================== 1 已开通验证器校验,修改表数据为已验证 ==================== //
// // if phoneCode.Id > 0 { // 旧手机验证码修改
// // err = aduserdb.UpdateVerifyFlag(phoneCode.Id, tx)
// // if err != nil {
// // log.Error("AuthSwitchBefore phoneCode", zap.Error(err))
// // _ = tx.Rollback()
// // return statuscode.PhoneCaptchaInvalid
// // }
// // }
// // if emailCode.Id > 0 { // 旧邮箱验证码修改
// // err = aduserdb.UpdateVerifyFlag(emailCode.Id, tx)
// // if err != nil {
// // log.Error("AuthSwitchBefore emailCode", zap.Error(err))
// // _ = tx.Rollback()
// // return statuscode.EmailCaptchaInvalid
// // }
// // }
//
// // // ==================== 2 新绑定的验证器校验,修改表数据为已验证 ==================== //
// // if phoneNewCode.Id > 0 { // 新手机验证码修改
// // err = aduserdb.UpdateVerifyFlag(phoneNewCode.Id, tx)
// // if err != nil {
// // log.Error("AuthSwitchBefore phoneNewCode", zap.Error(err))
// // _ = tx.Rollback()
// // return statuscode.PhoneCaptchaInvalid
// // }
// // }
// // if emailNewCode.Id > 0 { // 新邮箱验证码修改
// // err = aduserdb.UpdateVerifyFlag(emailNewCode.Id, tx)
// // if err != nil {
// // log.Error("AuthSwitchBefore emailNewCode", zap.Error(err))
// // _ = tx.Rollback()
// // return statuscode.EmailCaptchaInvalid
// // }
// // }
// // // 提交事务
// // if err = tx.Commit(); err != nil {
// // return statuscode.ServerError
// // }
//
// if err != nil {
// return transCode
// }
// return statuscode.OK
//}
//
//// AuthSwitchNew 身份验证开关(新)
//func AuthSwitchNew(orm *gorm.DB, auth sysmodel.AuthenticatorSwitch) int {
// // 验证器-开关
// switch auth.BusinessType {
// case businesstype.OpenPhoneAuth:
// if err := aduserdb.BindPhoneAuth(orm, auth.UserID, auth.NewPhoneArea, auth.NewPhone); err != nil {
// return statuscode.ServerError
// }
// case businesstype.ChangePhoneAuth:
// if err := aduserdb.ChangePhoneAuth(orm, auth.UserID, auth.NewPhoneArea, auth.NewPhone); err != nil {
// return statuscode.ServerError
// }
// // hc todo 注释掉 先不急
// // 更新商家信息
// // merchant := merchantdb.GetMerchantInfoForUserId(orm, auth.UserID)
// // if merchant.Id > 0 {
// // _ = merchantdb.UpdateMerchantMobile(auth.UserID, auth.NewPhone)
// // }
// case businesstype.ClosePhoneAuth:
// if err := aduserdb.ClosePhoneAuth(orm, auth.UserID); err != nil {
// return statuscode.ServerError
// }
//
// case businesstype.OpenEmailAuth:
// if err := aduserdb.BindEmailAuth(orm, auth.UserID, auth.NewEmail); err != nil {
// return statuscode.ServerError
// }
// case businesstype.ChangeEmailAuth:
// if err := aduserdb.ChangeEmailAuth(orm, auth.UserID, auth.NewEmail); err != nil {
// return statuscode.ServerError
// }
// case businesstype.CloseEmailAuth:
// if err := aduserdb.CloseEmailAuth(orm, auth.UserID); err != nil {
// return statuscode.ServerError
// }
//
// case businesstype.OpenGoogleAuth:
// if err := aduserdb.OpenGoogleAuth(orm, auth.UserID); err != nil {
// return statuscode.ServerError
// }
// case businesstype.ChangeGoogleAuth:
// if err := aduserdb.CloseGoogleAuth(orm, auth.UserID); err != nil {
// return statuscode.ServerError
// }
// case businesstype.CloseGoogleAuth:
// if err := aduserdb.CloseGoogleAuth(orm, auth.UserID); err != nil {
// return statuscode.ServerError
// }
//
// default:
// return statuscode.ParameterInvalid
// }
//
// return statuscode.OK
//}