2021-07-18 09:56:29 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-07-21 08:06:26 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2023-12-21 19:56:14 +00:00
|
|
|
"github.com/google/uuid"
|
2022-05-24 07:12:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-07-28 09:52:11 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2023-12-21 19:56:14 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/cookie"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2022-01-21 08:04:04 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2023-12-03 17:19:40 +00:00
|
|
|
mailService "github.com/authorizerdev/authorizer/server/email"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-05-30 03:49:55 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-05-30 06:24:16 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/parsers"
|
2022-08-13 06:04:24 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
2023-12-21 19:56:14 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/smsproviders"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-18 09:56:29 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// ForgotPasswordResolver is a resolver for forgot password mutation
|
2023-12-21 19:56:14 +00:00
|
|
|
func ForgotPasswordResolver(ctx context.Context, params model.ForgotPasswordInput) (*model.ForgotPasswordResponse, error) {
|
2022-05-24 07:12:29 +00:00
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2021-08-04 06:48:57 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, err
|
2021-08-04 06:48:57 +00:00
|
|
|
}
|
2022-05-24 07:12:29 +00:00
|
|
|
|
2022-05-30 03:49:55 +00:00
|
|
|
isBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting basic auth disabled: ", err)
|
|
|
|
isBasicAuthDisabled = true
|
|
|
|
}
|
2023-12-03 17:19:40 +00:00
|
|
|
isEmailVerificationDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableEmailVerification)
|
|
|
|
if err != nil {
|
2023-12-21 19:56:14 +00:00
|
|
|
log.Debug("Error getting email verification disabled: ", err)
|
2023-12-03 17:19:40 +00:00
|
|
|
isEmailVerificationDisabled = true
|
2021-07-28 09:52:11 +00:00
|
|
|
}
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2023-12-03 17:19:40 +00:00
|
|
|
isMobileBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableMobileBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting mobile basic auth disabled: ", err)
|
|
|
|
isMobileBasicAuthDisabled = true
|
|
|
|
}
|
|
|
|
isMobileVerificationDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisablePhoneVerification)
|
|
|
|
if err != nil {
|
2023-12-21 19:56:14 +00:00
|
|
|
log.Debug("Error getting mobile verification disabled: ", err)
|
2023-12-03 17:19:40 +00:00
|
|
|
isMobileVerificationDisabled = true
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-03 17:19:40 +00:00
|
|
|
email := refs.StringValue(params.Email)
|
|
|
|
phoneNumber := refs.StringValue(params.PhoneNumber)
|
|
|
|
if email == "" && phoneNumber == "" {
|
|
|
|
log.Debug("Email or phone number is required")
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, fmt.Errorf(`email or phone number is required`)
|
2023-12-03 17:19:40 +00:00
|
|
|
}
|
2022-05-24 07:12:29 +00:00
|
|
|
log := log.WithFields(log.Fields{
|
2023-12-03 17:19:40 +00:00
|
|
|
"email": refs.StringValue(params.Email),
|
|
|
|
"phone_number": refs.StringValue(params.PhoneNumber),
|
2022-05-24 07:12:29 +00:00
|
|
|
})
|
2023-12-03 17:19:40 +00:00
|
|
|
isEmailLogin := email != ""
|
|
|
|
isMobileLogin := phoneNumber != ""
|
|
|
|
if isBasicAuthDisabled && isEmailLogin && !isEmailVerificationDisabled {
|
|
|
|
log.Debug("Basic authentication is disabled.")
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, fmt.Errorf(`basic authentication is disabled for this instance`)
|
2023-12-03 17:19:40 +00:00
|
|
|
}
|
|
|
|
if isMobileBasicAuthDisabled && isMobileLogin && !isMobileVerificationDisabled {
|
|
|
|
log.Debug("Mobile basic authentication is disabled.")
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, fmt.Errorf(`mobile basic authentication is disabled for this instance`)
|
2023-12-03 17:19:40 +00:00
|
|
|
}
|
|
|
|
var user *models.User
|
|
|
|
if isEmailLogin {
|
|
|
|
user, err = db.Provider.GetUserByEmail(ctx, email)
|
|
|
|
} else {
|
|
|
|
user, err = db.Provider.GetUserByPhoneNumber(ctx, phoneNumber)
|
|
|
|
}
|
2021-07-18 09:56:29 +00:00
|
|
|
if err != nil {
|
2023-12-03 17:19:40 +00:00
|
|
|
log.Debug("Failed to get user: ", err)
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, fmt.Errorf(`bad user credentials`)
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
2024-02-08 16:18:31 +00:00
|
|
|
|
|
|
|
if user.SignupMethods == "magic_link_login" {
|
|
|
|
user.SignupMethods = "basic_auth"
|
|
|
|
|
|
|
|
user, err = db.Provider.UpdateUser(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to update user signup method: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 06:24:16 +00:00
|
|
|
hostname := parsers.GetHost(gc)
|
2022-03-08 07:06:26 +00:00
|
|
|
_, nonceHash, err := utils.GenerateNonce()
|
2022-03-02 12:12:31 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to generate nonce: ", err)
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, err
|
2022-03-02 12:12:31 +00:00
|
|
|
}
|
2023-12-03 17:19:40 +00:00
|
|
|
if user.RevokedTimestamp != nil {
|
|
|
|
log.Debug("User access is revoked")
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, fmt.Errorf(`user access has been revoked`)
|
2023-12-03 17:19:40 +00:00
|
|
|
}
|
|
|
|
if isEmailLogin {
|
|
|
|
redirectURI := ""
|
|
|
|
// give higher preference to params redirect uri
|
|
|
|
if strings.TrimSpace(refs.StringValue(params.RedirectURI)) != "" {
|
|
|
|
redirectURI = refs.StringValue(params.RedirectURI)
|
|
|
|
} else {
|
|
|
|
redirectURI, err = memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyResetPasswordURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("ResetPasswordURL not found using default app url: ", err)
|
|
|
|
redirectURI = hostname + "/app/reset-password"
|
|
|
|
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyResetPasswordURL, redirectURI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
verificationToken, err := token.CreateVerificationToken(email, constants.VerificationTypeForgotPassword, hostname, nonceHash, redirectURI)
|
2022-10-24 06:07:42 +00:00
|
|
|
if err != nil {
|
2023-12-03 17:19:40 +00:00
|
|
|
log.Debug("Failed to create verification token", err)
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, err
|
2022-10-24 06:07:42 +00:00
|
|
|
}
|
2023-12-03 17:19:40 +00:00
|
|
|
_, err = db.Provider.AddVerificationRequest(ctx, &models.VerificationRequest{
|
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: constants.VerificationTypeForgotPassword,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: email,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add verification request", err)
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, err
|
2023-12-03 17:19:40 +00:00
|
|
|
}
|
|
|
|
// execute it as go routine so that we can reduce the api latency
|
|
|
|
go mailService.SendEmail([]string{email}, constants.VerificationTypeForgotPassword, map[string]interface{}{
|
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
|
|
|
"verification_url": utils.GetForgotPasswordURL(verificationToken, redirectURI),
|
|
|
|
})
|
2023-12-21 19:56:14 +00:00
|
|
|
return &model.ForgotPasswordResponse{
|
|
|
|
Message: `Please check your inbox! We have sent a password reset link.`,
|
|
|
|
}, nil
|
2022-03-08 07:06:26 +00:00
|
|
|
}
|
2023-12-03 17:19:40 +00:00
|
|
|
if isMobileLogin {
|
2023-12-21 19:56:14 +00:00
|
|
|
expiresAt := time.Now().Add(1 * time.Minute).Unix()
|
|
|
|
otp := utils.GenerateOTP()
|
|
|
|
otpData, err := db.Provider.UpsertOTP(ctx, &models.OTP{
|
|
|
|
Email: refs.StringValue(user.Email),
|
|
|
|
PhoneNumber: refs.StringValue(user.PhoneNumber),
|
|
|
|
Otp: otp,
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add otp: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mfaSession := uuid.NewString()
|
|
|
|
err = memorystore.Provider.SetMfaSession(user.ID, mfaSession, expiresAt)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add mfasession: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cookie.SetMfaSession(gc, mfaSession)
|
|
|
|
smsBody := strings.Builder{}
|
|
|
|
smsBody.WriteString("Your verification code is: ")
|
|
|
|
smsBody.WriteString(otpData.Otp)
|
|
|
|
if err := smsproviders.SendSMS(phoneNumber, smsBody.String()); err != nil {
|
|
|
|
log.Debug("Failed to send sms: ", err)
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
return &model.ForgotPasswordResponse{
|
|
|
|
Message: "Please enter the OTP sent to your phone number and change your password.",
|
|
|
|
ShouldShowMobileOtpScreen: refs.NewBoolRef(true),
|
|
|
|
}, nil
|
2022-05-24 07:20:33 +00:00
|
|
|
}
|
2023-12-21 19:56:14 +00:00
|
|
|
return nil, fmt.Errorf(`email or phone number verification needs to be enabled`)
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|