2021-11-11 23:52:03 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-05-24 07:12:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-11-11 23:52:03 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"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-11-11 23:52:03 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-05-30 03:49:55 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-05-30 06:24:16 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/parsers"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-11-11 23:52:03 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2022-05-30 06:24:16 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/validators"
|
2021-11-11 23:52:03 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// MagicLinkLoginResolver is a resolver for magic link login mutation
|
|
|
|
func MagicLinkLoginResolver(ctx context.Context, params model.MagicLinkLoginInput) (*model.Response, error) {
|
2021-11-11 23:52:03 +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-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2022-01-31 06:05:24 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2021-11-11 23:52:03 +00:00
|
|
|
|
2022-05-30 03:49:55 +00:00
|
|
|
isMagicLinkLoginDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableMagicLinkLogin)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting magic link login disabled: ", err)
|
|
|
|
isMagicLinkLoginDisabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isMagicLinkLoginDisabled {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Magic link login is disabled.")
|
2021-11-11 23:52:03 +00:00
|
|
|
return res, fmt.Errorf(`magic link login is disabled for this instance`)
|
|
|
|
}
|
|
|
|
|
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
2022-05-30 06:24:16 +00:00
|
|
|
if !validators.IsValidEmail(params.Email) {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Invalid email")
|
2021-11-11 23:52:03 +00:00
|
|
|
return res, fmt.Errorf(`invalid email address`)
|
|
|
|
}
|
|
|
|
|
2022-05-24 07:12:29 +00:00
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"email": params.Email,
|
|
|
|
})
|
|
|
|
|
2021-11-11 23:52:03 +00:00
|
|
|
inputRoles := []string{}
|
|
|
|
|
2022-01-21 08:04:04 +00:00
|
|
|
user := models.User{
|
2021-11-11 23:52:03 +00:00
|
|
|
Email: params.Email,
|
|
|
|
}
|
|
|
|
|
|
|
|
// find user with email
|
2022-01-21 08:04:04 +00:00
|
|
|
existingUser, err := db.Provider.GetUserByEmail(params.Email)
|
2021-11-11 23:52:03 +00:00
|
|
|
if err != nil {
|
2022-05-30 03:49:55 +00:00
|
|
|
isSignupDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableSignUp)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting signup disabled: ", err)
|
|
|
|
}
|
|
|
|
if isSignupDisabled {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Signup is disabled.")
|
2022-03-16 17:19:18 +00:00
|
|
|
return res, fmt.Errorf(`signup is disabled for this instance`)
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
user.SignupMethods = constants.SignupMethodMagicLinkLogin
|
2021-11-11 23:52:03 +00:00
|
|
|
// define roles for new user
|
|
|
|
if len(params.Roles) > 0 {
|
|
|
|
// check if roles exists
|
2022-05-30 03:49:55 +00:00
|
|
|
roles, err := memorystore.Provider.GetSliceStoreEnvVariable(constants.EnvKeyRoles)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting roles: ", err)
|
|
|
|
return res, err
|
|
|
|
}
|
2022-05-30 06:24:16 +00:00
|
|
|
if !validators.IsValidRoles(params.Roles, roles) {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Invalid roles: ", params.Roles)
|
2021-11-11 23:52:03 +00:00
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
|
|
|
} else {
|
|
|
|
inputRoles = params.Roles
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-30 03:49:55 +00:00
|
|
|
inputRoles, err = memorystore.Provider.GetSliceStoreEnvVariable(constants.EnvKeyDefaultRoles)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting default roles: ", err)
|
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
|
|
|
}
|
|
|
|
|
2021-11-11 23:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user.Roles = strings.Join(inputRoles, ",")
|
2022-01-21 08:04:04 +00:00
|
|
|
user, _ = db.Provider.AddUser(user)
|
2021-11-11 23:52:03 +00:00
|
|
|
} else {
|
|
|
|
user = existingUser
|
|
|
|
// There multiple scenarios with roles here in magic link login
|
|
|
|
// 1. user has access to protected roles + roles and trying to login
|
|
|
|
// 2. user has not signed up for one of the available role but trying to signup.
|
|
|
|
// Need to modify roles in this case
|
|
|
|
|
2022-03-24 08:43:55 +00:00
|
|
|
if user.RevokedTimestamp != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("User access is revoked at: ", user.RevokedTimestamp)
|
2022-03-24 08:43:55 +00:00
|
|
|
return res, fmt.Errorf(`user access has been revoked`)
|
|
|
|
}
|
|
|
|
|
2021-11-11 23:52:03 +00:00
|
|
|
// find the unassigned roles
|
2022-03-08 07:06:26 +00:00
|
|
|
if len(params.Roles) <= 0 {
|
2022-05-30 03:49:55 +00:00
|
|
|
inputRoles, err = memorystore.Provider.GetSliceStoreEnvVariable(constants.EnvKeyDefaultRoles)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting default roles: ", err)
|
|
|
|
return res, fmt.Errorf(`invalid default roles`)
|
|
|
|
}
|
2022-03-08 07:06:26 +00:00
|
|
|
}
|
2021-11-11 23:52:03 +00:00
|
|
|
existingRoles := strings.Split(existingUser.Roles, ",")
|
|
|
|
unasignedRoles := []string{}
|
|
|
|
for _, ir := range inputRoles {
|
|
|
|
if !utils.StringSliceContains(existingRoles, ir) {
|
|
|
|
unasignedRoles = append(unasignedRoles, ir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(unasignedRoles) > 0 {
|
|
|
|
// check if it contains protected unassigned role
|
|
|
|
hasProtectedRole := false
|
2022-05-30 03:49:55 +00:00
|
|
|
protectedRoles, err := memorystore.Provider.GetSliceStoreEnvVariable(constants.EnvKeyProtectedRoles)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting protected roles: ", err)
|
|
|
|
return res, err
|
|
|
|
}
|
2021-11-11 23:52:03 +00:00
|
|
|
for _, ur := range unasignedRoles {
|
2022-05-30 03:49:55 +00:00
|
|
|
if utils.StringSliceContains(protectedRoles, ur) {
|
2021-11-11 23:52:03 +00:00
|
|
|
hasProtectedRole = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasProtectedRole {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("User is not assigned one of the protected roles", unasignedRoles)
|
2021-11-11 23:52:03 +00:00
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
|
|
|
} else {
|
|
|
|
user.Roles = existingUser.Roles + "," + strings.Join(unasignedRoles, ",")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user.Roles = existingUser.Roles
|
|
|
|
}
|
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
signupMethod := existingUser.SignupMethods
|
2022-01-17 06:02:13 +00:00
|
|
|
if !strings.Contains(signupMethod, constants.SignupMethodMagicLinkLogin) {
|
|
|
|
signupMethod = signupMethod + "," + constants.SignupMethodMagicLinkLogin
|
2021-11-11 23:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
user.SignupMethods = signupMethod
|
2022-01-21 08:04:04 +00:00
|
|
|
user, _ = db.Provider.UpdateUser(user)
|
2021-12-17 15:55:07 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to update user: ", err)
|
2021-12-17 15:55:07 +00:00
|
|
|
}
|
2021-11-11 23:52:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 06:24:16 +00:00
|
|
|
hostname := parsers.GetHost(gc)
|
2022-05-30 03:49:55 +00:00
|
|
|
isEmailVerificationDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableEmailVerification)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting email verification disabled: ", err)
|
|
|
|
isEmailVerificationDisabled = true
|
|
|
|
}
|
|
|
|
if !isEmailVerificationDisabled {
|
2021-11-11 23:52:03 +00:00
|
|
|
// insert verification request
|
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-25 07:00:22 +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
|
|
|
redirectURLParams := "&roles=" + strings.Join(inputRoles, ",")
|
|
|
|
if params.State != nil {
|
|
|
|
redirectURLParams = redirectURLParams + "&state=" + *params.State
|
|
|
|
}
|
|
|
|
if params.Scope != nil && len(params.Scope) > 0 {
|
|
|
|
redirectURLParams = redirectURLParams + "&scope=" + strings.Join(params.Scope, " ")
|
|
|
|
}
|
2022-05-30 06:24:16 +00:00
|
|
|
redirectURL := parsers.GetAppURL(gc)
|
2022-03-08 07:06:26 +00:00
|
|
|
if params.RedirectURI != nil {
|
|
|
|
redirectURL = *params.RedirectURI
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(redirectURL, "?") {
|
|
|
|
redirectURL = redirectURL + "&" + redirectURLParams
|
|
|
|
} else {
|
|
|
|
redirectURL = redirectURL + "?" + redirectURLParams
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
verificationType := constants.VerificationTypeMagicLinkLogin
|
2022-03-08 07:06:26 +00:00
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, verificationType, hostname, nonceHash, redirectURL)
|
2021-11-11 23:52:03 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to create verification token: ", err)
|
2021-11-11 23:52:03 +00:00
|
|
|
}
|
2022-03-09 01:40:07 +00:00
|
|
|
_, err = db.Provider.AddVerificationRequest(models.VerificationRequest{
|
2022-03-08 07:06:26 +00:00
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: verificationType,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: redirectURL,
|
2021-11-11 23:52:03 +00:00
|
|
|
})
|
2022-03-09 01:40:07 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to add verification request in db: ", err)
|
2022-03-09 01:40:07 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2021-11-11 23:52:03 +00:00
|
|
|
|
2022-03-09 01:40:07 +00:00
|
|
|
// exec it as go routing so that we can reduce the api latency
|
2022-03-02 12:12:31 +00:00
|
|
|
go email.SendVerificationMail(params.Email, verificationToken, hostname)
|
2021-11-11 23:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res = &model.Response{
|
2021-11-14 22:42:28 +00:00
|
|
|
Message: `Magic Link has been sent to your email. Please check your inbox!`,
|
2021-11-11 23:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|