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
|
|
|
|
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"
|
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"
|
2022-01-17 06:02:13 +00:00
|
|
|
"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"
|
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"
|
2022-05-30 06:24:16 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/validators"
|
2023-06-26 11:25:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/smsproviders"
|
2021-07-18 09:56:29 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// ForgotPasswordResolver is a resolver for forgot password mutation
|
|
|
|
func ForgotPasswordResolver(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) {
|
2021-07-18 09:56:29 +00:00
|
|
|
var res *model.Response
|
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)
|
2021-08-04 06:48:57 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2022-05-24 07:12:29 +00:00
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
disablePhoneVerification, _ := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisablePhoneVerification)
|
2022-05-30 03:49:55 +00:00
|
|
|
isBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication)
|
2023-06-26 11:25:13 +00:00
|
|
|
|
2022-05-30 03:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting basic auth disabled: ", err)
|
|
|
|
isBasicAuthDisabled = true
|
|
|
|
}
|
2023-06-26 11:25:13 +00:00
|
|
|
|
2022-05-30 03:49:55 +00:00
|
|
|
if isBasicAuthDisabled {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Basic authentication is disabled")
|
2021-07-28 09:52:11 +00:00
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
mobile := strings.TrimSpace(params.EmailOrPhone)
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
if !validators.IsValidEmail(params.EmailOrPhone) && len(mobile) < 10 {
|
|
|
|
log.Debug("Invalid email or phone: ", params.EmailOrPhone)
|
|
|
|
return res, fmt.Errorf("invalid email or phone")
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
if validators.IsValidEmail(params.EmailOrPhone) {
|
|
|
|
|
|
|
|
params.EmailOrPhone = strings.ToLower(params.EmailOrPhone)
|
2022-10-24 06:07:42 +00:00
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"email": params.EmailOrPhone,
|
|
|
|
})
|
|
|
|
user, err := db.Provider.GetUserByEmail(ctx, params.EmailOrPhone)
|
2022-10-24 06:07:42 +00:00
|
|
|
if err != nil {
|
2023-06-26 11:25:13 +00:00
|
|
|
log.Debug("User not found: ", err)
|
|
|
|
return res, fmt.Errorf(`user with this email not found`)
|
2022-10-24 06:07:42 +00:00
|
|
|
}
|
2022-03-08 07:06:26 +00:00
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
hostname := parsers.GetHost(gc)
|
|
|
|
_, nonceHash, err := utils.GenerateNonce()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to generate nonce: ", err)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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(params.EmailOrPhone, constants.VerificationTypeForgotPassword, hostname, nonceHash, redirectURI)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to create verification token", err)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
_, err = db.Provider.AddVerificationRequest(ctx, models.VerificationRequest{
|
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: constants.VerificationTypeForgotPassword,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.EmailOrPhone,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add verification request", err)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute it as go routine so that we can reduce the api latency
|
|
|
|
go email.SendEmail([]string{params.EmailOrPhone}, constants.VerificationTypeForgotPassword, map[string]interface{}{
|
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
|
|
|
"verification_url": utils.GetForgotPasswordURL(verificationToken, redirectURI),
|
|
|
|
})
|
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: `Please check your inbox! We have sent a password reset link.`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2022-05-24 07:20:33 +00:00
|
|
|
}
|
2021-07-21 08:06:26 +00:00
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
if !disablePhoneVerification && len(mobile) > 9 {
|
|
|
|
|
|
|
|
if _, err := db.Provider.GetUserByPhoneNumber(ctx, refs.StringValue(¶ms.EmailOrPhone)); err != nil {
|
|
|
|
return res, fmt.Errorf("user with given phone number does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
duration, _ := time.ParseDuration("10m")
|
|
|
|
smsCode := utils.GenerateOTP()
|
|
|
|
|
|
|
|
smsBody := strings.Builder{}
|
|
|
|
smsBody.WriteString("Your verification code is: ")
|
|
|
|
smsBody.WriteString(smsCode)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err = db.Provider.UpsertSMSRequest(ctx, &models.SMSVerificationRequest{
|
|
|
|
PhoneNumber: params.EmailOrPhone,
|
|
|
|
Code: smsCode,
|
|
|
|
CodeExpiresAt: time.Now().Add(duration).Unix(),
|
|
|
|
})
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2023-06-26 11:25:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to upsert sms otp: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = smsproviders.SendSMS(params.EmailOrPhone, smsBody.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to send sms: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: `verification code has been sent to your phone`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2023-06-26 11:25:13 +00:00
|
|
|
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|