authorizer/server/resolvers/resend_verify_email.go

84 lines
2.5 KiB
Go
Raw Normal View History

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"
"github.com/authorizerdev/authorizer/server/token"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/utils"
)
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) {
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-25 07:00:22 +00:00
log.Debug("Failed to get GinContext: ", err)
2022-01-31 06:05:24 +00:00
return res, err
}
params.Email = strings.ToLower(params.Email)
if !utils.IsValidEmail(params.Email) {
2022-05-25 07:00:22 +00:00
log.Debug("Invalid email: ", params.Email)
return res, fmt.Errorf("invalid email")
}
if !utils.IsValidVerificationIdentifier(params.Identifier) {
2022-05-25 07:00:22 +00:00
log.Debug("Invalid verification identifier: ", params.Identifier)
return res, fmt.Errorf("invalid identifier")
}
2022-01-21 08:04:04 +00:00
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(params.Email, params.Identifier)
if err != nil {
2022-05-25 07:00:22 +00:00
log.Debug("Failed to get verification request: ", err)
return res, fmt.Errorf(`verification request not found`)
}
// delete current verification and create new one
2022-01-21 08:04:04 +00:00
err = db.Provider.DeleteVerificationRequest(verificationRequest)
if err != nil {
2022-05-25 07:00:22 +00:00
log.Debug("Failed to delete verification request: ", err)
}
2022-01-31 06:05:24 +00:00
hostname := utils.GetHost(gc)
_, 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
}
verificationToken, err := token.CreateVerificationToken(params.Email, params.Identifier, hostname, nonceHash, verificationRequest.RedirectURI)
if err != nil {
2022-05-25 07:00:22 +00:00
log.Debug("Failed to create verification token: ", err)
}
2022-05-24 07:12:29 +00:00
_, err = db.Provider.AddVerificationRequest(models.VerificationRequest{
Token: verificationToken,
Identifier: params.Identifier,
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
Email: params.Email,
Nonce: nonceHash,
RedirectURI: verificationRequest.RedirectURI,
})
2022-05-24 07:12:29 +00:00
if err != nil {
2022-05-25 07:00:22 +00:00
log.Debug("Failed to add verification request: ", err)
2022-05-24 07:12:29 +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)
res = &model.Response{
Message: `Verification email has been sent. Please check your inbox`,
}
return res, nil
}