2021-07-18 07:26:17 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-05-24 07:12:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
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-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 07:26:17 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// ResendVerifyEmailResolver is a resolver for resend verify email mutation
|
|
|
|
func ResendVerifyEmailResolver(ctx context.Context, params model.ResendVerifyEmailInput) (*model.Response, error) {
|
2021-07-18 07:26:17 +00:00
|
|
|
var res *model.Response
|
2022-05-24 07:12:29 +00:00
|
|
|
|
2022-01-31 06:05:24 +00:00
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
|
|
if err != nil {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Failed to get GinContext", err)
|
2022-01-31 06:05:24 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2021-07-18 07:26:17 +00:00
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Invalid email", params.Email)
|
2021-07-18 07:26:17 +00:00
|
|
|
return res, fmt.Errorf("invalid email")
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
if !utils.IsValidVerificationIdentifier(params.Identifier) {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Invalid verification identifier", params.Identifier)
|
2021-12-23 05:01:52 +00:00
|
|
|
return res, fmt.Errorf("invalid identifier")
|
|
|
|
}
|
|
|
|
|
2022-01-21 08:04:04 +00:00
|
|
|
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(params.Email, params.Identifier)
|
2021-07-18 07:26:17 +00:00
|
|
|
if err != nil {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Failed to get verification request", err)
|
2021-07-18 07:26:17 +00:00
|
|
|
return res, fmt.Errorf(`verification request not found`)
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
// delete current verification and create new one
|
2022-01-21 08:04:04 +00:00
|
|
|
err = db.Provider.DeleteVerificationRequest(verificationRequest)
|
2021-12-23 05:01:52 +00:00
|
|
|
if err != nil {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Failed to delete verification request", err)
|
2021-12-23 05:01:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 06:05:24 +00:00
|
|
|
hostname := utils.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-24 07:12:29 +00:00
|
|
|
log.Debug("Failed to generate nonce", err)
|
2022-03-02 12:12:31 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2022-03-08 07:06:26 +00:00
|
|
|
|
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, params.Identifier, hostname, nonceHash, verificationRequest.RedirectURI)
|
2021-07-18 07:26:17 +00:00
|
|
|
if err != nil {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Failed to create verification token", err)
|
2021-07-18 07:26:17 +00:00
|
|
|
}
|
2022-05-24 07:12:29 +00:00
|
|
|
_, err = db.Provider.AddVerificationRequest(models.VerificationRequest{
|
2022-03-08 07:06:26 +00:00
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: params.Identifier,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: verificationRequest.RedirectURI,
|
2021-07-18 07:26:17 +00:00
|
|
|
})
|
2022-05-24 07:12:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add verification request", err)
|
|
|
|
}
|
2021-07-18 07:26:17 +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.SendVerificationMail(params.Email, verificationToken, hostname)
|
2021-07-18 07:26:17 +00:00
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|