2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-05-12 22:09:41 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-05-12 21:38:31 +08:00
|
|
|
package controllers
|
|
|
|
|
2021-05-12 22:09:41 +08:00
|
|
|
import (
|
2023-04-06 23:06:18 +08:00
|
|
|
"encoding/json"
|
2021-09-04 22:20:47 +08:00
|
|
|
"errors"
|
2021-05-13 09:39:07 +08:00
|
|
|
"fmt"
|
2021-05-12 22:09:41 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-06-18 16:00:31 +08:00
|
|
|
"github.com/casdoor/casdoor/captcha"
|
2023-04-25 23:05:53 +08:00
|
|
|
"github.com/casdoor/casdoor/form"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2021-05-12 22:09:41 +08:00
|
|
|
)
|
2021-05-12 21:38:31 +08:00
|
|
|
|
2023-02-16 22:53:28 +08:00
|
|
|
const (
|
2023-05-05 21:23:59 +08:00
|
|
|
SignupVerification = "signup"
|
|
|
|
ResetVerification = "reset"
|
|
|
|
LoginVerification = "login"
|
|
|
|
ForgetVerification = "forget"
|
|
|
|
MfaSetupVerification = "mfaSetup"
|
|
|
|
MfaAuthVerification = "mfaAuth"
|
2023-02-16 22:53:28 +08:00
|
|
|
)
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// SendVerificationCode ...
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Title SendVerificationCode
|
|
|
|
// @Tag Verification API
|
2021-12-13 09:49:43 +08:00
|
|
|
// @router /send-verification-code [post]
|
2023-12-23 11:57:18 +08:00
|
|
|
// @Success 200 {object} object.Userinfo The Response object
|
2021-05-12 21:38:31 +08:00
|
|
|
func (c *ApiController) SendVerificationCode() {
|
2023-04-25 23:05:53 +08:00
|
|
|
var vform form.VerificationForm
|
|
|
|
err := c.ParseForm(&vform)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
2021-05-12 21:38:31 +08:00
|
|
|
return
|
|
|
|
}
|
2023-04-25 23:05:53 +08:00
|
|
|
remoteAddr := util.GetIPFromRequest(c.Ctx.Request)
|
|
|
|
|
|
|
|
if msg := vform.CheckParameter(form.SendVerifyCode, c.GetAcceptLanguage()); msg != "" {
|
|
|
|
c.ResponseError(msg)
|
2023-02-16 22:53:28 +08:00
|
|
|
return
|
|
|
|
}
|
2021-05-12 21:38:31 +08:00
|
|
|
|
2023-12-11 18:01:56 +08:00
|
|
|
provider, err := object.GetCaptchaProviderByApplication(vform.ApplicationId, "false", c.GetAcceptLanguage())
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if provider != nil {
|
|
|
|
if vform.CaptchaType != provider.Type {
|
2023-02-19 16:56:51 +08:00
|
|
|
c.ResponseError(c.T("verification:Turing test failed."))
|
|
|
|
return
|
|
|
|
}
|
2023-12-11 18:01:56 +08:00
|
|
|
|
|
|
|
if provider.Type != "Default" {
|
|
|
|
vform.ClientSecret = provider.ClientSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
if vform.CaptchaType != "none" {
|
|
|
|
if captchaProvider := captcha.GetCaptchaProvider(vform.CaptchaType); captchaProvider == nil {
|
|
|
|
c.ResponseError(c.T("general:don't support captchaProvider: ") + vform.CaptchaType)
|
|
|
|
return
|
|
|
|
} else if isHuman, err := captchaProvider.VerifyCaptcha(vform.CaptchaToken, vform.ClientSecret); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
} else if !isHuman {
|
|
|
|
c.ResponseError(c.T("verification:Turing test failed."))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
application, err := object.GetApplication(vform.ApplicationId)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
organization, err := object.GetOrganization(util.GetId(application.Owner, application.Organization))
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(c.T(err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-11-28 00:11:33 +08:00
|
|
|
if organization == nil {
|
2023-03-19 21:47:49 +08:00
|
|
|
c.ResponseError(c.T("check:Organization does not exist"))
|
2022-11-28 00:11:33 +08:00
|
|
|
return
|
|
|
|
}
|
2021-10-31 08:49:39 +08:00
|
|
|
|
2023-02-16 22:53:28 +08:00
|
|
|
var user *object.User
|
|
|
|
// checkUser != "", means method is ForgetVerification
|
2023-04-25 23:05:53 +08:00
|
|
|
if vform.CheckUser != "" {
|
2023-02-16 22:53:28 +08:00
|
|
|
owner := application.Organization
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err = object.GetUser(util.GetId(owner, vform.CheckUser))
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-01-19 22:13:02 +08:00
|
|
|
if user == nil || user.IsDeleted {
|
|
|
|
c.ResponseError(c.T("verification:the user does not exist, please sign up first"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.IsForbidden {
|
|
|
|
c.ResponseError(c.T("check:The user is forbidden to sign in, please contact the administrator"))
|
|
|
|
return
|
|
|
|
}
|
2021-12-07 00:05:53 +08:00
|
|
|
}
|
|
|
|
|
2023-07-07 12:30:07 +08:00
|
|
|
// mfaUserSession != "", means method is MfaAuthVerification
|
|
|
|
if mfaUserSession := c.getMfaUserSession(); mfaUserSession != "" {
|
|
|
|
user, err = object.GetUser(mfaUserSession)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-05-05 21:23:59 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
sendResp := errors.New("invalid dest type")
|
2022-03-15 12:54:57 +08:00
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
switch vform.Type {
|
2023-04-06 23:06:18 +08:00
|
|
|
case object.VerifyTypeEmail:
|
2023-04-25 23:05:53 +08:00
|
|
|
if !util.IsEmailValid(vform.Dest) {
|
2023-03-19 21:47:49 +08:00
|
|
|
c.ResponseError(c.T("check:Email is invalid"))
|
2021-05-13 09:55:37 +08:00
|
|
|
return
|
|
|
|
}
|
2021-05-24 01:02:38 +08:00
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
if vform.Method == LoginVerification || vform.Method == ForgetVerification {
|
|
|
|
if user != nil && util.GetMaskedEmail(user.Email) == vform.Dest {
|
|
|
|
vform.Dest = user.Email
|
2023-02-16 22:53:28 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err = object.GetUserByEmail(organization.Name, vform.Dest)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:53:28 +08:00
|
|
|
if user == nil {
|
|
|
|
c.ResponseError(c.T("verification:the user does not exist, please sign up first"))
|
|
|
|
return
|
|
|
|
}
|
2023-04-25 23:05:53 +08:00
|
|
|
} else if vform.Method == ResetVerification {
|
2023-02-16 22:53:28 +08:00
|
|
|
user = c.getCurrentUser()
|
2023-05-05 21:23:59 +08:00
|
|
|
} else if vform.Method == MfaAuthVerification {
|
2023-06-21 18:56:37 +08:00
|
|
|
mfaProps := user.GetPreferredMfaProps(false)
|
2023-05-05 21:23:59 +08:00
|
|
|
if user != nil && util.GetMaskedEmail(mfaProps.Secret) == vform.Dest {
|
|
|
|
vform.Dest = mfaProps.Secret
|
|
|
|
}
|
2023-07-07 12:30:07 +08:00
|
|
|
} else if vform.Method == MfaSetupVerification {
|
2024-02-06 20:17:59 +08:00
|
|
|
c.SetSession(MfaDestSession, vform.Dest)
|
2022-11-28 00:11:33 +08:00
|
|
|
}
|
|
|
|
|
2024-02-23 00:09:37 +08:00
|
|
|
provider, err = application.GetEmailProvider(vform.Method)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-10-18 15:32:12 +08:00
|
|
|
if provider == nil {
|
2024-02-16 01:13:34 +08:00
|
|
|
c.ResponseError(fmt.Sprintf(c.T("verification:please add an Email provider to the \"Providers\" list for the application: %s"), application.Name))
|
2023-10-18 15:32:12 +08:00
|
|
|
return
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
sendResp = object.SendVerificationCodeToEmail(organization, user, provider, remoteAddr, vform.Dest)
|
2023-04-06 23:06:18 +08:00
|
|
|
case object.VerifyTypePhone:
|
2023-04-25 23:05:53 +08:00
|
|
|
if vform.Method == LoginVerification || vform.Method == ForgetVerification {
|
|
|
|
if user != nil && util.GetMaskedPhone(user.Phone) == vform.Dest {
|
|
|
|
vform.Dest = user.Phone
|
2023-02-16 22:53:28 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
if user, err = object.GetUserByPhone(organization.Name, vform.Dest); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
} else if user == nil {
|
2023-02-16 22:53:28 +08:00
|
|
|
c.ResponseError(c.T("verification:the user does not exist, please sign up first"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
vform.CountryCode = user.GetCountryCode(vform.CountryCode)
|
2023-06-21 18:56:37 +08:00
|
|
|
} else if vform.Method == ResetVerification || vform.Method == MfaSetupVerification {
|
|
|
|
if vform.CountryCode == "" {
|
|
|
|
if user = c.getCurrentUser(); user != nil {
|
|
|
|
vform.CountryCode = user.GetCountryCode(vform.CountryCode)
|
|
|
|
}
|
2023-02-16 22:53:28 +08:00
|
|
|
}
|
2023-07-07 12:30:07 +08:00
|
|
|
|
|
|
|
if vform.Method == MfaSetupVerification {
|
2024-02-06 20:17:59 +08:00
|
|
|
c.SetSession(MfaCountryCodeSession, vform.CountryCode)
|
|
|
|
c.SetSession(MfaDestSession, vform.Dest)
|
2023-07-07 12:30:07 +08:00
|
|
|
}
|
2023-05-05 21:23:59 +08:00
|
|
|
} else if vform.Method == MfaAuthVerification {
|
2023-06-21 18:56:37 +08:00
|
|
|
mfaProps := user.GetPreferredMfaProps(false)
|
2023-05-05 21:23:59 +08:00
|
|
|
if user != nil && util.GetMaskedPhone(mfaProps.Secret) == vform.Dest {
|
|
|
|
vform.Dest = mfaProps.Secret
|
|
|
|
}
|
|
|
|
|
|
|
|
vform.CountryCode = mfaProps.CountryCode
|
2021-05-13 09:55:37 +08:00
|
|
|
}
|
2022-11-28 00:11:33 +08:00
|
|
|
|
2024-02-23 00:09:37 +08:00
|
|
|
provider, err = application.GetSmsProvider(vform.Method)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-10-18 15:32:12 +08:00
|
|
|
if provider == nil {
|
2024-02-16 01:13:34 +08:00
|
|
|
c.ResponseError(fmt.Sprintf(c.T("verification:please add a SMS provider to the \"Providers\" list for the application: %s"), application.Name))
|
2023-10-18 15:32:12 +08:00
|
|
|
return
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
if phone, ok := util.GetE164Number(vform.Dest, vform.CountryCode); !ok {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("verification:Phone number is invalid in your region %s"), vform.CountryCode))
|
2021-05-18 20:11:03 +08:00
|
|
|
return
|
2023-02-16 22:53:28 +08:00
|
|
|
} else {
|
|
|
|
sendResp = object.SendVerificationCodeToPhone(organization, user, provider, remoteAddr, phone)
|
2021-05-13 09:39:07 +08:00
|
|
|
}
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 22:20:47 +08:00
|
|
|
if sendResp != nil {
|
2023-02-16 22:53:28 +08:00
|
|
|
c.ResponseError(sendResp.Error())
|
2021-10-31 08:49:39 +08:00
|
|
|
} else {
|
2023-02-16 22:53:28 +08:00
|
|
|
c.ResponseOk()
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
// VerifyCaptcha ...
|
|
|
|
// @Title VerifyCaptcha
|
|
|
|
// @Tag Verification API
|
|
|
|
// @router /verify-captcha [post]
|
2023-12-23 11:57:18 +08:00
|
|
|
// @Success 200 {object} object.Userinfo The Response object
|
2023-04-25 23:05:53 +08:00
|
|
|
func (c *ApiController) VerifyCaptcha() {
|
|
|
|
var vform form.VerificationForm
|
|
|
|
err := c.ParseForm(&vform)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg := vform.CheckParameter(form.VerifyCaptcha, c.GetAcceptLanguage()); msg != "" {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-11 18:01:56 +08:00
|
|
|
captchaProvider, err := object.GetCaptchaProviderByOwnerName(vform.ApplicationId, c.GetAcceptLanguage())
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if captchaProvider.Type != "Default" {
|
|
|
|
vform.ClientSecret = captchaProvider.ClientSecret
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
provider := captcha.GetCaptchaProvider(vform.CaptchaType)
|
|
|
|
if provider == nil {
|
|
|
|
c.ResponseError(c.T("verification:Invalid captcha provider."))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid, err := provider.VerifyCaptcha(vform.CaptchaToken, vform.ClientSecret)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ResponseOk(isValid)
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// ResetEmailOrPhone ...
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title ResetEmailOrPhone
|
2024-01-15 23:27:42 +08:00
|
|
|
// @router /reset-email-or-phone [post]
|
2023-12-23 11:57:18 +08:00
|
|
|
// @Success 200 {object} object.Userinfo The Response object
|
2021-05-12 21:38:31 +08:00
|
|
|
func (c *ApiController) ResetEmailOrPhone() {
|
2022-09-18 15:43:49 +08:00
|
|
|
user, ok := c.RequireSignedInUser()
|
2021-05-17 23:25:28 +08:00
|
|
|
if !ok {
|
2021-05-12 21:38:31 +08:00
|
|
|
return
|
|
|
|
}
|
2021-05-17 23:25:28 +08:00
|
|
|
|
2021-05-12 21:38:31 +08:00
|
|
|
destType := c.Ctx.Request.Form.Get("type")
|
|
|
|
dest := c.Ctx.Request.Form.Get("dest")
|
|
|
|
code := c.Ctx.Request.Form.Get("code")
|
2023-02-16 22:53:28 +08:00
|
|
|
|
2023-02-18 09:31:58 +08:00
|
|
|
if util.IsStringsEmpty(destType, dest, code) {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
2021-05-12 21:38:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:39:07 +08:00
|
|
|
checkDest := dest
|
2023-05-30 15:49:39 +08:00
|
|
|
organization, err := object.GetOrganizationByUser(user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(c.T(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-06 23:06:18 +08:00
|
|
|
if destType == object.VerifyTypePhone {
|
2023-02-23 18:06:13 +08:00
|
|
|
if object.HasUserByField(user.Owner, "phone", dest) {
|
2023-01-10 22:34:08 +08:00
|
|
|
c.ResponseError(c.T("check:Phone already exists"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
phoneItem := object.GetAccountItemByName("Phone", organization)
|
2022-07-30 18:17:13 +08:00
|
|
|
if phoneItem == nil {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(c.T("verification:Unable to get the phone modify rule."))
|
2022-07-30 18:17:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
if pass, errMsg := object.CheckAccountItemModifyRule(phoneItem, user.IsAdminUser(), c.GetAcceptLanguage()); !pass {
|
2022-07-30 18:17:13 +08:00
|
|
|
c.ResponseError(errMsg)
|
|
|
|
return
|
|
|
|
}
|
2023-02-16 22:53:28 +08:00
|
|
|
if checkDest, ok = util.GetE164Number(dest, user.GetCountryCode("")); !ok {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("verification:Phone number is invalid in your region %s"), user.CountryCode))
|
|
|
|
return
|
2021-05-13 09:39:07 +08:00
|
|
|
}
|
2023-04-06 23:06:18 +08:00
|
|
|
} else if destType == object.VerifyTypeEmail {
|
2023-02-23 18:06:13 +08:00
|
|
|
if object.HasUserByField(user.Owner, "email", dest) {
|
2023-01-10 22:34:08 +08:00
|
|
|
c.ResponseError(c.T("check:Email already exists"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
emailItem := object.GetAccountItemByName("Email", organization)
|
2022-07-30 18:17:13 +08:00
|
|
|
if emailItem == nil {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(c.T("verification:Unable to get the email modify rule."))
|
2022-07-30 18:17:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
if pass, errMsg := object.CheckAccountItemModifyRule(emailItem, user.IsAdminUser(), c.GetAcceptLanguage()); !pass {
|
2022-07-30 18:17:13 +08:00
|
|
|
c.ResponseError(errMsg)
|
|
|
|
return
|
|
|
|
}
|
2021-05-13 09:39:07 +08:00
|
|
|
}
|
2023-04-25 23:05:53 +08:00
|
|
|
|
2024-02-16 08:53:56 +08:00
|
|
|
result, err := object.CheckVerificationCode(checkDest, code, c.GetAcceptLanguage())
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(c.T(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if result.Code != object.VerificationSuccess {
|
2023-03-15 23:44:38 +08:00
|
|
|
c.ResponseError(result.Msg)
|
2021-05-12 21:38:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch destType {
|
2023-04-06 23:06:18 +08:00
|
|
|
case object.VerifyTypeEmail:
|
2021-05-12 21:38:31 +08:00
|
|
|
user.Email = dest
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err = object.SetUserField(user, "email", user.Email)
|
2023-04-06 23:06:18 +08:00
|
|
|
case object.VerifyTypePhone:
|
2021-05-13 09:39:07 +08:00
|
|
|
user.Phone = dest
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err = object.SetUserField(user, "phone", user.Phone)
|
2021-05-12 21:38:31 +08:00
|
|
|
default:
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(c.T("verification:Unknown type"))
|
2021-05-12 21:38:31 +08:00
|
|
|
return
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = object.DisableVerificationCode(checkDest)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-05-12 21:38:31 +08:00
|
|
|
|
2023-02-16 22:53:28 +08:00
|
|
|
c.ResponseOk()
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
2022-06-18 16:00:31 +08:00
|
|
|
|
2023-04-06 23:06:18 +08:00
|
|
|
// VerifyCode
|
2023-04-25 23:05:53 +08:00
|
|
|
// @Tag Verification API
|
2023-04-06 23:06:18 +08:00
|
|
|
// @Title VerifyCode
|
2024-01-15 23:27:42 +08:00
|
|
|
// @router /verify-code [post]
|
2023-12-23 11:57:18 +08:00
|
|
|
// @Success 200 {object} object.Userinfo The Response object
|
2023-04-06 23:06:18 +08:00
|
|
|
func (c *ApiController) VerifyCode() {
|
2023-04-25 23:05:53 +08:00
|
|
|
var authForm form.AuthForm
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &authForm)
|
2023-04-06 23:06:18 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var user *object.User
|
2023-04-25 23:05:53 +08:00
|
|
|
if authForm.Name != "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err = object.GetUserByFields(authForm.Organization, authForm.Name)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-04-06 23:06:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var checkDest string
|
2023-04-25 23:05:53 +08:00
|
|
|
if strings.Contains(authForm.Username, "@") {
|
|
|
|
if user != nil && util.GetMaskedEmail(user.Email) == authForm.Username {
|
|
|
|
authForm.Username = user.Email
|
2023-04-06 23:06:18 +08:00
|
|
|
}
|
2023-04-25 23:05:53 +08:00
|
|
|
checkDest = authForm.Username
|
2023-04-06 23:06:18 +08:00
|
|
|
} else {
|
2023-04-25 23:05:53 +08:00
|
|
|
if user != nil && util.GetMaskedPhone(user.Phone) == authForm.Username {
|
|
|
|
authForm.Username = user.Phone
|
2023-04-06 23:06:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
if user, err = object.GetUserByFields(authForm.Organization, authForm.Username); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
} else if user == nil {
|
2023-04-25 23:05:53 +08:00
|
|
|
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), util.GetId(authForm.Organization, authForm.Username)))
|
2023-04-06 23:06:18 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
verificationCodeType := object.GetVerifyType(authForm.Username)
|
2023-04-06 23:06:18 +08:00
|
|
|
if verificationCodeType == object.VerifyTypePhone {
|
2023-04-25 23:05:53 +08:00
|
|
|
authForm.CountryCode = user.GetCountryCode(authForm.CountryCode)
|
2023-04-06 23:06:18 +08:00
|
|
|
var ok bool
|
2023-04-25 23:05:53 +08:00
|
|
|
if checkDest, ok = util.GetE164Number(authForm.Username, authForm.CountryCode); !ok {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("verification:Phone number is invalid in your region %s"), authForm.CountryCode))
|
2023-04-06 23:06:18 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-16 08:53:56 +08:00
|
|
|
result, err := object.CheckVerificationCode(checkDest, authForm.Code, c.GetAcceptLanguage())
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(c.T(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if result.Code != object.VerificationSuccess {
|
2023-04-06 23:06:18 +08:00
|
|
|
c.ResponseError(result.Msg)
|
|
|
|
return
|
|
|
|
}
|
2024-02-16 08:53:56 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.DisableVerificationCode(checkDest)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-04-06 23:06:18 +08:00
|
|
|
|
2024-02-16 08:53:56 +08:00
|
|
|
c.SetSession("verifiedCode", authForm.Code)
|
2023-04-06 23:06:18 +08:00
|
|
|
c.ResponseOk()
|
|
|
|
}
|