2021-07-18 12:56:17 +05:30
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-05-24 12:42:29 +05:30
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
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"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-05-30 11:54:16 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/parsers"
|
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"
|
2022-05-30 11:54:16 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/validators"
|
2021-07-18 12:56:17 +05:30
|
|
|
)
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
// ResendVerifyEmailResolver is a resolver for resend verify email mutation
|
|
|
|
func ResendVerifyEmailResolver(ctx context.Context, params model.ResendVerifyEmailInput) (*model.Response, error) {
|
2021-07-18 12:56:17 +05:30
|
|
|
var res *model.Response
|
2022-05-24 12:42:29 +05:30
|
|
|
|
2022-01-31 11:35:24 +05:30
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2022-01-31 11:35:24 +05:30
|
|
|
return res, err
|
|
|
|
}
|
2021-07-18 12:56:17 +05:30
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
2022-05-30 11:54:16 +05:30
|
|
|
if !validators.IsValidEmail(params.Email) {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Invalid email: ", params.Email)
|
2021-07-18 12:56:17 +05:30
|
|
|
return res, fmt.Errorf("invalid email")
|
|
|
|
}
|
|
|
|
|
2022-05-30 11:54:16 +05:30
|
|
|
if !validators.IsValidVerificationIdentifier(params.Identifier) {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Invalid verification identifier: ", params.Identifier)
|
2021-12-23 10:31:52 +05:30
|
|
|
return res, fmt.Errorf("invalid identifier")
|
|
|
|
}
|
|
|
|
|
2022-08-09 01:43:37 +05:30
|
|
|
user, err := db.Provider.GetUserByEmail(ctx, params.Email)
|
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("invalid user")
|
|
|
|
}
|
|
|
|
|
2022-07-10 21:49:33 +05:30
|
|
|
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(ctx, params.Email, params.Identifier)
|
2021-07-18 12:56:17 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get verification request: ", err)
|
2021-07-18 12:56:17 +05:30
|
|
|
return res, fmt.Errorf(`verification request not found`)
|
|
|
|
}
|
|
|
|
|
2021-12-23 10:31:52 +05:30
|
|
|
// delete current verification and create new one
|
2022-07-10 21:49:33 +05:30
|
|
|
err = db.Provider.DeleteVerificationRequest(ctx, verificationRequest)
|
2021-12-23 10:31:52 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to delete verification request: ", err)
|
2021-12-23 10:31:52 +05:30
|
|
|
}
|
|
|
|
|
2022-05-30 11:54:16 +05:30
|
|
|
hostname := parsers.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 {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to generate nonce: ", err)
|
2022-03-02 17:42:31 +05:30
|
|
|
return res, err
|
|
|
|
}
|
2022-03-08 12:36:26 +05:30
|
|
|
|
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, params.Identifier, hostname, nonceHash, verificationRequest.RedirectURI)
|
2021-07-18 12:56:17 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to create verification token: ", err)
|
2021-07-18 12:56:17 +05:30
|
|
|
}
|
2023-07-31 16:42:11 +05:30
|
|
|
_, err = db.Provider.AddVerificationRequest(ctx, &models.VerificationRequest{
|
2022-03-08 12:36:26 +05:30
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: params.Identifier,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: verificationRequest.RedirectURI,
|
2021-07-18 12:56:17 +05:30
|
|
|
})
|
2022-05-24 12:42:29 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to add verification request: ", err)
|
2022-05-24 12:42:29 +05:30
|
|
|
}
|
2021-07-18 12:56:17 +05:30
|
|
|
|
2022-08-09 01:43:37 +05:30
|
|
|
// exec it as go routine so that we can reduce the api latency
|
|
|
|
go email.SendEmail([]string{params.Email}, params.Identifier, map[string]interface{}{
|
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
2023-05-02 18:39:10 +05:30
|
|
|
"verification_url": utils.GetEmailVerificationURL(verificationToken, hostname, verificationRequest.RedirectURI),
|
2022-08-09 01:43:37 +05:30
|
|
|
})
|
2021-07-18 12:56:17 +05:30
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|