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 object
|
|
|
|
|
|
|
|
import (
|
2021-09-04 22:20:47 +08:00
|
|
|
"errors"
|
2021-05-12 21:38:31 +08:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2023-04-06 23:06:18 +08:00
|
|
|
"strings"
|
2021-05-12 21:38:31 +08:00
|
|
|
"time"
|
2021-05-18 19:42:03 +08:00
|
|
|
|
2022-03-20 23:21:09 +08:00
|
|
|
"github.com/casdoor/casdoor/conf"
|
2022-10-23 15:16:24 +08:00
|
|
|
"github.com/casdoor/casdoor/i18n"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2023-02-12 09:33:24 +08:00
|
|
|
"github.com/xorm-io/core"
|
2021-05-12 21:38:31 +08:00
|
|
|
)
|
|
|
|
|
2023-03-15 23:44:38 +08:00
|
|
|
type VerifyResult struct {
|
|
|
|
Code int
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:51:43 +07:00
|
|
|
const (
|
2023-03-15 23:44:38 +08:00
|
|
|
VerificationSuccess int = 0
|
|
|
|
wrongCodeError = 1
|
|
|
|
noRecordError = 2
|
|
|
|
timeoutError = 3
|
2023-01-06 17:51:43 +07:00
|
|
|
)
|
|
|
|
|
2023-04-06 23:06:18 +08:00
|
|
|
const (
|
|
|
|
VerifyTypePhone = "phone"
|
|
|
|
VerifyTypeEmail = "email"
|
|
|
|
)
|
|
|
|
|
2021-05-12 21:38:31 +08:00
|
|
|
type VerificationRecord struct {
|
2021-05-24 01:27:03 +08:00
|
|
|
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
|
|
|
|
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
|
|
|
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
|
|
|
|
|
|
|
RemoteAddr string `xorm:"varchar(100)"`
|
|
|
|
Type string `xorm:"varchar(10)"`
|
2021-05-24 01:18:21 +08:00
|
|
|
User string `xorm:"varchar(100) notnull"`
|
2021-05-24 01:02:38 +08:00
|
|
|
Provider string `xorm:"varchar(100) notnull"`
|
2021-05-12 21:38:31 +08:00
|
|
|
Receiver string `xorm:"varchar(100) notnull"`
|
|
|
|
Code string `xorm:"varchar(10) notnull"`
|
|
|
|
Time int64 `xorm:"notnull"`
|
|
|
|
IsUsed bool
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:00:48 +08:00
|
|
|
func IsAllowSend(user *User, remoteAddr, recordType string) error {
|
|
|
|
var record VerificationRecord
|
|
|
|
record.RemoteAddr = remoteAddr
|
|
|
|
record.Type = recordType
|
|
|
|
if user != nil {
|
|
|
|
record.User = user.GetId()
|
|
|
|
}
|
|
|
|
has, err := adapter.Engine.Desc("created_time").Get(&record)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if has && now-record.Time < 60 {
|
|
|
|
return errors.New("you can only send one code in 60s")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-04 22:20:47 +08:00
|
|
|
func SendVerificationCodeToEmail(organization *Organization, user *User, provider *Provider, remoteAddr string, dest string) error {
|
2021-05-24 01:02:38 +08:00
|
|
|
if provider == nil {
|
2022-08-09 16:50:49 +08:00
|
|
|
return fmt.Errorf("please set an Email provider first")
|
2021-05-24 01:02:38 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 20:21:41 +08:00
|
|
|
sender := organization.DisplayName
|
|
|
|
title := provider.Title
|
2022-09-04 12:15:07 +08:00
|
|
|
code := getRandomCode(6)
|
2021-05-24 20:21:41 +08:00
|
|
|
// "You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes."
|
|
|
|
content := fmt.Sprintf(provider.Content, code)
|
2021-05-12 21:38:31 +08:00
|
|
|
|
2022-11-13 22:00:48 +08:00
|
|
|
if err := IsAllowSend(user, remoteAddr, provider.Category); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-22 23:06:41 +08:00
|
|
|
if err := SendEmail(provider, title, content, dest, sender); err != nil {
|
2021-09-04 22:20:47 +08:00
|
|
|
return err
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:00:48 +08:00
|
|
|
if err := AddToVerificationRecord(user, provider, remoteAddr, provider.Category, dest, code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 22:20:47 +08:00
|
|
|
func SendVerificationCodeToPhone(organization *Organization, user *User, provider *Provider, remoteAddr string, dest string) error {
|
2021-05-24 01:02:38 +08:00
|
|
|
if provider == nil {
|
2022-08-09 16:50:49 +08:00
|
|
|
return errors.New("please set a SMS provider first")
|
2021-05-24 01:02:38 +08:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:00:48 +08:00
|
|
|
if err := IsAllowSend(user, remoteAddr, provider.Category); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:15:07 +08:00
|
|
|
code := getRandomCode(6)
|
2022-08-22 23:06:41 +08:00
|
|
|
if err := SendSms(provider, code, dest); err != nil {
|
2021-09-04 22:20:47 +08:00
|
|
|
return err
|
2021-05-12 22:09:41 +08:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:00:48 +08:00
|
|
|
if err := AddToVerificationRecord(user, provider, remoteAddr, provider.Category, dest, code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-05-12 22:09:41 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 22:20:47 +08:00
|
|
|
func AddToVerificationRecord(user *User, provider *Provider, remoteAddr, recordType, dest, code string) error {
|
2021-05-12 21:38:31 +08:00
|
|
|
var record VerificationRecord
|
|
|
|
record.RemoteAddr = remoteAddr
|
2021-05-18 19:42:03 +08:00
|
|
|
record.Type = recordType
|
2021-05-24 01:18:21 +08:00
|
|
|
if user != nil {
|
|
|
|
record.User = user.GetId()
|
|
|
|
}
|
2021-05-24 01:27:03 +08:00
|
|
|
record.Owner = provider.Owner
|
|
|
|
record.Name = util.GenerateId()
|
|
|
|
record.CreatedTime = util.GetCurrentTime()
|
|
|
|
|
2023-02-18 09:31:58 +08:00
|
|
|
record.Provider = provider.Name
|
2021-05-12 21:38:31 +08:00
|
|
|
record.Receiver = dest
|
|
|
|
record.Code = code
|
2022-11-13 22:00:48 +08:00
|
|
|
record.Time = time.Now().Unix()
|
2021-05-12 21:38:31 +08:00
|
|
|
record.IsUsed = false
|
|
|
|
|
2022-11-13 22:00:48 +08:00
|
|
|
_, err := adapter.Engine.Insert(record)
|
2021-05-12 21:38:31 +08:00
|
|
|
if err != nil {
|
2021-09-04 22:20:47 +08:00
|
|
|
return err
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 22:20:47 +08:00
|
|
|
return nil
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
2021-05-18 20:11:03 +08:00
|
|
|
func getVerificationRecord(dest string) *VerificationRecord {
|
2021-05-12 21:38:31 +08:00
|
|
|
var record VerificationRecord
|
|
|
|
record.Receiver = dest
|
2021-09-20 22:38:04 +08:00
|
|
|
has, err := adapter.Engine.Desc("time").Where("is_used = false").Get(&record)
|
2021-05-12 21:38:31 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !has {
|
2021-05-18 20:11:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &record
|
|
|
|
}
|
|
|
|
|
2023-03-15 23:44:38 +08:00
|
|
|
func CheckVerificationCode(dest, code, lang string) *VerifyResult {
|
2021-05-18 20:11:03 +08:00
|
|
|
record := getVerificationRecord(dest)
|
|
|
|
|
|
|
|
if record == nil {
|
2023-03-15 23:44:38 +08:00
|
|
|
return &VerifyResult{noRecordError, i18n.Translate(lang, "verification:Code has not been sent yet!")}
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
2022-03-20 23:21:09 +08:00
|
|
|
timeout, err := conf.GetConfigInt64("verificationCodeTimeout")
|
2021-05-18 20:11:03 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:38:31 +08:00
|
|
|
now := time.Now().Unix()
|
2021-05-18 20:11:03 +08:00
|
|
|
if now-record.Time > timeout*60 {
|
2023-03-15 23:44:38 +08:00
|
|
|
return &VerifyResult{timeoutError, fmt.Sprintf(i18n.Translate(lang, "verification:You should verify your code in %d min!"), timeout)}
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if record.Code != code {
|
2023-03-15 23:44:38 +08:00
|
|
|
return &VerifyResult{wrongCodeError, i18n.Translate(lang, "verification:Wrong verification code!")}
|
2021-05-12 21:38:31 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 23:44:38 +08:00
|
|
|
return &VerifyResult{VerificationSuccess, ""}
|
2021-05-18 20:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func DisableVerificationCode(dest string) {
|
|
|
|
record := getVerificationRecord(dest)
|
|
|
|
if record == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:38:31 +08:00
|
|
|
record.IsUsed = true
|
2021-05-24 01:27:03 +08:00
|
|
|
_, err := adapter.Engine.ID(core.PK{record.Owner, record.Name}).AllCols().Update(record)
|
2021-05-12 21:38:31 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:51:43 +07:00
|
|
|
func CheckSigninCode(user *User, dest, code, lang string) string {
|
|
|
|
// check the login error times
|
|
|
|
if msg := checkSigninErrorTimes(user, lang); msg != "" {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
result := CheckVerificationCode(dest, code, lang)
|
2023-03-15 23:44:38 +08:00
|
|
|
switch result.Code {
|
|
|
|
case VerificationSuccess:
|
2023-01-06 17:51:43 +07:00
|
|
|
resetUserSigninErrorTimes(user)
|
|
|
|
return ""
|
2023-03-15 23:44:38 +08:00
|
|
|
case wrongCodeError:
|
2023-01-06 17:51:43 +07:00
|
|
|
return recordSigninErrorInfo(user, lang)
|
|
|
|
default:
|
2023-03-15 23:44:38 +08:00
|
|
|
return result.Msg
|
2023-01-06 17:51:43 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 23:06:18 +08:00
|
|
|
func GetVerifyType(username string) (verificationCodeType string) {
|
|
|
|
if strings.Contains(username, "@") {
|
|
|
|
return VerifyTypeEmail
|
|
|
|
} else {
|
|
|
|
return VerifyTypeEmail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 11:42:14 +08:00
|
|
|
// From Casnode/object/validateCode.go line 116
|
2021-05-12 21:38:31 +08:00
|
|
|
var stdNums = []byte("0123456789")
|
|
|
|
|
|
|
|
func getRandomCode(length int) string {
|
|
|
|
var result []byte
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
result = append(result, stdNums[r.Intn(len(stdNums))])
|
|
|
|
}
|
|
|
|
return string(result)
|
|
|
|
}
|