2021-07-18 03:55:20 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-14 06:31:37 +00:00
|
|
|
"encoding/json"
|
2022-07-29 14:19:50 +00:00
|
|
|
"errors"
|
2021-07-18 03:55:20 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-05-24 07:12:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/cookie"
|
2022-02-28 15:56:49 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/crypto"
|
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"
|
2022-05-27 17:50:38 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-05-30 06:24:16 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/parsers"
|
2022-07-15 16:41:08 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2022-05-30 06:24:16 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/validators"
|
2021-07-18 03:55:20 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// UpdateProfileResolver is resolver for update profile mutation
|
|
|
|
func UpdateProfileResolver(ctx context.Context, params model.UpdateProfileInput) (*model.Response, error) {
|
2021-07-18 03:55:20 +00:00
|
|
|
var res *model.Response
|
2022-05-24 07:12:29 +00:00
|
|
|
|
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2021-07-18 03:55:20 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-03-02 12:12:31 +00:00
|
|
|
accessToken, err := token.GetAccessToken(gc)
|
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to get access token: ", err)
|
2022-03-02 12:12:31 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
claims, err := token.ValidateAccessToken(gc, accessToken)
|
2021-07-18 03:55:20 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to validate access token: ", err)
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate if all params are not empty
|
2023-08-14 06:31:37 +00:00
|
|
|
if params.GivenName == nil && params.FamilyName == nil && params.Picture == nil && params.MiddleName == nil && params.Nickname == nil && params.OldPassword == nil && params.Email == nil && params.Birthdate == nil && params.Gender == nil && params.PhoneNumber == nil && params.NewPassword == nil && params.ConfirmNewPassword == nil && params.IsMultiFactorAuthEnabled == nil && params.AppData == nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("All params are empty")
|
2022-01-22 19:54:41 +00:00
|
|
|
return res, fmt.Errorf("please enter at least one param to update")
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 12:12:31 +00:00
|
|
|
userID := claims["sub"].(string)
|
2022-05-24 07:12:29 +00:00
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"user_id": userID,
|
|
|
|
})
|
2022-07-10 16:19:33 +00:00
|
|
|
user, err := db.Provider.GetUserByID(ctx, userID)
|
2021-07-18 03:55:20 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to get user by id: ", err)
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.GivenName != nil && refs.StringValue(user.GivenName) != refs.StringValue(params.GivenName) {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.GivenName = params.GivenName
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.FamilyName != nil && refs.StringValue(user.FamilyName) != refs.StringValue(params.FamilyName) {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.FamilyName = params.FamilyName
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.MiddleName != nil && refs.StringValue(user.MiddleName) != refs.StringValue(params.MiddleName) {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.MiddleName = params.MiddleName
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.Nickname != nil && refs.StringValue(user.Nickname) != refs.StringValue(params.Nickname) {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Nickname = params.Nickname
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.Birthdate != nil && refs.StringValue(user.Birthdate) != refs.StringValue(params.Birthdate) {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Birthdate = params.Birthdate
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.Gender != nil && refs.StringValue(user.Gender) != refs.StringValue(params.Gender) {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Gender = params.Gender
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.PhoneNumber != nil && refs.StringValue(user.PhoneNumber) != refs.StringValue(params.PhoneNumber) {
|
2022-12-24 21:52:42 +00:00
|
|
|
// verify if phone number is unique
|
|
|
|
if _, err := db.Provider.GetUserByPhoneNumber(ctx, strings.TrimSpace(refs.StringValue(params.PhoneNumber))); err == nil {
|
|
|
|
log.Debug("user with given phone number already exists")
|
|
|
|
return nil, errors.New("user with given phone number already exists")
|
|
|
|
}
|
2021-12-22 10:01:45 +00:00
|
|
|
user.PhoneNumber = params.PhoneNumber
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.Picture != nil && refs.StringValue(user.Picture) != refs.StringValue(params.Picture) {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Picture = params.Picture
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
2023-08-14 06:31:37 +00:00
|
|
|
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-07-23 09:56:44 +00:00
|
|
|
if params.IsMultiFactorAuthEnabled != nil && refs.BoolValue(user.IsMultiFactorAuthEnabled) != refs.BoolValue(params.IsMultiFactorAuthEnabled) {
|
2022-07-29 14:19:50 +00:00
|
|
|
if refs.BoolValue(params.IsMultiFactorAuthEnabled) {
|
|
|
|
isEnvServiceEnabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyIsEmailServiceEnabled)
|
|
|
|
if err != nil || !isEnvServiceEnabled {
|
|
|
|
log.Debug("Email service not enabled:")
|
|
|
|
return nil, errors.New("email service not enabled, so cannot enable multi factor authentication")
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 08:42:36 +00:00
|
|
|
|
2022-08-03 17:50:23 +00:00
|
|
|
isMFAEnforced, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyEnforceMultiFactorAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("MFA service not enabled: ", err)
|
|
|
|
isMFAEnforced = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if isMFAEnforced && !refs.BoolValue(params.IsMultiFactorAuthEnabled) {
|
|
|
|
log.Debug("Cannot disable mfa service as it is enforced:")
|
|
|
|
return nil, errors.New("cannot disable multi factor authentication as it is enforced by organization")
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:42:36 +00:00
|
|
|
user.IsMultiFactorAuthEnabled = params.IsMultiFactorAuthEnabled
|
2022-07-23 09:56:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 15:15:21 +00:00
|
|
|
isPasswordChanging := false
|
|
|
|
if params.NewPassword != nil && params.ConfirmNewPassword == nil {
|
|
|
|
isPasswordChanging = true
|
|
|
|
log.Debug("confirm password is empty")
|
|
|
|
return res, fmt.Errorf("confirm password is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.ConfirmNewPassword != nil && params.NewPassword == nil {
|
|
|
|
isPasswordChanging = true
|
|
|
|
log.Debug("new password is empty")
|
|
|
|
return res, fmt.Errorf("new password is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.NewPassword != nil && params.ConfirmNewPassword != nil {
|
|
|
|
isPasswordChanging = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPasswordChanging && user.Password != nil && params.OldPassword == nil {
|
|
|
|
log.Debug("old password is empty")
|
|
|
|
return res, fmt.Errorf("old password is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPasswordChanging && user.Password != nil && params.OldPassword != nil {
|
2022-07-15 16:41:08 +00:00
|
|
|
if err = bcrypt.CompareHashAndPassword([]byte(refs.StringValue(user.Password)), []byte(refs.StringValue(params.OldPassword))); err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to compare hash and old password: ", err)
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf("incorrect old password")
|
|
|
|
}
|
2022-07-13 15:15:21 +00:00
|
|
|
}
|
2021-07-18 03:55:20 +00:00
|
|
|
|
2022-07-13 15:15:21 +00:00
|
|
|
shouldAddBasicSignUpMethod := false
|
|
|
|
isBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting basic auth disabled: ", err)
|
|
|
|
isBasicAuthDisabled = true
|
|
|
|
}
|
2021-07-18 03:55:20 +00:00
|
|
|
|
2022-12-21 17:44:24 +00:00
|
|
|
isMobileBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableMobileBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting mobile basic auth disabled: ", err)
|
|
|
|
isBasicAuthDisabled = true
|
|
|
|
}
|
|
|
|
|
2022-07-13 15:15:21 +00:00
|
|
|
if params.NewPassword != nil && params.ConfirmNewPassword != nil {
|
2022-12-21 17:44:24 +00:00
|
|
|
if isBasicAuthDisabled || isMobileBasicAuthDisabled {
|
2022-07-13 15:15:21 +00:00
|
|
|
log.Debug("Cannot update password as basic authentication is disabled")
|
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if refs.StringValue(params.ConfirmNewPassword) != refs.StringValue(params.NewPassword) {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Failed to compare new password and confirm new password")
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`password and confirm password does not match`)
|
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if user.Password == nil || refs.StringValue(user.Password) == "" {
|
2022-07-13 15:15:21 +00:00
|
|
|
shouldAddBasicSignUpMethod = true
|
|
|
|
}
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if err := validators.IsValidPassword(refs.StringValue(params.NewPassword)); err != nil {
|
2022-07-13 15:15:21 +00:00
|
|
|
log.Debug("Invalid password")
|
|
|
|
return res, err
|
|
|
|
}
|
2021-07-18 03:55:20 +00:00
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
password, _ := crypto.EncryptPassword(refs.StringValue(params.NewPassword))
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Password = &password
|
2022-07-13 15:15:21 +00:00
|
|
|
|
|
|
|
if shouldAddBasicSignUpMethod {
|
|
|
|
user.SignupMethods = user.SignupMethods + "," + constants.AuthRecipeMethodBasicAuth
|
|
|
|
}
|
2021-07-18 03:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasEmailChanged := false
|
|
|
|
|
2022-07-15 16:41:08 +00:00
|
|
|
if params.Email != nil && user.Email != refs.StringValue(params.Email) {
|
2021-07-18 03:55:20 +00:00
|
|
|
// check if valid email
|
2022-05-30 06:24:16 +00:00
|
|
|
if !validators.IsValidEmail(*params.Email) {
|
2022-07-15 16:41:08 +00:00
|
|
|
log.Debug("Failed to validate email: ", refs.StringValue(params.Email))
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf("invalid email address")
|
|
|
|
}
|
|
|
|
newEmail := strings.ToLower(*params.Email)
|
2022-05-25 07:00:22 +00:00
|
|
|
|
|
|
|
// check if valid email
|
2022-05-30 06:24:16 +00:00
|
|
|
if !validators.IsValidEmail(newEmail) {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to validate new email: ", newEmail)
|
|
|
|
return res, fmt.Errorf("invalid new email address")
|
|
|
|
}
|
2021-07-18 03:55:20 +00:00
|
|
|
// check if user with new email exists
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err := db.Provider.GetUserByEmail(ctx, newEmail)
|
2021-07-18 03:55:20 +00:00
|
|
|
// err = nil means user exists
|
|
|
|
if err == nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to get user by email: ", newEmail)
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf("user with this email address already exists")
|
|
|
|
}
|
|
|
|
|
2022-06-11 13:40:39 +00:00
|
|
|
go memorystore.Provider.DeleteAllUserSessions(user.ID)
|
2022-05-24 07:12:29 +00:00
|
|
|
go cookie.DeleteSession(gc)
|
2021-07-18 03:55:20 +00:00
|
|
|
|
|
|
|
user.Email = newEmail
|
2022-05-30 03:49:55 +00:00
|
|
|
isEmailVerificationDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableEmailVerification)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to get disable email verification env variable: ", err)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if !isEmailVerificationDisabled {
|
2022-05-30 06:24:16 +00:00
|
|
|
hostname := parsers.GetHost(gc)
|
2022-03-02 12:12:31 +00:00
|
|
|
user.EmailVerifiedAt = nil
|
|
|
|
hasEmailChanged = true
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
verificationType := constants.VerificationTypeUpdateEmail
|
2022-05-30 06:24:16 +00:00
|
|
|
redirectURL := parsers.GetAppURL(gc)
|
2022-03-08 07:06:26 +00:00
|
|
|
verificationToken, err := token.CreateVerificationToken(newEmail, verificationType, hostname, nonceHash, redirectURL)
|
2022-03-02 12:12:31 +00:00
|
|
|
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
|
|
|
return res, err
|
2022-03-02 12:12:31 +00:00
|
|
|
}
|
2023-07-31 11:12:11 +00:00
|
|
|
_, err = db.Provider.AddVerificationRequest(ctx, &models.VerificationRequest{
|
2022-03-08 07:06:26 +00:00
|
|
|
Token: verificationToken,
|
|
|
|
Identifier: verificationType,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: newEmail,
|
|
|
|
Nonce: nonceHash,
|
|
|
|
RedirectURI: redirectURL,
|
2022-03-02 12:12:31 +00:00
|
|
|
})
|
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
|
|
|
return res, err
|
|
|
|
}
|
2022-03-02 12:12:31 +00:00
|
|
|
|
2022-08-08 20:13:37 +00:00
|
|
|
// exec it as go routine so that we can reduce the api latency
|
2022-08-13 06:04:24 +00:00
|
|
|
go email.SendEmail([]string{user.Email}, verificationType, map[string]interface{}{
|
2022-08-08 20:13:37 +00:00
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
2023-05-02 13:09:10 +00:00
|
|
|
"verification_url": utils.GetEmailVerificationURL(verificationToken, hostname, redirectURL),
|
2022-08-08 20:13:37 +00:00
|
|
|
})
|
2021-07-18 03:55:20 +00:00
|
|
|
|
2022-03-02 12:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err = db.Provider.UpdateUser(ctx, user)
|
2021-08-04 06:48:57 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to update user: ", err)
|
2021-08-04 06:48:57 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2021-07-18 03:55:20 +00:00
|
|
|
message := `Profile details updated successfully.`
|
|
|
|
if hasEmailChanged {
|
|
|
|
message += `For the email change we have sent new verification email, please verify and continue`
|
|
|
|
}
|
|
|
|
res = &model.Response{
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|