2021-07-17 21:59:50 +05:30
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-14 12:01:37 +05:30
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-07-18 09:25:20 +05:30
|
|
|
"fmt"
|
2021-07-17 21:59:50 +05:30
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-10-23 21:08:08 +05:30
|
|
|
"github.com/google/uuid"
|
2022-05-24 12:42:29 +05:30
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-07-28 15:43:08 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-23 01:24:41 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/cookie"
|
2022-02-28 21:26:49 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/crypto"
|
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-27 23:20:38 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-05-30 11:54:16 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/parsers"
|
2022-08-03 23:20:23 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
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-17 21:59:50 +05:30
|
|
|
)
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
// SignupResolver is a resolver for signup mutation
|
|
|
|
func SignupResolver(ctx context.Context, params model.SignUpInput) (*model.AuthResponse, error) {
|
2021-07-28 15:43:08 +05:30
|
|
|
var res *model.AuthResponse
|
2022-05-24 12:42:29 +05:30
|
|
|
|
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2021-07-28 15:43:08 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2021-07-28 15:43:08 +05:30
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-05-30 09:19:55 +05:30
|
|
|
isSignupDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableSignUp)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting signup disabled: ", err)
|
|
|
|
isSignupDisabled = true
|
|
|
|
}
|
|
|
|
if isSignupDisabled {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Signup is disabled")
|
2022-03-16 22:49:18 +05:30
|
|
|
return res, fmt.Errorf(`signup is disabled for this instance`)
|
|
|
|
}
|
|
|
|
|
2022-05-30 09:19:55 +05:30
|
|
|
isBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting basic auth disabled: ", err)
|
|
|
|
isBasicAuthDisabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isBasicAuthDisabled {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Basic authentication is disabled")
|
2021-07-28 15:43:08 +05:30
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
2022-03-16 22:49:18 +05:30
|
|
|
|
2021-07-21 03:34:03 +05:30
|
|
|
if params.ConfirmPassword != params.Password {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Passwords do not match")
|
2021-12-20 23:21:27 +05:30
|
|
|
return res, fmt.Errorf(`password and confirm password does not match`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2022-06-18 15:31:57 +05:30
|
|
|
if err := validators.IsValidPassword(params.Password); err != nil {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("Invalid password")
|
2022-06-18 15:31:57 +05:30
|
|
|
return res, err
|
2022-03-17 15:35:07 +05:30
|
|
|
}
|
|
|
|
|
2021-07-17 21:59:50 +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 09:25:20 +05:30
|
|
|
return res, fmt.Errorf(`invalid email address`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2022-05-24 12:42:29 +05:30
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"email": params.Email,
|
|
|
|
})
|
2021-07-17 21:59:50 +05:30
|
|
|
// find user with email
|
2022-07-10 21:49:33 +05:30
|
|
|
existingUser, err := db.Provider.GetUserByEmail(ctx, params.Email)
|
2021-07-17 21:59:50 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get user by email: ", err)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2023-08-01 16:09:17 +05:30
|
|
|
if existingUser != nil {
|
|
|
|
if existingUser.EmailVerifiedAt != nil {
|
|
|
|
// email is verified
|
|
|
|
log.Debug("Email is already verified and signed up.")
|
|
|
|
return res, fmt.Errorf(`%s has already signed up`, params.Email)
|
|
|
|
} else if existingUser.ID != "" && existingUser.EmailVerifiedAt == nil {
|
|
|
|
log.Debug("Email is already signed up. Verification pending...")
|
|
|
|
return res, fmt.Errorf("%s has already signed up. please complete the email verification process or reset the password", params.Email)
|
|
|
|
}
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
2021-12-20 23:21:27 +05:30
|
|
|
|
2021-12-24 18:42:32 +05:30
|
|
|
inputRoles := []string{}
|
|
|
|
if len(params.Roles) > 0 {
|
|
|
|
// check if roles exists
|
2022-05-31 08:14:03 +05:30
|
|
|
rolesString, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyRoles)
|
|
|
|
roles := []string{}
|
2022-05-30 09:19:55 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting roles: ", err)
|
|
|
|
return res, err
|
2022-05-31 08:14:03 +05:30
|
|
|
} else {
|
|
|
|
roles = strings.Split(rolesString, ",")
|
2022-05-30 09:19:55 +05:30
|
|
|
}
|
2022-06-07 08:00:30 +05:30
|
|
|
if !validators.IsValidRoles(params.Roles, roles) {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Invalid roles: ", params.Roles)
|
2021-12-24 18:42:32 +05:30
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
|
|
|
} else {
|
|
|
|
inputRoles = params.Roles
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-31 08:14:03 +05:30
|
|
|
inputRolesString, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyDefaultRoles)
|
2022-05-30 09:19:55 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting default roles: ", err)
|
|
|
|
return res, err
|
2022-05-31 08:14:03 +05:30
|
|
|
} else {
|
|
|
|
inputRoles = strings.Split(inputRolesString, ",")
|
2022-05-30 09:19:55 +05:30
|
|
|
}
|
2021-12-24 18:42:32 +05:30
|
|
|
}
|
2023-07-31 16:42:11 +05:30
|
|
|
user := &models.User{
|
2021-07-17 22:39:50 +05:30
|
|
|
Email: params.Email,
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
2021-09-20 10:36:26 +05:30
|
|
|
user.Roles = strings.Join(inputRoles, ",")
|
2022-02-28 21:26:49 +05:30
|
|
|
password, _ := crypto.EncryptPassword(params.Password)
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Password = &password
|
2021-07-17 22:39:50 +05:30
|
|
|
|
2021-12-22 10:51:12 +05:30
|
|
|
if params.GivenName != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.GivenName = params.GivenName
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2021-12-22 10:51:12 +05:30
|
|
|
if params.FamilyName != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.FamilyName = params.FamilyName
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2021-12-22 10:51:12 +05:30
|
|
|
if params.MiddleName != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.MiddleName = params.MiddleName
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Nickname != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Nickname = params.Nickname
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Gender != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Gender = params.Gender
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Birthdate != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Birthdate = params.Birthdate
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.PhoneNumber != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.PhoneNumber = params.PhoneNumber
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Picture != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Picture = params.Picture
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
2022-07-23 15:26:44 +05:30
|
|
|
if params.IsMultiFactorAuthEnabled != nil {
|
|
|
|
user.IsMultiFactorAuthEnabled = params.IsMultiFactorAuthEnabled
|
|
|
|
}
|
|
|
|
|
2022-08-03 23:20:23 +05:30
|
|
|
isMFAEnforced, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyEnforceMultiFactorAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("MFA service not enabled: ", err)
|
|
|
|
isMFAEnforced = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if isMFAEnforced {
|
|
|
|
user.IsMultiFactorAuthEnabled = refs.NewBoolRef(true)
|
|
|
|
}
|
|
|
|
|
2023-08-14 12:01:37 +05:30
|
|
|
if params.AppData != nil {
|
|
|
|
appDataString := ""
|
|
|
|
appDataBytes, err := json.Marshal(params.AppData)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("failed to marshall source app_data: ", err)
|
|
|
|
return nil, errors.New("malformed app_data")
|
|
|
|
}
|
|
|
|
appDataString = string(appDataBytes)
|
|
|
|
user.AppData = &appDataString
|
|
|
|
}
|
|
|
|
|
2022-06-29 22:24:00 +05:30
|
|
|
user.SignupMethods = constants.AuthRecipeMethodBasicAuth
|
2022-05-30 09:19:55 +05:30
|
|
|
isEmailVerificationDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableEmailVerification)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting email verification disabled: ", err)
|
|
|
|
isEmailVerificationDisabled = true
|
|
|
|
}
|
|
|
|
if isEmailVerificationDisabled {
|
2021-12-22 15:38:51 +05:30
|
|
|
now := time.Now().Unix()
|
|
|
|
user.EmailVerifiedAt = &now
|
2021-07-28 15:43:08 +05:30
|
|
|
}
|
2022-07-10 21:49:33 +05:30
|
|
|
user, err = db.Provider.AddUser(ctx, user)
|
2021-07-17 21:59:50 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to add user: ", err)
|
2021-07-17 21:59:50 +05:30
|
|
|
return res, err
|
|
|
|
}
|
2021-10-13 22:11:41 +05:30
|
|
|
roles := strings.Split(user.Roles, ",")
|
2022-01-23 01:24:41 +05:30
|
|
|
userToReturn := user.AsAPIUser()
|
2021-07-28 15:43:08 +05:30
|
|
|
|
2022-05-30 11:54:16 +05:30
|
|
|
hostname := parsers.GetHost(gc)
|
2022-05-30 09:19:55 +05:30
|
|
|
if !isEmailVerificationDisabled {
|
2021-07-28 15:43:08 +05:30
|
|
|
// insert verification request
|
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-01-17 11:32:13 +05:30
|
|
|
verificationType := constants.VerificationTypeBasicAuthSignup
|
2022-05-30 11:54:16 +05:30
|
|
|
redirectURL := parsers.GetAppURL(gc)
|
2022-03-16 21:52:45 +05:30
|
|
|
if params.RedirectURI != nil {
|
|
|
|
redirectURL = *params.RedirectURI
|
|
|
|
}
|
2022-03-08 12:36:26 +05:30
|
|
|
verificationToken, err := token.CreateVerificationToken(params.Email, verificationType, hostname, nonceHash, redirectURL)
|
2021-07-28 15:43:08 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to create verification token: ", err)
|
2022-03-02 17:42:31 +05:30
|
|
|
return res, err
|
2021-07-28 15:43:08 +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: verificationType,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: redirectURL,
|
2021-07-28 15:43:08 +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
|
|
|
return res, err
|
|
|
|
}
|
2021-07-28 15:43:08 +05:30
|
|
|
|
2022-08-09 01:43:37 +05:30
|
|
|
// exec it as go routine so that we can reduce the api latency
|
2022-07-11 10:42:42 +05:30
|
|
|
go func() {
|
2022-08-09 01:43:37 +05:30
|
|
|
// exec it as go routine so that we can reduce the api latency
|
|
|
|
email.SendEmail([]string{params.Email}, constants.VerificationTypeBasicAuthSignup, map[string]interface{}{
|
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
2023-05-02 18:39:10 +05:30
|
|
|
"verification_url": utils.GetEmailVerificationURL(verificationToken, hostname, redirectURL),
|
2022-08-09 01:43:37 +05:30
|
|
|
})
|
2022-07-11 10:42:42 +05:30
|
|
|
utils.RegisterEvent(ctx, constants.UserCreatedWebhookEvent, constants.AuthRecipeMethodBasicAuth, user)
|
|
|
|
}()
|
2021-07-28 15:43:08 +05:30
|
|
|
|
|
|
|
res = &model.AuthResponse{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
|
|
|
User: userToReturn,
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-02 17:42:31 +05:30
|
|
|
scope := []string{"openid", "email", "profile"}
|
2022-03-09 11:53:34 +05:30
|
|
|
if params.Scope != nil && len(scope) > 0 {
|
|
|
|
scope = params.Scope
|
|
|
|
}
|
2021-07-28 15:43:08 +05:30
|
|
|
|
2022-11-15 21:45:08 +05:30
|
|
|
code := ""
|
|
|
|
codeChallenge := ""
|
|
|
|
nonce := ""
|
|
|
|
if params.State != nil {
|
|
|
|
// Get state from store
|
|
|
|
authorizeState, _ := memorystore.Provider.GetState(refs.StringValue(params.State))
|
|
|
|
if authorizeState != "" {
|
|
|
|
authorizeStateSplit := strings.Split(authorizeState, "@@")
|
|
|
|
if len(authorizeStateSplit) > 1 {
|
|
|
|
code = authorizeStateSplit[0]
|
|
|
|
codeChallenge = authorizeStateSplit[1]
|
|
|
|
} else {
|
|
|
|
nonce = authorizeState
|
|
|
|
}
|
|
|
|
go memorystore.Provider.RemoveState(refs.StringValue(params.State))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nonce == "" {
|
|
|
|
nonce = uuid.New().String()
|
2022-11-12 23:54:37 +05:30
|
|
|
}
|
|
|
|
|
2022-11-13 01:22:21 +05:30
|
|
|
authToken, err := token.CreateAuthToken(gc, user, roles, scope, constants.AuthRecipeMethodBasicAuth, nonce, code)
|
2022-01-23 01:24:41 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to create auth token: ", err)
|
2022-01-23 01:24:41 +05:30
|
|
|
return res, err
|
|
|
|
}
|
2022-03-02 17:42:31 +05:30
|
|
|
|
2022-11-15 21:45:08 +05:30
|
|
|
// Code challenge could be optional if PKCE flow is not used
|
|
|
|
if code != "" {
|
|
|
|
if err := memorystore.Provider.SetState(code, codeChallenge+"@@"+authToken.FingerPrintHash); err != nil {
|
|
|
|
log.Debug("SetState failed: ", err)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 15:21:20 +03:00
|
|
|
expiresIn := authToken.AccessToken.ExpiresAt - time.Now().Unix()
|
|
|
|
if expiresIn <= 0 {
|
|
|
|
expiresIn = 1
|
|
|
|
}
|
2022-01-23 01:24:41 +05:30
|
|
|
|
2021-07-28 15:43:08 +05:30
|
|
|
res = &model.AuthResponse{
|
2021-12-22 10:51:12 +05:30
|
|
|
Message: `Signed up successfully.`,
|
2022-01-23 01:24:41 +05:30
|
|
|
AccessToken: &authToken.AccessToken.Token,
|
2022-03-02 17:42:31 +05:30
|
|
|
ExpiresIn: &expiresIn,
|
2021-12-22 10:51:12 +05:30
|
|
|
User: userToReturn,
|
2021-07-28 15:43:08 +05:30
|
|
|
}
|
2022-06-12 00:27:21 +05:30
|
|
|
|
2022-06-29 22:24:00 +05:30
|
|
|
sessionKey := constants.AuthRecipeMethodBasicAuth + ":" + user.ID
|
2022-06-12 00:27:21 +05:30
|
|
|
cookie.SetSession(gc, authToken.FingerPrintHash)
|
2023-04-08 13:06:15 +05:30
|
|
|
memorystore.Provider.SetUserSession(sessionKey, constants.TokenTypeSessionToken+"_"+authToken.FingerPrint, authToken.FingerPrintHash, authToken.SessionTokenExpiresAt)
|
|
|
|
memorystore.Provider.SetUserSession(sessionKey, constants.TokenTypeAccessToken+"_"+authToken.FingerPrint, authToken.AccessToken.Token, authToken.AccessToken.ExpiresAt)
|
2022-06-12 00:27:21 +05:30
|
|
|
|
|
|
|
if authToken.RefreshToken != nil {
|
|
|
|
res.RefreshToken = &authToken.RefreshToken.Token
|
2023-04-08 13:06:15 +05:30
|
|
|
memorystore.Provider.SetUserSession(sessionKey, constants.TokenTypeRefreshToken+"_"+authToken.FingerPrint, authToken.RefreshToken.Token, authToken.RefreshToken.ExpiresAt)
|
2022-06-12 00:27:21 +05:30
|
|
|
}
|
2022-07-11 10:42:42 +05:30
|
|
|
|
|
|
|
go func() {
|
2023-08-29 18:14:48 +05:30
|
|
|
utils.RegisterEvent(ctx, constants.UserCreatedWebhookEvent, constants.AuthRecipeMethodBasicAuth, user)
|
2022-07-11 10:42:42 +05:30
|
|
|
utils.RegisterEvent(ctx, constants.UserSignUpWebhookEvent, constants.AuthRecipeMethodBasicAuth, user)
|
2023-08-02 10:02:41 +05:30
|
|
|
// User is also logged in with signup
|
|
|
|
utils.RegisterEvent(ctx, constants.UserLoginWebhookEvent, constants.AuthRecipeMethodBasicAuth, user)
|
2023-07-31 16:42:11 +05:30
|
|
|
db.Provider.AddSession(ctx, &models.Session{
|
2022-07-11 10:42:42 +05:30
|
|
|
UserID: user.ID,
|
|
|
|
UserAgent: utils.GetUserAgent(gc.Request),
|
|
|
|
IP: utils.GetIP(gc.Request),
|
|
|
|
})
|
|
|
|
}()
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|