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-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"
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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-21 08:06:26 +00:00
|
|
|
params.Email = strings.ToLower(params.Email)
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2022-05-30 06:24:16 +00:00
|
|
|
if !validators.IsValidEmail(params.Email) {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Invalid email address: ", params.Email)
|
2021-07-21 08:06:26 +00:00
|
|
|
return res, fmt.Errorf("invalid email")
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 07:12:29 +00:00
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"email": params.Email,
|
|
|
|
})
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err = db.Provider.GetUserByEmail(ctx, params.Email)
|
2021-07-18 09:56:29 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("User not found: ", err)
|
2021-07-21 08:06:26 +00:00
|
|
|
return res, fmt.Errorf(`user with this email not found`)
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-03-02 12:12:31 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2022-05-30 06:24:16 +00:00
|
|
|
redirectURL := parsers.GetAppURL(gc) + "/reset-password"
|
2022-03-08 07:06:26 +00:00
|
|
|
if params.RedirectURI != nil {
|
|
|
|
redirectURL = *params.RedirectURI
|
|
|
|
}
|
|
|
|
|
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, constants.VerificationTypeForgotPassword, hostname, nonceHash, redirectURL)
|
2021-07-18 09:56:29 +00:00
|
|
|
if err != nil {
|
2022-05-24 07:20:33 +00:00
|
|
|
log.Debug("Failed to create verification token", err)
|
|
|
|
return res, err
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err = db.Provider.AddVerificationRequest(ctx, models.VerificationRequest{
|
2022-03-08 07:06:26 +00:00
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: constants.VerificationTypeForgotPassword,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: redirectURL,
|
2021-07-21 08:06:26 +00:00
|
|
|
})
|
2022-05-24 07:20:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add verification request", err)
|
|
|
|
return res, err
|
|
|
|
}
|
2021-07-21 08:06:26 +00:00
|
|
|
|
|
|
|
// exec it as go routin so that we can reduce the api latency
|
2022-03-02 12:12:31 +00:00
|
|
|
go email.SendForgotPasswordMail(params.Email, verificationToken, hostname)
|
2021-07-18 09:56:29 +00:00
|
|
|
|
|
|
|
res = &model.Response{
|
2021-07-21 08:06:26 +00:00
|
|
|
Message: `Please check your inbox! We have sent a password reset link.`,
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|