2021-07-17 16:29:50 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-18 03:55:20 +00:00
|
|
|
"fmt"
|
2021-07-17 16:29:50 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2021-07-28 10:13:08 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/session"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-17 16:29:50 +00:00
|
|
|
)
|
|
|
|
|
2021-07-28 10:13:08 +00:00
|
|
|
func Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse, error) {
|
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
|
|
var res *model.AuthResponse
|
|
|
|
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-20 22:04:03 +00:00
|
|
|
if params.ConfirmPassword != params.Password {
|
2021-12-20 17:51:27 +00:00
|
|
|
return res, fmt.Errorf(`password and confirm password does not match`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`invalid email address`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 05:06:26 +00:00
|
|
|
inputRoles := []string{}
|
|
|
|
|
2021-10-13 16:41:41 +00:00
|
|
|
if len(params.Roles) > 0 {
|
2021-09-20 05:06:26 +00:00
|
|
|
// check if roles exists
|
2021-10-13 16:41:41 +00:00
|
|
|
if !utils.IsValidRoles(constants.ROLES, params.Roles) {
|
2021-09-20 05:06:26 +00:00
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
2021-10-13 16:41:41 +00:00
|
|
|
} else {
|
|
|
|
inputRoles = params.Roles
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-13 16:41:41 +00:00
|
|
|
inputRoles = constants.DEFAULT_ROLES
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 16:29:50 +00:00
|
|
|
// find user with email
|
|
|
|
existingUser, err := db.Mgr.GetUserByEmail(params.Email)
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println("user with email " + params.Email + " not found")
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 10:08:51 +00:00
|
|
|
if existingUser.EmailVerifiedAt != nil {
|
2021-07-17 16:29:50 +00:00
|
|
|
// email is verified
|
2021-12-20 17:51:27 +00:00
|
|
|
return res, fmt.Errorf(`%s has already signed up`, params.Email)
|
2021-12-22 10:08:51 +00:00
|
|
|
} else if existingUser.ID != "" && existingUser.EmailVerifiedAt == nil {
|
2021-12-20 17:51:27 +00:00
|
|
|
return res, fmt.Errorf("%s has already signed up. please complete the email verification process or reset the password", params.Email)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
|
2021-07-17 16:29:50 +00:00
|
|
|
user := db.User{
|
2021-07-17 17:09:50 +00:00
|
|
|
Email: params.Email,
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 05:06:26 +00:00
|
|
|
user.Roles = strings.Join(inputRoles, ",")
|
|
|
|
|
2021-07-17 17:09:50 +00:00
|
|
|
password, _ := utils.HashPassword(params.Password)
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Password = &password
|
2021-07-17 17:09:50 +00:00
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
if params.GivenName != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.GivenName = params.GivenName
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
if params.FamilyName != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.FamilyName = params.FamilyName
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
if params.MiddleName != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.MiddleName = params.MiddleName
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if params.Nickname != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Nickname = params.Nickname
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if params.Gender != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Gender = params.Gender
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if params.Birthdate != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Birthdate = params.Birthdate
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if params.PhoneNumber != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.PhoneNumber = params.PhoneNumber
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if params.Picture != nil {
|
2021-12-22 10:01:45 +00:00
|
|
|
user.Picture = params.Picture
|
2021-12-22 05:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user.SignupMethods = enum.BasicAuth.String()
|
2021-12-20 12:03:11 +00:00
|
|
|
if constants.DISABLE_EMAIL_VERIFICATION {
|
2021-12-22 10:08:51 +00:00
|
|
|
now := time.Now().Unix()
|
|
|
|
user.EmailVerifiedAt = &now
|
2021-07-28 10:13:08 +00:00
|
|
|
}
|
2021-12-17 15:55:07 +00:00
|
|
|
user, err = db.Mgr.AddUser(user)
|
2021-07-17 16:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2021-08-07 08:41:26 +00:00
|
|
|
userIdStr := fmt.Sprintf("%v", user.ID)
|
2021-10-13 16:41:41 +00:00
|
|
|
roles := strings.Split(user.Roles, ",")
|
2021-12-24 01:05:02 +00:00
|
|
|
userToReturn := utils.GetResponseUserData(user)
|
2021-07-28 10:13:08 +00:00
|
|
|
|
2021-12-20 12:03:11 +00:00
|
|
|
if !constants.DISABLE_EMAIL_VERIFICATION {
|
2021-07-28 10:13:08 +00:00
|
|
|
// insert verification request
|
|
|
|
verificationType := enum.BasicAuthSignup.String()
|
2021-08-04 10:25:13 +00:00
|
|
|
token, err := utils.CreateVerificationToken(params.Email, verificationType)
|
2021-07-28 10:13:08 +00:00
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println(`error generating token`, err)
|
2021-07-28 10:13:08 +00:00
|
|
|
}
|
|
|
|
db.Mgr.AddVerification(db.VerificationRequest{
|
|
|
|
Token: token,
|
|
|
|
Identifier: verificationType,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
})
|
|
|
|
|
|
|
|
// exec it as go routin so that we can reduce the api latency
|
|
|
|
go func() {
|
|
|
|
utils.SendVerificationMail(params.Email, token)
|
|
|
|
}()
|
|
|
|
|
|
|
|
res = &model.AuthResponse{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
|
|
|
User: userToReturn,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2021-10-13 16:41:41 +00:00
|
|
|
refreshToken, _, _ := utils.CreateAuthToken(user, enum.RefreshToken, roles)
|
2021-07-28 10:13:08 +00:00
|
|
|
|
2021-10-13 16:41:41 +00:00
|
|
|
accessToken, expiresAt, _ := utils.CreateAuthToken(user, enum.AccessToken, roles)
|
2021-07-28 10:13:08 +00:00
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
session.SetToken(userIdStr, accessToken, refreshToken)
|
2021-12-23 05:01:52 +00:00
|
|
|
utils.CreateSession(user.ID, gc)
|
2021-07-28 10:13:08 +00:00
|
|
|
res = &model.AuthResponse{
|
2021-12-22 05:21:12 +00:00
|
|
|
Message: `Signed up successfully.`,
|
|
|
|
AccessToken: &accessToken,
|
|
|
|
ExpiresAt: &expiresAt,
|
|
|
|
User: userToReturn,
|
2021-07-28 10:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
utils.SetCookie(gc, accessToken)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|