2022-07-29 08:19:46 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-29 14:19:50 +00:00
|
|
|
"errors"
|
2022-07-29 08:19:46 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-08-02 08:42:36 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-07-29 08:19:46 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-07-29 14:19:50 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/email"
|
2022-07-29 08:19:46 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-08-02 08:42:36 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-07-29 08:19:46 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResendOTPResolver is a resolver for resend otp mutation
|
|
|
|
func ResendOTPResolver(ctx context.Context, params model.ResendOTPRequest) (*model.Response, error) {
|
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"email": params.Email,
|
|
|
|
})
|
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
user, err := db.Provider.GetUserByEmail(ctx, params.Email)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to get user by email: ", err)
|
2022-07-29 14:19:50 +00:00
|
|
|
return nil, fmt.Errorf(`user with this email not found`)
|
2022-07-29 08:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if user.RevokedTimestamp != nil {
|
|
|
|
log.Debug("User access is revoked")
|
2022-07-29 14:19:50 +00:00
|
|
|
return nil, fmt.Errorf(`user access has been revoked`)
|
2022-07-29 08:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if user.EmailVerifiedAt == nil {
|
|
|
|
log.Debug("User email is not verified")
|
2022-07-29 14:19:50 +00:00
|
|
|
return nil, fmt.Errorf(`email not verified`)
|
2022-07-29 08:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !refs.BoolValue(user.IsMultiFactorAuthEnabled) {
|
|
|
|
log.Debug("User multi factor authentication is not enabled")
|
2022-07-29 14:19:50 +00:00
|
|
|
return nil, fmt.Errorf(`multi factor authentication not enabled`)
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:42:36 +00:00
|
|
|
isEmailServiceEnabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyIsEmailServiceEnabled)
|
|
|
|
if err != nil || !isEmailServiceEnabled {
|
|
|
|
log.Debug("Email service not enabled: ", err)
|
|
|
|
return nil, errors.New("email service not enabled")
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:50:23 +00:00
|
|
|
isMFADisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableMultiFactorAuthentication)
|
|
|
|
if err != nil || isMFADisabled {
|
|
|
|
log.Debug("MFA service not enabled: ", err)
|
|
|
|
return nil, errors.New("multi factor authentication is disabled for this instance")
|
|
|
|
}
|
|
|
|
|
2022-07-29 14:19:50 +00:00
|
|
|
// get otp by email
|
|
|
|
otpData, err := db.Provider.GetOTPByEmail(ctx, params.Email)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to get otp for given email: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if otpData == nil {
|
|
|
|
log.Debug("No otp found for given email: ", params.Email)
|
|
|
|
return &model.Response{
|
|
|
|
Message: "Failed to get for given email",
|
|
|
|
}, errors.New("failed to get otp for given email")
|
2022-07-29 08:19:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 14:19:50 +00:00
|
|
|
otp := utils.GenerateOTP()
|
|
|
|
otpData, err = db.Provider.UpsertOTP(ctx, &models.OTP{
|
2022-07-29 08:19:46 +00:00
|
|
|
Email: user.Email,
|
2022-07-29 14:19:50 +00:00
|
|
|
Otp: otp,
|
2022-07-29 08:19:46 +00:00
|
|
|
ExpiresAt: time.Now().Add(1 * time.Minute).Unix(),
|
|
|
|
})
|
2022-07-29 14:19:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error generating new otp: ", err)
|
|
|
|
return nil, err
|
2022-07-29 08:19:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 14:19:50 +00:00
|
|
|
go func() {
|
2022-08-08 20:13:37 +00:00
|
|
|
// exec it as go routine so that we can reduce the api latency
|
|
|
|
go email.SendEmail([]string{params.Email}, constants.VerificationTypeOTP, map[string]interface{}{
|
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
|
|
|
"otp": otp,
|
|
|
|
})
|
2022-07-29 14:19:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error sending otp email: ", otp)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &model.Response{
|
|
|
|
Message: `OTP has been sent. Please check your inbox`,
|
|
|
|
}, nil
|
2022-07-29 08:19:46 +00:00
|
|
|
}
|