2021-07-18 15:26:29 +05:30
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-07-21 13:36:26 +05:30
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-07-18 15:26:29 +05:30
|
|
|
|
2021-07-28 15:22:11 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2022-01-21 13:34:04 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-01-17 11:32:13 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/email"
|
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-01-23 01:24:41 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-18 15:26:29 +05:30
|
|
|
)
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
// ForgotPasswordResolver is a resolver for forgot password mutation
|
|
|
|
func ForgotPasswordResolver(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) {
|
2021-08-04 12:18:57 +05:30
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2021-07-18 15:26:29 +05:30
|
|
|
var res *model.Response
|
2021-08-04 12:18:57 +05:30
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-02-28 07:55:01 +05:30
|
|
|
if envstore.EnvStoreObj.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication) {
|
2021-07-28 15:22:11 +05:30
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
2021-07-21 13:36:26 +05:30
|
|
|
params.Email = strings.ToLower(params.Email)
|
2021-07-18 15:26:29 +05:30
|
|
|
|
2021-07-21 13:36:26 +05:30
|
|
|
if !utils.IsValidEmail(params.Email) {
|
|
|
|
return res, fmt.Errorf("invalid email")
|
2021-07-18 15:26:29 +05:30
|
|
|
}
|
|
|
|
|
2022-01-21 13:34:04 +05:30
|
|
|
_, err = db.Provider.GetUserByEmail(params.Email)
|
2021-07-18 15:26:29 +05:30
|
|
|
if err != nil {
|
2021-07-21 13:36:26 +05:30
|
|
|
return res, fmt.Errorf(`user with this email not found`)
|
2021-07-18 15:26:29 +05:30
|
|
|
}
|
|
|
|
|
2022-01-31 11:35:24 +05:30
|
|
|
hostname := utils.GetHost(gc)
|
2022-03-08 12:36:26 +05:30
|
|
|
_, nonceHash, err := utils.GenerateNonce()
|
2022-03-02 17:42:31 +05:30
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-03-15 08:53:48 +05:30
|
|
|
redirectURL := utils.GetAppURL(gc) + "/reset-password"
|
2022-03-08 12:36:26 +05:30
|
|
|
if params.RedirectURI != nil {
|
|
|
|
redirectURL = *params.RedirectURI
|
|
|
|
}
|
|
|
|
|
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, constants.VerificationTypeForgotPassword, hostname, nonceHash, redirectURL)
|
2021-07-18 15:26:29 +05:30
|
|
|
if err != nil {
|
2021-12-17 21:25:07 +05:30
|
|
|
log.Println(`error generating token`, err)
|
2021-07-18 15:26:29 +05:30
|
|
|
}
|
2022-01-21 13:34:04 +05:30
|
|
|
db.Provider.AddVerificationRequest(models.VerificationRequest{
|
2022-03-08 12:36:26 +05:30
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: constants.VerificationTypeForgotPassword,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: redirectURL,
|
2021-07-21 13:36:26 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
// exec it as go routin so that we can reduce the api latency
|
2022-03-02 17:42:31 +05:30
|
|
|
go email.SendForgotPasswordMail(params.Email, verificationToken, hostname)
|
2021-07-18 15:26:29 +05:30
|
|
|
|
|
|
|
res = &model.Response{
|
2021-07-21 13:36:26 +05:30
|
|
|
Message: `Please check your inbox! We have sent a password reset link.`,
|
2021-07-18 15:26:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|