Updated mobile signup to send sms when service enabled

This commit is contained in:
Mussie Teshome 2023-06-11 15:59:53 +03:00
parent 7749534087
commit 8dc7366182

View File

@ -19,15 +19,10 @@ import (
"github.com/authorizerdev/authorizer/server/refs" "github.com/authorizerdev/authorizer/server/refs"
"github.com/authorizerdev/authorizer/server/token" "github.com/authorizerdev/authorizer/server/token"
"github.com/authorizerdev/authorizer/server/utils" "github.com/authorizerdev/authorizer/server/utils"
"github.com/authorizerdev/authorizer/server/smsproviders"
"github.com/authorizerdev/authorizer/server/validators" "github.com/authorizerdev/authorizer/server/validators"
// "github.com/twilio/twilio-go"
// api "github.com/twilio/twilio-go/rest/api/v2010"
) )
// Send Message With Twilio
// Verify It
// Then set phone_verified_at ..
// MobileSignupResolver is a resolver for mobile_basic_auth_signup mutation // MobileSignupResolver is a resolver for mobile_basic_auth_signup mutation
func MobileSignupResolver(ctx context.Context, params *model.MobileSignUpInput) (*model.AuthResponse, error) { func MobileSignupResolver(ctx context.Context, params *model.MobileSignUpInput) (*model.AuthResponse, error) {
var res *model.AuthResponse var res *model.AuthResponse
@ -137,40 +132,11 @@ func MobileSignupResolver(ctx context.Context, params *model.MobileSignUpInput)
} }
} }
now := time.Now().Unix()
user := models.User{ user := models.User{
Email: emailInput, Email: emailInput,
PhoneNumber: &mobile, PhoneNumber: &mobile,
PhoneNumberVerifiedAt: &now,
} }
// create the model sms_verification_requests
// insert the data into sms_verification_requests
// while inserting - encrypt the code
// give max mins to verify and (10m - configurable)
// new mutation verify sms - to compare against
// check if it is verified - mobile login - set phone_number_verified_at (throw phone_not_verified_error if not)
// client := twilio.NewRestClientWithParams(twilio.ClientParams{
// Username: "AC2fa25c42aebbb4adecf321f98f2378f8",
// Password: "80d261d70d81a7838df0ab30e3b0b837",
// })
// paramTwilio := &api.CreateMessageParams{}
// paramTwilio.SetBody("The mobile signup here")
// paramTwilio.SetFrom("+13655360739")
// paramTwilio.SetTo(mobile)
// resp, err := client.Api.CreateMessage(paramTwilio)
// if err != nil {
// log.Info("Error getting default roles: ", err.Error())
// } else {
// if resp.Sid != nil {
// log.Info("--Good--")
// } else {
// log.Info("-- --")
// }
// }
user.Roles = strings.Join(inputRoles, ",") user.Roles = strings.Join(inputRoles, ",")
password, _ := crypto.EncryptPassword(params.Password) password, _ := crypto.EncryptPassword(params.Password)
@ -213,17 +179,51 @@ func MobileSignupResolver(ctx context.Context, params *model.MobileSignUpInput)
log.Debug("MFA service not enabled: ", err) log.Debug("MFA service not enabled: ", err)
isMFAEnforced = false isMFAEnforced = false
} }
if isMFAEnforced { if isMFAEnforced {
user.IsMultiFactorAuthEnabled = refs.NewBoolRef(true) user.IsMultiFactorAuthEnabled = refs.NewBoolRef(true)
} }
disablePhoneVerification, _ := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisablePhoneVerification)
log.Errorf("ooioioioioioioioi: ", disablePhoneVerification)
if disablePhoneVerification {
now := time.Now().Unix()
user.PhoneNumberVerifiedAt = &now
}
user.SignupMethods = constants.AuthRecipeMethodMobileBasicAuth user.SignupMethods = constants.AuthRecipeMethodMobileBasicAuth
user, err = db.Provider.AddUser(ctx, user) user, err = db.Provider.AddUser(ctx, user)
if err != nil { if err != nil {
log.Debug("Failed to add user: ", err) log.Debug("Failed to add user: ", err)
return res, err return res, err
} }
if !disablePhoneVerification {
duration, _ := time.ParseDuration("10m")
smsCode := utils.GenerateOTP()
smsBody := strings.Builder{}
smsBody.WriteString("Your verification code is: ")
smsBody.WriteString(smsCode)
// TODO: For those who enabled the webhook to call their sms vendor separately - sending the otp to their api
if err != nil {
log.Debug("error while upserting user: ", err.Error())
return nil, err
}
go func() {
db.Provider.UpsertSMSRequest(ctx, &models.SMSVerificationRequest{
PhoneNumber: mobile,
Code: smsCode,
CodeExpiresAt: time.Now().Add(duration).Unix(),
})
smsproviders.SendSMS(mobile, smsBody.String())
}()
}
roles := strings.Split(user.Roles, ",") roles := strings.Split(user.Roles, ",")
userToReturn := user.AsAPIUser() userToReturn := user.AsAPIUser()