2021-07-18 09:56:29 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-07-21 08:06:26 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-07-18 09:56:29 +00:00
|
|
|
|
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"
|
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
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
|
|
|
|
func ForgotPasswordResolver(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) {
|
2021-08-04 06:48:57 +00:00
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2021-07-18 09:56:29 +00:00
|
|
|
var res *model.Response
|
2021-08-04 06:48:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-01-20 11:22:37 +00:00
|
|
|
if envstore.EnvInMemoryStoreObj.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication) {
|
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
|
|
|
|
2021-07-21 08:06:26 +00:00
|
|
|
if !utils.IsValidEmail(params.Email) {
|
|
|
|
return res, fmt.Errorf("invalid email")
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 08:04:04 +00:00
|
|
|
_, err = db.Provider.GetUserByEmail(params.Email)
|
2021-07-18 09:56:29 +00:00
|
|
|
if err != nil {
|
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-01-31 06:05:24 +00:00
|
|
|
hostname := utils.GetHost(gc)
|
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, constants.VerificationTypeForgotPassword, hostname)
|
2021-07-18 09:56:29 +00:00
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println(`error generating token`, err)
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
2022-01-21 08:04:04 +00:00
|
|
|
db.Provider.AddVerificationRequest(models.VerificationRequest{
|
2022-01-22 19:54:41 +00:00
|
|
|
Token: verificationToken,
|
2022-01-17 06:02:13 +00:00
|
|
|
Identifier: constants.VerificationTypeForgotPassword,
|
2021-07-21 08:06:26 +00:00
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
})
|
|
|
|
|
|
|
|
// exec it as go routin so that we can reduce the api latency
|
|
|
|
go func() {
|
2022-01-31 06:05:24 +00:00
|
|
|
email.SendForgotPasswordMail(params.Email, verificationToken, hostname)
|
2021-07-21 08:06:26 +00:00
|
|
|
}()
|
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
|
|
|
|
}
|