73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package resolvers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
"github.com/authorizerdev/authorizer/server/db"
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
"github.com/authorizerdev/authorizer/server/email"
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
|
"github.com/authorizerdev/authorizer/server/token"
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
|
)
|
|
|
|
// ForgotPasswordResolver is a resolver for forgot password mutation
|
|
func ForgotPasswordResolver(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) {
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
var res *model.Response
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
if envstore.EnvStoreObj.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication) {
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
}
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
|
return res, fmt.Errorf("invalid email")
|
|
}
|
|
|
|
_, err = db.Provider.GetUserByEmail(params.Email)
|
|
if err != nil {
|
|
return res, fmt.Errorf(`user with this email not found`)
|
|
}
|
|
|
|
hostname := utils.GetHost(gc)
|
|
_, nonceHash, err := utils.GenerateNonce()
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
redirectURL := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAppURL)
|
|
if params.RedirectURI != nil {
|
|
redirectURL = *params.RedirectURI
|
|
}
|
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, constants.VerificationTypeForgotPassword, hostname, nonceHash, redirectURL)
|
|
if err != nil {
|
|
log.Println(`error generating token`, err)
|
|
}
|
|
db.Provider.AddVerificationRequest(models.VerificationRequest{
|
|
Token: verificationToken,
|
|
Identifier: constants.VerificationTypeForgotPassword,
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
Email: params.Email,
|
|
Nonce: nonceHash,
|
|
RedirectURI: redirectURL,
|
|
})
|
|
|
|
// exec it as go routin so that we can reduce the api latency
|
|
go email.SendForgotPasswordMail(params.Email, verificationToken, hostname)
|
|
|
|
res = &model.Response{
|
|
Message: `Please check your inbox! We have sent a password reset link.`,
|
|
}
|
|
|
|
return res, nil
|
|
}
|