2021-07-17 21:59:50 +05:30
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-03-25 15:21:20 +03:00
|
|
|
"time"
|
2021-07-17 21:59:50 +05:30
|
|
|
|
2022-05-24 12:42:29 +05:30
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
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"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2022-04-23 17:52:02 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-07-29 19:49:50 +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-07-28 11:18:06 +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
|
|
|
// LoginResolver is a resolver for login mutation
|
|
|
|
func LoginResolver(ctx context.Context, params model.LoginInput) (*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-17 21:59:50 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2021-07-17 21:59:50 +05:30
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-05-30 09:19:55 +05:30
|
|
|
isBasiAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting basic auth disabled: ", err)
|
|
|
|
isBasiAuthDisabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isBasiAuthDisabled {
|
2022-05-24 12:42:29 +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-05-24 12:42:29 +05:30
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"email": params.Email,
|
|
|
|
})
|
2021-07-17 21:59:50 +05:30
|
|
|
params.Email = strings.ToLower(params.Email)
|
2022-07-10 21:49:33 +05:30
|
|
|
user, 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)
|
2022-09-03 21:48:33 +05:30
|
|
|
return res, fmt.Errorf(`bad user credentials`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2022-03-24 14:13:55 +05:30
|
|
|
if user.RevokedTimestamp != nil {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("User access is revoked")
|
2022-03-24 14:13:55 +05:30
|
|
|
return res, fmt.Errorf(`user access has been revoked`)
|
|
|
|
}
|
|
|
|
|
2022-06-29 22:24:00 +05:30
|
|
|
if !strings.Contains(user.SignupMethods, constants.AuthRecipeMethodBasicAuth) {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("User signup method is not basic auth")
|
2021-07-18 09:25:20 +05:30
|
|
|
return res, fmt.Errorf(`user has not signed up email & password`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2021-12-22 15:38:51 +05:30
|
|
|
if user.EmailVerifiedAt == nil {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("User email is not verified")
|
2021-07-18 09:25:20 +05:30
|
|
|
return res, fmt.Errorf(`email not verified`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
2021-07-18 09:52:54 +05:30
|
|
|
|
2021-12-22 15:31:45 +05:30
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(*user.Password), []byte(params.Password))
|
2021-07-17 21:59:50 +05:30
|
|
|
|
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to compare password: ", err)
|
2022-09-03 21:48:33 +05:30
|
|
|
return res, fmt.Errorf(`bad user credentials`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
2022-05-30 09:19:55 +05:30
|
|
|
|
2022-05-31 08:14:03 +05:30
|
|
|
defaultRolesString, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyDefaultRoles)
|
|
|
|
roles := []string{}
|
2022-05-30 09:19:55 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting default roles: ", err)
|
2022-05-31 08:14:03 +05:30
|
|
|
defaultRolesString = ""
|
|
|
|
} else {
|
|
|
|
roles = strings.Split(defaultRolesString, ",")
|
2022-05-30 09:19:55 +05:30
|
|
|
}
|
2022-05-31 08:14:03 +05:30
|
|
|
|
2021-10-13 22:11:41 +05:30
|
|
|
currentRoles := strings.Split(user.Roles, ",")
|
|
|
|
if len(params.Roles) > 0 {
|
2022-11-07 17:33:13 +01:00
|
|
|
if params.Roles[0] = "all_allowed_roles" {
|
|
|
|
roles = currentRoles
|
|
|
|
} else {
|
|
|
|
if !validators.IsValidRoles(params.Roles, currentRoles) {
|
|
|
|
log.Debug("Invalid roles: ", params.Roles)
|
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
|
|
|
}
|
|
|
|
|
|
|
|
roles = params.Roles
|
2021-09-20 10:36:26 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-17 21:59:50 +05:30
|
|
|
|
2022-03-02 17:42:31 +05:30
|
|
|
scope := []string{"openid", "email", "profile"}
|
|
|
|
if params.Scope != nil && len(scope) > 0 {
|
|
|
|
scope = params.Scope
|
|
|
|
}
|
|
|
|
|
2022-08-02 14:12:36 +05:30
|
|
|
isEmailServiceEnabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyIsEmailServiceEnabled)
|
|
|
|
if err != nil || !isEmailServiceEnabled {
|
|
|
|
log.Debug("Email service not enabled: ", err)
|
|
|
|
}
|
|
|
|
|
2022-08-03 23:20:23 +05:30
|
|
|
isMFADisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableMultiFactorAuthentication)
|
|
|
|
if err != nil || !isEmailServiceEnabled {
|
|
|
|
log.Debug("MFA service not enabled: ", err)
|
|
|
|
}
|
|
|
|
|
2022-08-02 14:12:36 +05:30
|
|
|
// If email service is not enabled continue the process in any way
|
2022-08-03 23:20:23 +05:30
|
|
|
if refs.BoolValue(user.IsMultiFactorAuthEnabled) && isEmailServiceEnabled && !isMFADisabled {
|
2022-07-29 19:49:50 +05:30
|
|
|
otp := utils.GenerateOTP()
|
|
|
|
otpData, err := db.Provider.UpsertOTP(ctx, &models.OTP{
|
2022-07-29 13:49:46 +05:30
|
|
|
Email: user.Email,
|
2022-07-29 19:49:50 +05:30
|
|
|
Otp: otp,
|
2022-07-29 13:49:46 +05:30
|
|
|
ExpiresAt: time.Now().Add(1 * time.Minute).Unix(),
|
|
|
|
})
|
2022-07-29 19:49:50 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to add otp: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2022-08-09 01:43:37 +05:30
|
|
|
// exec it as go routine so that we can reduce the api latency
|
|
|
|
go email.SendEmail([]string{params.Email}, constants.VerificationTypeOTP, map[string]interface{}{
|
|
|
|
"user": user.ToMap(),
|
|
|
|
"organization": utils.GetOrganization(),
|
|
|
|
"otp": otpData.Otp,
|
|
|
|
})
|
2022-07-29 19:49:50 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Debug("Failed to send otp email: ", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-07-28 11:18:06 +05:30
|
|
|
return &model.AuthResponse{
|
|
|
|
Message: "Please check the OTP in your inbox",
|
|
|
|
ShouldShowOtpScreen: refs.NewBoolRef(true),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-06-29 22:24:00 +05:30
|
|
|
authToken, err := token.CreateAuthToken(gc, user, roles, scope, constants.AuthRecipeMethodBasicAuth)
|
2022-01-23 01:24:41 +05:30
|
|
|
if err != nil {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("Failed to create auth token", err)
|
2022-01-23 01:24:41 +05:30
|
|
|
return res, err
|
|
|
|
}
|
2021-07-17 21:59:50 +05:30
|
|
|
|
2022-03-25 15:21:20 +03:00
|
|
|
expiresIn := authToken.AccessToken.ExpiresAt - time.Now().Unix()
|
|
|
|
if expiresIn <= 0 {
|
|
|
|
expiresIn = 1
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:43:08 +05:30
|
|
|
res = &model.AuthResponse{
|
2021-12-22 10:51:12 +05:30
|
|
|
Message: `Logged in successfully`,
|
2022-01-23 01:24:41 +05:30
|
|
|
AccessToken: &authToken.AccessToken.Token,
|
2022-03-02 17:42:31 +05:30
|
|
|
IDToken: &authToken.IDToken.Token,
|
|
|
|
ExpiresIn: &expiresIn,
|
2022-01-23 01:24:41 +05:30
|
|
|
User: user.AsAPIUser(),
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2022-03-08 12:36:26 +05:30
|
|
|
cookie.SetSession(gc, authToken.FingerPrintHash)
|
2022-06-29 22:24:00 +05:30
|
|
|
sessionStoreKey := constants.AuthRecipeMethodBasicAuth + ":" + user.ID
|
|
|
|
memorystore.Provider.SetUserSession(sessionStoreKey, constants.TokenTypeSessionToken+"_"+authToken.FingerPrint, authToken.FingerPrintHash)
|
|
|
|
memorystore.Provider.SetUserSession(sessionStoreKey, constants.TokenTypeAccessToken+"_"+authToken.FingerPrint, authToken.AccessToken.Token)
|
2022-03-02 17:42:31 +05:30
|
|
|
|
|
|
|
if authToken.RefreshToken != nil {
|
|
|
|
res.RefreshToken = &authToken.RefreshToken.Token
|
2022-06-29 22:24:00 +05:30
|
|
|
memorystore.Provider.SetUserSession(sessionStoreKey, constants.TokenTypeRefreshToken+"_"+authToken.FingerPrint, authToken.RefreshToken.Token)
|
2022-03-02 17:42:31 +05:30
|
|
|
}
|
|
|
|
|
2022-07-11 10:42:42 +05:30
|
|
|
go func() {
|
|
|
|
utils.RegisterEvent(ctx, constants.UserLoginWebhookEvent, constants.AuthRecipeMethodBasicAuth, user)
|
|
|
|
db.Provider.AddSession(ctx, models.Session{
|
|
|
|
UserID: user.ID,
|
|
|
|
UserAgent: utils.GetUserAgent(gc.Request),
|
|
|
|
IP: utils.GetIP(gc.Request),
|
|
|
|
})
|
|
|
|
}()
|
2022-03-02 17:42:31 +05:30
|
|
|
|
2021-07-17 21:59:50 +05:30
|
|
|
return res, nil
|
|
|
|
}
|