2021-07-17 16:29:50 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2021-07-28 10:13:08 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
|
|
|
"github.com/authorizerdev/authorizer/server/enum"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
|
|
|
"github.com/authorizerdev/authorizer/server/session"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-17 16:29:50 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2021-07-28 10:13:08 +00:00
|
|
|
func Login(ctx context.Context, params model.LoginInput) (*model.AuthResponse, error) {
|
2021-07-17 16:29:50 +00:00
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2021-07-28 10:13:08 +00:00
|
|
|
var res *model.AuthResponse
|
2021-07-17 16:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-12-20 12:03:11 +00:00
|
|
|
if constants.DISABLE_BASIC_AUTHENTICATION {
|
2021-07-28 10:13:08 +00:00
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
|
|
|
|
2021-07-17 16:29:50 +00:00
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
user, err := db.Mgr.GetUserByEmail(params.Email)
|
|
|
|
if err != nil {
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`user with this email not found`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
if !strings.Contains(user.SignupMethods, enum.BasicAuth.String()) {
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`user has not signed up email & password`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 10:08:51 +00:00
|
|
|
if user.EmailVerifiedAt == nil {
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`email not verified`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
2021-07-18 04:22:54 +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 {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println("compare password error:", err)
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`invalid password`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
2021-10-13 16:41:41 +00:00
|
|
|
roles := constants.DEFAULT_ROLES
|
|
|
|
currentRoles := strings.Split(user.Roles, ",")
|
|
|
|
if len(params.Roles) > 0 {
|
|
|
|
if !utils.IsValidRoles(currentRoles, params.Roles) {
|
|
|
|
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
|
|
|
}
|
2021-10-13 16:41:41 +00:00
|
|
|
refreshToken, _, _ := utils.CreateAuthToken(user, enum.RefreshToken, roles)
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2021-10-13 16:41:41 +00:00
|
|
|
accessToken, expiresAt, _ := utils.CreateAuthToken(user, enum.AccessToken, roles)
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
session.SetToken(user.ID, accessToken, refreshToken)
|
2021-12-23 05:01:52 +00:00
|
|
|
utils.CreateSession(user.ID, gc)
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2021-07-28 10:13:08 +00:00
|
|
|
res = &model.AuthResponse{
|
2021-12-22 05:21:12 +00:00
|
|
|
Message: `Logged in successfully`,
|
|
|
|
AccessToken: &accessToken,
|
|
|
|
ExpiresAt: &expiresAt,
|
|
|
|
User: utils.GetResUser(user),
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
utils.SetCookie(gc, accessToken)
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|