2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-06-17 11:55:06 +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.
package object
2022-08-17 01:39:53 +08:00
import (
"fmt"
"regexp"
"time"
2022-12-07 13:13:23 +08:00
"github.com/casdoor/casdoor/i18n"
2022-08-17 01:39:53 +08:00
)
2021-06-17 11:55:06 +08:00
2022-02-27 13:44:44 +08:00
var reRealName * regexp . Regexp
2021-06-17 11:55:06 +08:00
func init ( ) {
var err error
2022-02-27 13:44:44 +08:00
reRealName , err = regexp . Compile ( "^[\u4E00-\u9FA5]{2,3}(?:·[\u4E00-\u9FA5]{2,3})*$" )
2021-06-17 11:55:06 +08:00
if err != nil {
panic ( err )
}
}
2022-02-27 13:44:44 +08:00
func isValidRealName ( s string ) bool {
return reRealName . MatchString ( s )
2021-06-17 11:55:06 +08:00
}
2022-08-17 01:39:53 +08:00
2023-11-19 19:58:07 +08:00
func resetUserSigninErrorTimes ( user * User ) error {
2022-08-17 01:39:53 +08:00
// if the password is correct and wrong times is not zero, reset the error times
if user . SigninWrongTimes == 0 {
2023-11-19 19:58:07 +08:00
return nil
2022-08-17 01:39:53 +08:00
}
2023-11-19 19:58:07 +08:00
2022-08-17 01:39:53 +08:00
user . SigninWrongTimes = 0
2023-11-19 19:58:07 +08:00
_ , err := UpdateUser ( user . GetId ( ) , user , [ ] string { "signin_wrong_times" , "last_signin_wrong_time" } , false )
return err
2022-08-17 01:39:53 +08:00
}
2023-12-20 22:29:53 +08:00
func GetFailedSigninConfigByUser ( user * User ) ( int , int , error ) {
application , err := GetApplicationByUser ( user )
if err != nil {
return 0 , 0 , err
}
2024-08-10 09:31:46 +08:00
if application == nil {
return 0 , 0 , fmt . Errorf ( "the application for user %s is not found" , user . GetId ( ) )
}
2023-12-20 22:29:53 +08:00
2024-01-13 02:09:18 +08:00
failedSigninLimit := application . FailedSigninLimit
2023-12-20 22:29:53 +08:00
if failedSigninLimit == 0 {
failedSigninLimit = DefaultFailedSigninLimit
}
2024-01-13 02:09:18 +08:00
2024-01-13 02:12:29 +08:00
failedSigninFrozenTime := application . FailedSigninFrozenTime
if failedSigninFrozenTime == 0 {
failedSigninFrozenTime = DefaultFailedSigninFrozenTime
2023-12-20 22:29:53 +08:00
}
2024-01-13 02:12:29 +08:00
return failedSigninLimit , failedSigninFrozenTime , nil
2023-12-20 22:29:53 +08:00
}
2023-11-19 19:58:07 +08:00
func recordSigninErrorInfo ( user * User , lang string , options ... bool ) error {
2023-04-22 16:16:25 +08:00
enableCaptcha := false
if len ( options ) > 0 {
enableCaptcha = options [ 0 ]
}
2023-11-19 19:58:07 +08:00
2024-01-13 02:12:29 +08:00
failedSigninLimit , failedSigninFrozenTime , errSignin := GetFailedSigninConfigByUser ( user )
2023-12-20 22:29:53 +08:00
if errSignin != nil {
return errSignin
}
2022-08-17 01:39:53 +08:00
// increase failed login count
2023-12-20 22:29:53 +08:00
if user . SigninWrongTimes < failedSigninLimit {
2023-04-22 16:16:25 +08:00
user . SigninWrongTimes ++
}
2022-08-17 01:39:53 +08:00
2023-12-20 22:29:53 +08:00
if user . SigninWrongTimes >= failedSigninLimit {
2022-08-17 01:39:53 +08:00
// record the latest failed login time
user . LastSigninWrongTime = time . Now ( ) . UTC ( ) . Format ( time . RFC3339 )
}
// update user
2023-11-19 19:58:07 +08:00
_ , err := UpdateUser ( user . GetId ( ) , user , [ ] string { "signin_wrong_times" , "last_signin_wrong_time" } , false )
if err != nil {
return err
}
2023-12-20 22:29:53 +08:00
leftChances := failedSigninLimit - user . SigninWrongTimes
2023-04-22 16:16:25 +08:00
if leftChances == 0 && enableCaptcha {
2023-11-19 19:58:07 +08:00
return fmt . Errorf ( i18n . Translate ( lang , "check:password or code is incorrect" ) )
2023-04-22 16:16:25 +08:00
} else if leftChances >= 0 {
2023-11-19 19:58:07 +08:00
return fmt . Errorf ( i18n . Translate ( lang , "check:password or code is incorrect, you have %d remaining chances" ) , leftChances )
2022-08-17 01:39:53 +08:00
}
2023-11-19 19:58:07 +08:00
2022-08-17 01:39:53 +08:00
// don't show the chance error message if the user has no chance left
2024-01-13 02:12:29 +08:00
return fmt . Errorf ( i18n . Translate ( lang , "check:You have entered the wrong password or code too many times, please wait for %d minutes and try again" ) , failedSigninFrozenTime )
2022-08-17 01:39:53 +08:00
}