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"
|
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 (
|
|
|
|
SignupVerification = "signup"
|
|
|
|
ResetVerification = "reset"
|
|
|
|
LoginVerification = "login"
|
|
|
|
ForgetVerification = "forget"
|
|
|
|
)
|
|
|
|
|
2021-05-24 01:18:21 +08:00
|
|
|
func (c *ApiController) getCurrentUser() *object.User {
|
|
|
|
var user *object.User
|
2021-07-18 07:15:22 +08:00
|
|
|
userId := c.GetSessionUsername()
|
2021-05-24 01:18:21 +08:00
|
|
|
if userId == "" {
|
|
|
|
user = nil
|
|
|
|
} else {
|
|
|
|
user = object.GetUser(userId)
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
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]
|
2021-05-12 21:38:31 +08:00
|
|
|
func (c *ApiController) SendVerificationCode() {
|
|
|
|
destType := c.Ctx.Request.Form.Get("type")
|
|
|
|
dest := c.Ctx.Request.Form.Get("dest")
|
2023-02-16 22:53:28 +08:00
|
|
|
countryCode := c.Ctx.Request.Form.Get("countryCode")
|
2021-05-22 20:57:55 +08:00
|
|
|
checkType := c.Ctx.Request.Form.Get("checkType")
|
2023-03-05 20:31:46 +08:00
|
|
|
clientSecret := c.Ctx.Request.Form.Get("clientSecret")
|
|
|
|
captchaToken := c.Ctx.Request.Form.Get("captchaToken")
|
2022-07-10 00:40:52 +08:00
|
|
|
applicationId := c.Ctx.Request.Form.Get("applicationId")
|
2022-12-11 15:52:36 +08:00
|
|
|
method := c.Ctx.Request.Form.Get("method")
|
2023-02-16 22:53:28 +08:00
|
|
|
checkUser := c.Ctx.Request.Form.Get("checkUser")
|
2021-08-03 22:18:59 +08:00
|
|
|
remoteAddr := util.GetIPFromRequest(c.Ctx.Request)
|
2021-05-12 21:38:31 +08:00
|
|
|
|
2022-07-25 23:46:38 +08:00
|
|
|
if dest == "" {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": dest.")
|
2022-07-25 23:46:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if applicationId == "" {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": applicationId.")
|
2022-07-25 23:46:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if checkType == "" {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": checkType.")
|
2021-05-12 21:38:31 +08:00
|
|
|
return
|
|
|
|
}
|
2023-02-16 22:53:28 +08:00
|
|
|
if !strings.Contains(applicationId, "/") {
|
|
|
|
c.ResponseError(c.T("verification:Wrong parameter") + ": applicationId.")
|
|
|
|
return
|
|
|
|
}
|
2021-05-12 21:38:31 +08:00
|
|
|
|
2023-02-19 16:56:51 +08:00
|
|
|
if checkType != "none" {
|
2023-03-05 20:31:46 +08:00
|
|
|
if captchaToken == "" {
|
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": captchaToken.")
|
2023-02-19 16:56:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if captchaProvider := captcha.GetCaptchaProvider(checkType); captchaProvider == nil {
|
|
|
|
c.ResponseError(c.T("general:don't support captchaProvider: ") + checkType)
|
|
|
|
return
|
2023-03-05 20:31:46 +08:00
|
|
|
} else if isHuman, err := captchaProvider.VerifyCaptcha(captchaToken, clientSecret); err != nil {
|
2023-02-19 16:56:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-10 00:40:52 +08:00
|
|
|
application := object.GetApplication(applicationId)
|
2023-02-16 22:53:28 +08:00
|
|
|
organization := object.GetOrganization(util.GetId(application.Owner, application.Organization))
|
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
|
|
|
|
if checkUser != "" {
|
|
|
|
owner := application.Organization
|
|
|
|
user = object.GetUser(util.GetId(owner, checkUser))
|
2021-12-07 00:05:53 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
sendResp := errors.New("invalid dest type")
|
2022-03-15 12:54:57 +08:00
|
|
|
|
2021-05-12 21:38:31 +08:00
|
|
|
switch destType {
|
2023-04-06 23:06:18 +08:00
|
|
|
case object.VerifyTypeEmail:
|
2021-05-13 09:55:37 +08:00
|
|
|
if !util.IsEmailValid(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-02-16 22:53:28 +08:00
|
|
|
if method == LoginVerification || method == ForgetVerification {
|
|
|
|
if user != nil && util.GetMaskedEmail(user.Email) == dest {
|
|
|
|
dest = user.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
user = object.GetUserByEmail(organization.Name, dest)
|
|
|
|
if user == nil {
|
|
|
|
c.ResponseError(c.T("verification:the user does not exist, please sign up first"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if method == ResetVerification {
|
|
|
|
user = c.getCurrentUser()
|
2022-11-28 00:11:33 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 01:02:38 +08:00
|
|
|
provider := application.GetEmailProvider()
|
2021-09-04 22:20:47 +08:00
|
|
|
sendResp = object.SendVerificationCodeToEmail(organization, user, provider, remoteAddr, dest)
|
2023-04-06 23:06:18 +08:00
|
|
|
case object.VerifyTypePhone:
|
2023-02-16 22:53:28 +08:00
|
|
|
if method == LoginVerification || method == ForgetVerification {
|
|
|
|
if user != nil && util.GetMaskedPhone(user.Phone) == dest {
|
|
|
|
dest = user.Phone
|
|
|
|
}
|
|
|
|
|
|
|
|
if user = object.GetUserByPhone(organization.Name, dest); user == nil {
|
|
|
|
c.ResponseError(c.T("verification:the user does not exist, please sign up first"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
countryCode = user.GetCountryCode(countryCode)
|
|
|
|
} else if method == ResetVerification {
|
|
|
|
if user = c.getCurrentUser(); user != nil {
|
|
|
|
countryCode = user.GetCountryCode(countryCode)
|
|
|
|
}
|
2021-05-13 09:55:37 +08:00
|
|
|
}
|
2022-11-28 00:11:33 +08:00
|
|
|
|
2023-02-16 22:53:28 +08:00
|
|
|
provider := application.GetSmsProvider()
|
|
|
|
if phone, ok := util.GetE164Number(dest, countryCode); !ok {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("verification:Phone number is invalid in your region %s"), 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// ResetEmailOrPhone ...
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
|
|
|
// @Title ResetEmailOrPhone
|
|
|
|
// @router /api/reset-email-or-phone [post]
|
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-01-10 22:34:08 +08:00
|
|
|
organization := object.GetOrganizationByUser(user)
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-23 15:16:24 +08:00
|
|
|
if pass, errMsg := object.CheckAccountItemModifyRule(phoneItem, user, 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
|
|
|
|
}
|
|
|
|
|
2022-10-23 15:16:24 +08:00
|
|
|
if pass, errMsg := object.CheckAccountItemModifyRule(emailItem, user, c.GetAcceptLanguage()); !pass {
|
2022-07-30 18:17:13 +08:00
|
|
|
c.ResponseError(errMsg)
|
|
|
|
return
|
|
|
|
}
|
2021-05-13 09:39:07 +08:00
|
|
|
}
|
2023-03-15 23:44:38 +08:00
|
|
|
if result := object.CheckVerificationCode(checkDest, code, c.GetAcceptLanguage()); result.Code != object.VerificationSuccess {
|
|
|
|
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
|
|
|
|
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
|
2021-05-12 22:09:41 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:11:03 +08:00
|
|
|
object.DisableVerificationCode(checkDest)
|
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
|
|
|
|
// @Tag Account API
|
|
|
|
// @Title VerifyCode
|
|
|
|
// @router /api/verify-code [post]
|
|
|
|
func (c *ApiController) VerifyCode() {
|
|
|
|
var form RequestForm
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var user *object.User
|
|
|
|
if form.Name != "" {
|
|
|
|
user = object.GetUserByFields(form.Organization, form.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkDest string
|
|
|
|
if strings.Contains(form.Username, "@") {
|
|
|
|
if user != nil && util.GetMaskedEmail(user.Email) == form.Username {
|
|
|
|
form.Username = user.Email
|
|
|
|
}
|
|
|
|
checkDest = form.Username
|
|
|
|
} else {
|
|
|
|
if user != nil && util.GetMaskedPhone(user.Phone) == form.Username {
|
|
|
|
form.Username = user.Phone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if user = object.GetUserByFields(form.Organization, form.Username); user == nil {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), util.GetId(form.Organization, form.Username)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
verificationCodeType := object.GetVerifyType(form.Username)
|
|
|
|
if verificationCodeType == object.VerifyTypePhone {
|
|
|
|
form.CountryCode = user.GetCountryCode(form.CountryCode)
|
|
|
|
var ok bool
|
|
|
|
if checkDest, ok = util.GetE164Number(form.Username, form.CountryCode); !ok {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("verification:Phone number is invalid in your region %s"), form.CountryCode))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if result := object.CheckVerificationCode(checkDest, form.Code, c.GetAcceptLanguage()); result.Code != object.VerificationSuccess {
|
|
|
|
c.ResponseError(result.Msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
object.DisableVerificationCode(checkDest)
|
|
|
|
c.SetSession("verifiedCode", form.Code)
|
|
|
|
|
|
|
|
c.ResponseOk()
|
|
|
|
}
|
|
|
|
|
2022-06-18 16:00:31 +08:00
|
|
|
// VerifyCaptcha ...
|
|
|
|
// @Title VerifyCaptcha
|
|
|
|
// @Tag Verification API
|
|
|
|
// @router /verify-captcha [post]
|
|
|
|
func (c *ApiController) VerifyCaptcha() {
|
|
|
|
captchaType := c.Ctx.Request.Form.Get("captchaType")
|
|
|
|
|
|
|
|
captchaToken := c.Ctx.Request.Form.Get("captchaToken")
|
|
|
|
clientSecret := c.Ctx.Request.Form.Get("clientSecret")
|
|
|
|
if captchaToken == "" {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": captchaToken.")
|
2022-06-18 16:00:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if clientSecret == "" {
|
2023-01-17 22:57:05 +08:00
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": clientSecret.")
|
2022-06-18 16:00:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := captcha.GetCaptchaProvider(captchaType)
|
|
|
|
if provider == nil {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(c.T("verification:Invalid captcha provider."))
|
2022-06-18 16:00:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid, err := provider.VerifyCaptcha(captchaToken, clientSecret)
|
|
|
|
if err != nil {
|
2022-06-26 22:09:57 +08:00
|
|
|
c.ResponseError(err.Error())
|
2022-06-18 16:00:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ResponseOk(isValid)
|
|
|
|
}
|