2021-07-17 16:29:50 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-03-25 12:21:20 +00:00
|
|
|
"time"
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2022-10-23 15:38:08 +00:00
|
|
|
"github.com/google/uuid"
|
2022-05-24 07:12:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
2021-07-28 10:13:08 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/cookie"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2022-04-23 12:22:02 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2023-10-21 21:03:36 +00:00
|
|
|
mailService "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-07-28 05:48:06 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
2023-10-21 21:03:36 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/smsproviders"
|
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-17 16:29:50 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// LoginResolver is a resolver for login mutation
|
2023-10-21 21:03:36 +00:00
|
|
|
// User can login with email or phone number, but not both
|
2022-01-17 06:02:13 +00:00
|
|
|
func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthResponse, error) {
|
2021-07-28 10:13:08 +00:00
|
|
|
var res *model.AuthResponse
|
2022-05-24 07:12:29 +00:00
|
|
|
|
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2021-07-17 16:29:50 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2021-07-17 16:29:50 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2023-08-29 12:44:48 +00:00
|
|
|
isBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication)
|
2022-05-30 03:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting basic auth disabled: ", err)
|
2023-08-29 12:44:48 +00:00
|
|
|
isBasicAuthDisabled = true
|
2022-05-30 03:49:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 21:03:36 +00:00
|
|
|
isMobileBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableMobileBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting mobile basic auth disabled: ", err)
|
|
|
|
isMobileBasicAuthDisabled = true
|
2021-07-28 10:13:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 21:03:36 +00:00
|
|
|
email := refs.StringValue(params.Email)
|
|
|
|
phoneNumber := refs.StringValue(params.PhoneNumber)
|
|
|
|
if email == "" && phoneNumber == "" {
|
|
|
|
log.Debug("Email or phone number is required")
|
|
|
|
return res, fmt.Errorf(`email or phone number is required`)
|
|
|
|
}
|
2022-05-24 07:12:29 +00:00
|
|
|
log := log.WithFields(log.Fields{
|
2023-10-21 21:03:36 +00:00
|
|
|
"email": refs.StringValue(params.Email),
|
|
|
|
"phone_number": refs.StringValue(params.PhoneNumber),
|
2022-05-24 07:12:29 +00:00
|
|
|
})
|
2023-10-21 21:03:36 +00:00
|
|
|
isEmailLogin := email != ""
|
|
|
|
isMobileLogin := phoneNumber != ""
|
|
|
|
if isBasicAuthDisabled {
|
|
|
|
log.Debug("Basic authentication is disabled.")
|
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
|
|
|
if isMobileBasicAuthDisabled && isMobileLogin {
|
|
|
|
log.Debug("Mobile basic authentication is disabled.")
|
|
|
|
return res, fmt.Errorf(`mobile basic authentication is disabled for this instance`)
|
|
|
|
}
|
|
|
|
var user *models.User
|
|
|
|
if isEmailLogin {
|
|
|
|
user, err = db.Provider.GetUserByEmail(ctx, email)
|
|
|
|
} else {
|
|
|
|
user, err = db.Provider.GetUserByPhoneNumber(ctx, phoneNumber)
|
|
|
|
}
|
2021-07-17 16:29:50 +00:00
|
|
|
if err != nil {
|
2023-10-21 21:03:36 +00:00
|
|
|
log.Debug("Failed to get user: ", err)
|
2022-09-03 16:18:33 +00:00
|
|
|
return res, fmt.Errorf(`bad user credentials`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
2022-03-24 08:43:55 +00:00
|
|
|
if user.RevokedTimestamp != nil {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("User access is revoked")
|
2022-03-24 08:43:55 +00:00
|
|
|
return res, fmt.Errorf(`user access has been revoked`)
|
|
|
|
}
|
2023-10-21 21:03:36 +00:00
|
|
|
if isEmailLogin {
|
|
|
|
if !strings.Contains(user.SignupMethods, constants.AuthRecipeMethodBasicAuth) {
|
|
|
|
log.Debug("User signup method is not basic auth")
|
|
|
|
return res, fmt.Errorf(`user has not signed up email & password`)
|
|
|
|
}
|
2022-03-24 08:43:55 +00:00
|
|
|
|
2023-10-21 21:03:36 +00:00
|
|
|
if user.EmailVerifiedAt == nil {
|
|
|
|
log.Debug("User email is not verified")
|
|
|
|
return res, fmt.Errorf(`email not verified`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !strings.Contains(user.SignupMethods, constants.AuthRecipeMethodMobileBasicAuth) {
|
|
|
|
log.Debug("User signup method is not mobile basic auth")
|
|
|
|
return res, fmt.Errorf(`user has not signed up with phone number & password`)
|
|
|
|
}
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2023-10-21 21:03:36 +00:00
|
|
|
if user.PhoneNumberVerifiedAt == nil {
|
|
|
|
log.Debug("User phone number is not verified")
|
|
|
|
return res, fmt.Errorf(`phone number is not verified`)
|
|
|
|
}
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
2021-12-22 10:01:45 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(*user.Password), []byte(params.Password))
|
2021-07-17 16:29:50 +00:00
|
|
|
if err != nil {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Failed to compare password: ", err)
|
2022-09-03 16:18:33 +00:00
|
|
|
return res, fmt.Errorf(`bad user credentials`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
2022-05-31 02:44:03 +00:00
|
|
|
defaultRolesString, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyDefaultRoles)
|
|
|
|
roles := []string{}
|
2022-05-30 03:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting default roles: ", err)
|
2022-05-31 02:44:03 +00:00
|
|
|
defaultRolesString = ""
|
|
|
|
} else {
|
|
|
|
roles = strings.Split(defaultRolesString, ",")
|
2022-05-30 03:49:55 +00:00
|
|
|
}
|
2021-10-13 16:41:41 +00:00
|
|
|
currentRoles := strings.Split(user.Roles, ",")
|
|
|
|
if len(params.Roles) > 0 {
|
2022-05-30 06:24:16 +00:00
|
|
|
if !validators.IsValidRoles(params.Roles, currentRoles) {
|
2022-05-25 07:00:22 +00:00
|
|
|
log.Debug("Invalid roles: ", params.Roles)
|
2021-10-13 16:41:41 +00:00
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
2021-10-13 16:41:41 +00:00
|
|
|
roles = params.Roles
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
2022-03-02 12:12:31 +00:00
|
|
|
scope := []string{"openid", "email", "profile"}
|
|
|
|
if params.Scope != nil && len(scope) > 0 {
|
|
|
|
scope = params.Scope
|
|
|
|
}
|
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)
|
|
|
|
}
|
2023-10-21 21:03:36 +00:00
|
|
|
isSMSServiceEnabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyIsSMSServiceEnabled)
|
|
|
|
if err != nil || !isSMSServiceEnabled {
|
|
|
|
log.Debug("SMS service not enabled: ", err)
|
|
|
|
}
|
2022-08-02 08:42:36 +00:00
|
|
|
|
2022-08-03 17:50:23 +00:00
|
|
|
isMFADisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableMultiFactorAuthentication)
|
2023-07-23 04:33:37 +00:00
|
|
|
if err != nil || !isMFADisabled {
|
2022-08-03 17:50:23 +00:00
|
|
|
log.Debug("MFA service not enabled: ", err)
|
|
|
|
}
|
|
|
|
|
2023-10-21 21:03:36 +00:00
|
|
|
if refs.BoolValue(user.IsMultiFactorAuthEnabled) && !isMFADisabled {
|
2022-07-29 14:19:50 +00:00
|
|
|
otp := utils.GenerateOTP()
|
2023-07-20 07:11:39 +00:00
|
|
|
expires := time.Now().Add(1 * time.Minute).Unix()
|
2022-07-29 14:19:50 +00:00
|
|
|
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,
|
2023-07-20 07:11:39 +00:00
|
|
|
ExpiresAt: expires,
|
2022-07-29 08:19:46 +00:00
|
|
|
})
|
2022-07-29 14:19:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add otp: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-20 07:11:39 +00:00
|
|
|
mfaSession := uuid.NewString()
|
2023-07-24 03:58:36 +00:00
|
|
|
err = memorystore.Provider.SetMfaSession(user.ID, mfaSession, expires)
|
2023-07-20 07:11:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add mfasession: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cookie.SetMfaSession(gc, mfaSession)
|
2023-10-21 21:03:36 +00:00
|
|
|
if isEmailServiceEnabled && isEmailLogin {
|
|
|
|
go func() {
|
|
|
|
// exec it as go routine so that we can reduce the api latency
|
|
|
|
if err := mailService.SendEmail([]string{email}, constants.VerificationTypeOTP, map[string]interface{}{
|
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
|
|
|
"otp": otpData.Otp,
|
|
|
|
}); err != nil {
|
|
|
|
log.Debug("Failed to send otp email: ", err)
|
|
|
|
}
|
|
|
|
utils.RegisterEvent(ctx, constants.UserLoginWebhookEvent, constants.AuthRecipeMethodBasicAuth, user)
|
|
|
|
}()
|
|
|
|
} else if isSMSServiceEnabled && isMobileLogin {
|
|
|
|
smsBody := strings.Builder{}
|
|
|
|
smsBody.WriteString("Your verification code is: ")
|
|
|
|
smsBody.WriteString(otpData.Otp)
|
|
|
|
go func() {
|
|
|
|
utils.RegisterEvent(ctx, constants.UserLoginWebhookEvent, constants.AuthRecipeMethodMobileBasicAuth, user)
|
|
|
|
if err := smsproviders.SendSMS(phoneNumber, smsBody.String()); err != nil {
|
|
|
|
log.Debug("Failed to send sms: ", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2022-07-28 05:48:06 +00:00
|
|
|
return &model.AuthResponse{
|
2023-10-21 21:03:36 +00:00
|
|
|
Message: "Please check the OTP in",
|
|
|
|
ShouldShowEmailOtpScreen: refs.NewBoolRef(isEmailLogin),
|
|
|
|
ShouldShowMobileOtpScreen: refs.NewBoolRef(isMobileLogin),
|
2022-07-28 05:48:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-12 18:24:37 +00:00
|
|
|
code := ""
|
|
|
|
codeChallenge := ""
|
2022-11-15 16:15:08 +00:00
|
|
|
nonce := ""
|
2022-11-12 18:24:37 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 16:15:08 +00:00
|
|
|
if nonce == "" {
|
|
|
|
nonce = uuid.New().String()
|
|
|
|
}
|
2022-11-12 19:52:21 +00:00
|
|
|
authToken, err := token.CreateAuthToken(gc, user, roles, scope, constants.AuthRecipeMethodBasicAuth, nonce, code)
|
2022-01-22 19:54:41 +00:00
|
|
|
if err != nil {
|
2022-05-24 07:12:29 +00:00
|
|
|
log.Debug("Failed to create auth token", err)
|
2022-01-22 19:54:41 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2022-11-15 16:15:08 +00:00
|
|
|
// TODO add to other login options as well
|
|
|
|
// 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 12:21:20 +00:00
|
|
|
expiresIn := authToken.AccessToken.ExpiresAt - time.Now().Unix()
|
|
|
|
if expiresIn <= 0 {
|
|
|
|
expiresIn = 1
|
|
|
|
}
|
|
|
|
|
2021-07-28 10:13:08 +00:00
|
|
|
res = &model.AuthResponse{
|
2021-12-22 05:21:12 +00:00
|
|
|
Message: `Logged in successfully`,
|
2022-01-22 19:54:41 +00:00
|
|
|
AccessToken: &authToken.AccessToken.Token,
|
2022-03-02 12:12:31 +00:00
|
|
|
IDToken: &authToken.IDToken.Token,
|
|
|
|
ExpiresIn: &expiresIn,
|
2022-01-22 19:54:41 +00:00
|
|
|
User: user.AsAPIUser(),
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 07:06:26 +00:00
|
|
|
cookie.SetSession(gc, authToken.FingerPrintHash)
|
2022-06-29 16:54:00 +00:00
|
|
|
sessionStoreKey := constants.AuthRecipeMethodBasicAuth + ":" + user.ID
|
2023-04-08 07:36:15 +00:00
|
|
|
memorystore.Provider.SetUserSession(sessionStoreKey, constants.TokenTypeSessionToken+"_"+authToken.FingerPrint, authToken.FingerPrintHash, authToken.SessionTokenExpiresAt)
|
|
|
|
memorystore.Provider.SetUserSession(sessionStoreKey, constants.TokenTypeAccessToken+"_"+authToken.FingerPrint, authToken.AccessToken.Token, authToken.AccessToken.ExpiresAt)
|
2022-03-02 12:12:31 +00:00
|
|
|
|
|
|
|
if authToken.RefreshToken != nil {
|
|
|
|
res.RefreshToken = &authToken.RefreshToken.Token
|
2023-04-08 07:36:15 +00:00
|
|
|
memorystore.Provider.SetUserSession(sessionStoreKey, constants.TokenTypeRefreshToken+"_"+authToken.FingerPrint, authToken.RefreshToken.Token, authToken.RefreshToken.ExpiresAt)
|
2022-03-02 12:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 05:12:42 +00:00
|
|
|
go func() {
|
2023-10-21 21:03:36 +00:00
|
|
|
// Register event
|
|
|
|
if isEmailLogin {
|
|
|
|
utils.RegisterEvent(ctx, constants.UserLoginWebhookEvent, constants.AuthRecipeMethodBasicAuth, user)
|
|
|
|
} else {
|
|
|
|
utils.RegisterEvent(ctx, constants.UserLoginWebhookEvent, constants.AuthRecipeMethodMobileBasicAuth, user)
|
|
|
|
}
|
|
|
|
// Record session
|
2023-07-31 11:12:11 +00:00
|
|
|
db.Provider.AddSession(ctx, &models.Session{
|
2022-07-11 05:12:42 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
UserAgent: utils.GetUserAgent(gc.Request),
|
|
|
|
IP: utils.GetIP(gc.Request),
|
|
|
|
})
|
|
|
|
}()
|
2022-03-02 12:12:31 +00:00
|
|
|
|
2021-07-17 16:29:50 +00:00
|
|
|
return res, nil
|
|
|
|
}
|