fix: unique constraint data
This commit is contained in:
parent
508c714932
commit
3ee79c3937
2
Makefile
2
Makefile
|
@ -6,4 +6,4 @@ cmd:
|
||||||
clean:
|
clean:
|
||||||
rm -rf build
|
rm -rf build
|
||||||
test:
|
test:
|
||||||
cd server && go clean --testcache && go test -v ./...
|
cd server && go clean --testcache && go test -v ./test
|
|
@ -36,16 +36,12 @@ func initArangodb() (arangoDriver.Database, error) {
|
||||||
|
|
||||||
if arangodb_exists {
|
if arangodb_exists {
|
||||||
log.Println(constants.DATABASE_NAME + " db exists already")
|
log.Println(constants.DATABASE_NAME + " db exists already")
|
||||||
|
|
||||||
arangodb, err = arangoClient.Database(nil, constants.DATABASE_NAME)
|
arangodb, err = arangoClient.Database(nil, constants.DATABASE_NAME)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
arangodb, err = arangoClient.CreateDatabase(nil, constants.DATABASE_NAME, nil)
|
arangodb, err = arangoClient.CreateDatabase(nil, constants.DATABASE_NAME, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,12 +34,6 @@ func initMongodb() (*mongo.Database, error) {
|
||||||
|
|
||||||
mongodb.CreateCollection(ctx, Collections.User, options.CreateCollection())
|
mongodb.CreateCollection(ctx, Collections.User, options.CreateCollection())
|
||||||
userCollection := mongodb.Collection(Collections.User, options.Collection())
|
userCollection := mongodb.Collection(Collections.User, options.Collection())
|
||||||
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
||||||
mongo.IndexModel{
|
|
||||||
Keys: bson.M{"id": 1},
|
|
||||||
Options: options.Index().SetUnique(true).SetSparse(true),
|
|
||||||
},
|
|
||||||
}, options.CreateIndexes())
|
|
||||||
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
||||||
mongo.IndexModel{
|
mongo.IndexModel{
|
||||||
Keys: bson.M{"email": 1},
|
Keys: bson.M{"email": 1},
|
||||||
|
@ -49,18 +43,14 @@ func initMongodb() (*mongo.Database, error) {
|
||||||
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
||||||
mongo.IndexModel{
|
mongo.IndexModel{
|
||||||
Keys: bson.M{"phone_number": 1},
|
Keys: bson.M{"phone_number": 1},
|
||||||
Options: options.Index().SetUnique(true).SetSparse(true),
|
Options: options.Index().SetUnique(true).SetSparse(true).SetPartialFilterExpression(map[string]interface{}{
|
||||||
|
"phone_number": map[string]string{"$type": "string"},
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
}, options.CreateIndexes())
|
}, options.CreateIndexes())
|
||||||
|
|
||||||
mongodb.CreateCollection(ctx, Collections.VerificationRequest, options.CreateCollection())
|
mongodb.CreateCollection(ctx, Collections.VerificationRequest, options.CreateCollection())
|
||||||
verificationRequestCollection := mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
verificationRequestCollection := mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
||||||
verificationRequestCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
||||||
mongo.IndexModel{
|
|
||||||
Keys: bson.M{"id": 1},
|
|
||||||
Options: options.Index().SetUnique(true).SetSparse(true),
|
|
||||||
},
|
|
||||||
}, options.CreateIndexes())
|
|
||||||
verificationRequestCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
verificationRequestCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
||||||
mongo.IndexModel{
|
mongo.IndexModel{
|
||||||
Keys: bson.M{"email": 1, "identifier": 1},
|
Keys: bson.M{"email": 1, "identifier": 1},
|
||||||
|
@ -75,13 +65,6 @@ func initMongodb() (*mongo.Database, error) {
|
||||||
}, options.CreateIndexes())
|
}, options.CreateIndexes())
|
||||||
|
|
||||||
mongodb.CreateCollection(ctx, Collections.Session, options.CreateCollection())
|
mongodb.CreateCollection(ctx, Collections.Session, options.CreateCollection())
|
||||||
sessionCollection := mongodb.Collection(Collections.Session, options.Collection())
|
|
||||||
sessionCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
||||||
mongo.IndexModel{
|
|
||||||
Keys: bson.M{"id": 1},
|
|
||||||
Options: options.Index().SetUnique(true).SetSparse(true),
|
|
||||||
},
|
|
||||||
}, options.CreateIndexes())
|
|
||||||
|
|
||||||
return mongodb, nil
|
return mongodb, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Key string `json:"_key,omitempty" bson:"_key,omitempty"` // for arangodb
|
Key string `json:"_key,omitempty" bson:"_key,omitempty"` // for arangodb
|
||||||
// ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
|
|
||||||
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
||||||
UserID string `gorm:"type:char(36)" json:"user_id" bson:"user_id"`
|
UserID string `gorm:"type:char(36)" json:"user_id" bson:"user_id"`
|
||||||
User User `json:"-" bson:"-"`
|
User User `json:"-" bson:"-"`
|
||||||
|
@ -29,7 +28,6 @@ func (mgr *manager) AddSession(session Session) error {
|
||||||
|
|
||||||
if IsORMSupported {
|
if IsORMSupported {
|
||||||
session.Key = session.ID
|
session.Key = session.ID
|
||||||
// session.ObjectID = session.ID
|
|
||||||
res := mgr.sqlDB.Clauses(
|
res := mgr.sqlDB.Clauses(
|
||||||
clause.OnConflict{
|
clause.OnConflict{
|
||||||
DoNothing: true,
|
DoNothing: true,
|
||||||
|
@ -53,7 +51,6 @@ func (mgr *manager) AddSession(session Session) error {
|
||||||
|
|
||||||
if IsMongoDB {
|
if IsMongoDB {
|
||||||
session.Key = session.ID
|
session.Key = session.ID
|
||||||
// session.ObjectID = session.ID
|
|
||||||
session.CreatedAt = time.Now().Unix()
|
session.CreatedAt = time.Now().Unix()
|
||||||
session.UpdatedAt = time.Now().Unix()
|
session.UpdatedAt = time.Now().Unix()
|
||||||
sessionCollection := mgr.mongodb.Collection(Collections.Session, options.Collection())
|
sessionCollection := mgr.mongodb.Collection(Collections.Session, options.Collection())
|
||||||
|
|
|
@ -19,17 +19,17 @@ type User struct {
|
||||||
|
|
||||||
Email string `gorm:"unique" json:"email" bson:"email"`
|
Email string `gorm:"unique" json:"email" bson:"email"`
|
||||||
EmailVerifiedAt int64 `json:"email_verified_at" bson:"email_verified_at"`
|
EmailVerifiedAt int64 `json:"email_verified_at" bson:"email_verified_at"`
|
||||||
Password string `gorm:"type:text" json:"password" bson:"password"`
|
Password *string `gorm:"type:text" json:"password" bson:"password"`
|
||||||
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
|
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
|
||||||
GivenName string `json:"given_name" bson:"given_name"`
|
GivenName *string `json:"given_name" bson:"given_name"`
|
||||||
FamilyName string `json:"family_name" bson:"family_name"`
|
FamilyName *string `json:"family_name" bson:"family_name"`
|
||||||
MiddleName string `json:"middle_name" bson:"middle_name"`
|
MiddleName *string `json:"middle_name" bson:"middle_name"`
|
||||||
Nickname string `json:"nickname" bson:"nickname"`
|
Nickname *string `json:"nickname" bson:"nickname"`
|
||||||
Gender string `json:"gender" bson:"gender"`
|
Gender *string `json:"gender" bson:"gender"`
|
||||||
Birthdate string `json:"birthdate" bson:"birthdate"`
|
Birthdate *string `json:"birthdate" bson:"birthdate"`
|
||||||
PhoneNumber string `gorm:"unique" json:"phone_number" bson:"phone_number"`
|
PhoneNumber *string `gorm:"unique" json:"phone_number" bson:"phone_number"`
|
||||||
PhoneNumberVerifiedAt int64 `json:"phone_number_verified_at" bson:"phone_number_verified_at"`
|
PhoneNumberVerifiedAt *int64 `json:"phone_number_verified_at" bson:"phone_number_verified_at"`
|
||||||
Picture string `gorm:"type:text" json:"picture" bson:"picture"`
|
Picture *string `gorm:"type:text" json:"picture" bson:"picture"`
|
||||||
Roles string `json:"roles" bson:"roles"`
|
Roles string `json:"roles" bson:"roles"`
|
||||||
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
|
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
|
||||||
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
|
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
|
||||||
|
@ -44,7 +44,6 @@ func (mgr *manager) AddUser(user User) (User, error) {
|
||||||
if IsORMSupported {
|
if IsORMSupported {
|
||||||
// copy id as value for fields required for mongodb & arangodb
|
// copy id as value for fields required for mongodb & arangodb
|
||||||
user.Key = user.ID
|
user.Key = user.ID
|
||||||
// user.ObjectID = user.ID
|
|
||||||
result := mgr.sqlDB.Clauses(
|
result := mgr.sqlDB.Clauses(
|
||||||
clause.OnConflict{
|
clause.OnConflict{
|
||||||
UpdateAll: true,
|
UpdateAll: true,
|
||||||
|
@ -67,14 +66,13 @@ func (mgr *manager) AddUser(user User) (User, error) {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
user.Key = meta.Key
|
user.Key = meta.Key
|
||||||
// user.ObjectID = meta.ID.String()
|
user.ID = meta.ID.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsMongoDB {
|
if IsMongoDB {
|
||||||
user.CreatedAt = time.Now().Unix()
|
user.CreatedAt = time.Now().Unix()
|
||||||
user.UpdatedAt = time.Now().Unix()
|
user.UpdatedAt = time.Now().Unix()
|
||||||
user.Key = user.ID
|
user.Key = user.ID
|
||||||
// user.ObjectID = user.ID
|
|
||||||
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
userCollection := mgr.mongodb.Collection(Collections.User, options.Collection())
|
||||||
_, err := userCollection.InsertOne(nil, user)
|
_, err := userCollection.InsertOne(nil, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -108,7 +106,7 @@ func (mgr *manager) UpdateUser(user User) (User, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
user.Key = meta.Key
|
user.Key = meta.Key
|
||||||
// user.ObjectID = meta.ID.String()
|
user.ID = meta.ID.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsMongoDB {
|
if IsMongoDB {
|
||||||
|
|
|
@ -14,7 +14,6 @@ import (
|
||||||
|
|
||||||
type VerificationRequest struct {
|
type VerificationRequest struct {
|
||||||
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
|
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
|
||||||
// ObjectID string `json:"_id,omitempty" bson:"_id"` // for arangodb & mongodb
|
|
||||||
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
||||||
Token string `gorm:"type:text" json:"token" bson:"token"`
|
Token string `gorm:"type:text" json:"token" bson:"token"`
|
||||||
Identifier string `gorm:"uniqueIndex:idx_email_identifier" json:"identifier" bson:"identifier"`
|
Identifier string `gorm:"uniqueIndex:idx_email_identifier" json:"identifier" bson:"identifier"`
|
||||||
|
@ -32,7 +31,6 @@ func (mgr *manager) AddVerification(verification VerificationRequest) (Verificat
|
||||||
if IsORMSupported {
|
if IsORMSupported {
|
||||||
// copy id as value for fields required for mongodb & arangodb
|
// copy id as value for fields required for mongodb & arangodb
|
||||||
verification.Key = verification.ID
|
verification.Key = verification.ID
|
||||||
// verification.ObjectID = verification.ID
|
|
||||||
result := mgr.sqlDB.Clauses(clause.OnConflict{
|
result := mgr.sqlDB.Clauses(clause.OnConflict{
|
||||||
Columns: []clause.Column{{Name: "email"}, {Name: "identifier"}},
|
Columns: []clause.Column{{Name: "email"}, {Name: "identifier"}},
|
||||||
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at"}),
|
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at"}),
|
||||||
|
@ -54,14 +52,13 @@ func (mgr *manager) AddVerification(verification VerificationRequest) (Verificat
|
||||||
return verification, err
|
return verification, err
|
||||||
}
|
}
|
||||||
verification.Key = meta.Key
|
verification.Key = meta.Key
|
||||||
// verification.ObjectID = meta.ID.String()
|
verification.ID = meta.ID.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsMongoDB {
|
if IsMongoDB {
|
||||||
verification.CreatedAt = time.Now().Unix()
|
verification.CreatedAt = time.Now().Unix()
|
||||||
verification.UpdatedAt = time.Now().Unix()
|
verification.UpdatedAt = time.Now().Unix()
|
||||||
verification.Key = verification.ID
|
verification.Key = verification.ID
|
||||||
// verification.ObjectID = verification.ID
|
|
||||||
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
||||||
_, err := verificationRequestCollection.InsertOne(nil, verification)
|
_, err := verificationRequestCollection.InsertOne(nil, verification)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -89,10 +89,12 @@ func processGithubUserInfo(code string) (db.User, error) {
|
||||||
lastName = name[0]
|
lastName = name[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
picture := userRawData["avatar_url"]
|
||||||
|
|
||||||
user = db.User{
|
user = db.User{
|
||||||
GivenName: firstName,
|
GivenName: &firstName,
|
||||||
FamilyName: lastName,
|
FamilyName: &lastName,
|
||||||
Picture: userRawData["avatar_url"],
|
Picture: &picture,
|
||||||
Email: userRawData["email"],
|
Email: userRawData["email"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,10 +132,14 @@ func processFacebookUserInfo(code string) (db.User, error) {
|
||||||
|
|
||||||
picObject := userRawData["picture"].(map[string]interface{})["data"]
|
picObject := userRawData["picture"].(map[string]interface{})["data"]
|
||||||
picDataObject := picObject.(map[string]interface{})
|
picDataObject := picObject.(map[string]interface{})
|
||||||
|
firstName := fmt.Sprintf("%v", userRawData["first_name"])
|
||||||
|
lastName := fmt.Sprintf("%v", userRawData["last_name"])
|
||||||
|
picture := fmt.Sprintf("%v", picDataObject["url"])
|
||||||
|
|
||||||
user = db.User{
|
user = db.User{
|
||||||
GivenName: fmt.Sprintf("%v", userRawData["first_name"]),
|
GivenName: &firstName,
|
||||||
FamilyName: fmt.Sprintf("%v", userRawData["last_name"]),
|
FamilyName: &lastName,
|
||||||
Picture: fmt.Sprintf("%v", picDataObject["url"]),
|
Picture: &picture,
|
||||||
Email: email,
|
Email: email,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +252,6 @@ func OAuthCallbackHandler() gin.HandlerFunc {
|
||||||
user.Roles = existingUser.Roles
|
user.Roles = existingUser.Roles
|
||||||
}
|
}
|
||||||
user.Key = existingUser.Key
|
user.Key = existingUser.Key
|
||||||
// user.ObjectID = existingUser.ObjectID
|
|
||||||
user.ID = existingUser.ID
|
user.ID = existingUser.ID
|
||||||
user, err = db.Mgr.UpdateUser(user)
|
user, err = db.Mgr.UpdateUser(user)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,10 @@ import (
|
||||||
"github.com/authorizerdev/authorizer/server/db"
|
"github.com/authorizerdev/authorizer/server/db"
|
||||||
"github.com/authorizerdev/authorizer/server/env"
|
"github.com/authorizerdev/authorizer/server/env"
|
||||||
"github.com/authorizerdev/authorizer/server/handlers"
|
"github.com/authorizerdev/authorizer/server/handlers"
|
||||||
"github.com/authorizerdev/authorizer/server/middlewares"
|
|
||||||
"github.com/authorizerdev/authorizer/server/oauth"
|
"github.com/authorizerdev/authorizer/server/oauth"
|
||||||
|
"github.com/authorizerdev/authorizer/server/router"
|
||||||
"github.com/authorizerdev/authorizer/server/session"
|
"github.com/authorizerdev/authorizer/server/session"
|
||||||
"github.com/authorizerdev/authorizer/server/utils"
|
"github.com/authorizerdev/authorizer/server/utils"
|
||||||
"github.com/gin-contrib/location"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -20,26 +18,15 @@ func main() {
|
||||||
oauth.InitOAuth()
|
oauth.InitOAuth()
|
||||||
utils.InitServer()
|
utils.InitServer()
|
||||||
|
|
||||||
r := gin.Default()
|
router := router.InitRouter()
|
||||||
r.Use(location.Default())
|
|
||||||
r.Use(middlewares.GinContextToContextMiddleware())
|
|
||||||
r.Use(middlewares.CORSMiddleware())
|
|
||||||
|
|
||||||
r.GET("/", handlers.PlaygroundHandler())
|
|
||||||
r.POST("/graphql", handlers.GraphqlHandler())
|
|
||||||
r.GET("/verify_email", handlers.VerifyEmailHandler())
|
|
||||||
r.GET("/oauth_login/:oauth_provider", handlers.OAuthLoginHandler())
|
|
||||||
r.GET("/oauth_callback/:oauth_provider", handlers.OAuthCallbackHandler())
|
|
||||||
|
|
||||||
// login wall app related routes
|
// login wall app related routes
|
||||||
|
router.LoadHTMLGlob("templates/*")
|
||||||
r.LoadHTMLGlob("templates/*")
|
app := router.Group("/app")
|
||||||
app := r.Group("/app")
|
|
||||||
{
|
{
|
||||||
app.Static("/build", "app/build")
|
app.Static("/build", "app/build")
|
||||||
app.GET("/", handlers.AppHandler())
|
app.GET("/", handlers.AppHandler())
|
||||||
app.GET("/reset-password", handlers.AppHandler())
|
app.GET("/reset-password", handlers.AppHandler())
|
||||||
}
|
}
|
||||||
|
router.Run(":" + constants.PORT)
|
||||||
r.Run(":" + constants.PORT)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ func Login(ctx context.Context, params model.LoginInput) (*model.AuthResponse, e
|
||||||
return res, fmt.Errorf(`email not verified`)
|
return res, fmt.Errorf(`email not verified`)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(params.Password))
|
err = bcrypt.CompareHashAndPassword([]byte(*user.Password), []byte(params.Password))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("compare password error:", err)
|
log.Println("compare password error:", err)
|
||||||
|
|
|
@ -39,7 +39,7 @@ func ResetPassword(ctx context.Context, params model.ResetPasswordInput) (*model
|
||||||
}
|
}
|
||||||
|
|
||||||
password, _ := utils.HashPassword(params.Password)
|
password, _ := utils.HashPassword(params.Password)
|
||||||
user.Password = password
|
user.Password = &password
|
||||||
|
|
||||||
signupMethod := user.SignupMethods
|
signupMethod := user.SignupMethods
|
||||||
if !strings.Contains(signupMethod, enum.BasicAuth.String()) {
|
if !strings.Contains(signupMethod, enum.BasicAuth.String()) {
|
||||||
|
|
|
@ -68,38 +68,38 @@ func Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse,
|
||||||
user.Roles = strings.Join(inputRoles, ",")
|
user.Roles = strings.Join(inputRoles, ",")
|
||||||
|
|
||||||
password, _ := utils.HashPassword(params.Password)
|
password, _ := utils.HashPassword(params.Password)
|
||||||
user.Password = password
|
user.Password = &password
|
||||||
|
|
||||||
if params.GivenName != nil {
|
if params.GivenName != nil {
|
||||||
user.GivenName = *params.GivenName
|
user.GivenName = params.GivenName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.FamilyName != nil {
|
if params.FamilyName != nil {
|
||||||
user.FamilyName = *params.FamilyName
|
user.FamilyName = params.FamilyName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.MiddleName != nil {
|
if params.MiddleName != nil {
|
||||||
user.MiddleName = *params.MiddleName
|
user.MiddleName = params.MiddleName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Nickname != nil {
|
if params.Nickname != nil {
|
||||||
user.Nickname = *params.Nickname
|
user.Nickname = params.Nickname
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Gender != nil {
|
if params.Gender != nil {
|
||||||
user.Gender = *params.Gender
|
user.Gender = params.Gender
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Birthdate != nil {
|
if params.Birthdate != nil {
|
||||||
user.Birthdate = *params.Birthdate
|
user.Birthdate = params.Birthdate
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.PhoneNumber != nil {
|
if params.PhoneNumber != nil {
|
||||||
user.PhoneNumber = *params.PhoneNumber
|
user.PhoneNumber = params.PhoneNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Picture != nil {
|
if params.Picture != nil {
|
||||||
user.Picture = *params.Picture
|
user.Picture = params.Picture
|
||||||
}
|
}
|
||||||
|
|
||||||
user.SignupMethods = enum.BasicAuth.String()
|
user.SignupMethods = enum.BasicAuth.String()
|
||||||
|
|
|
@ -50,40 +50,40 @@ func UpdateProfile(ctx context.Context, params model.UpdateProfileInput) (*model
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.GivenName != nil && user.GivenName != *params.GivenName {
|
if params.GivenName != nil && user.GivenName != params.GivenName {
|
||||||
user.GivenName = *params.GivenName
|
user.GivenName = params.GivenName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.FamilyName != nil && user.FamilyName != *params.FamilyName {
|
if params.FamilyName != nil && user.FamilyName != params.FamilyName {
|
||||||
user.FamilyName = *params.FamilyName
|
user.FamilyName = params.FamilyName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.MiddleName != nil && user.MiddleName != *params.MiddleName {
|
if params.MiddleName != nil && user.MiddleName != params.MiddleName {
|
||||||
user.MiddleName = *params.MiddleName
|
user.MiddleName = params.MiddleName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Nickname != nil && user.Nickname != *params.Nickname {
|
if params.Nickname != nil && user.Nickname != params.Nickname {
|
||||||
user.Nickname = *params.Nickname
|
user.Nickname = params.Nickname
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Birthdate != nil && user.Birthdate != *params.Birthdate {
|
if params.Birthdate != nil && user.Birthdate != params.Birthdate {
|
||||||
user.Birthdate = *params.Birthdate
|
user.Birthdate = params.Birthdate
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Gender != nil && user.Gender != *params.Gender {
|
if params.Gender != nil && user.Gender != params.Gender {
|
||||||
user.Gender = *params.Gender
|
user.Gender = params.Gender
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.PhoneNumber != nil && user.PhoneNumber != *params.PhoneNumber {
|
if params.PhoneNumber != nil && user.PhoneNumber != params.PhoneNumber {
|
||||||
user.PhoneNumber = *params.PhoneNumber
|
user.PhoneNumber = params.PhoneNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Picture != nil && user.Picture != *params.Picture {
|
if params.Picture != nil && user.Picture != params.Picture {
|
||||||
user.Picture = *params.Picture
|
user.Picture = params.Picture
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.OldPassword != nil {
|
if params.OldPassword != nil {
|
||||||
if err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(*params.OldPassword)); err != nil {
|
if err = bcrypt.CompareHashAndPassword([]byte(*user.Password), []byte(*params.OldPassword)); err != nil {
|
||||||
return res, fmt.Errorf("incorrect old password")
|
return res, fmt.Errorf("incorrect old password")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ func UpdateProfile(ctx context.Context, params model.UpdateProfileInput) (*model
|
||||||
|
|
||||||
password, _ := utils.HashPassword(*params.NewPassword)
|
password, _ := utils.HashPassword(*params.NewPassword)
|
||||||
|
|
||||||
user.Password = password
|
user.Password = &password
|
||||||
}
|
}
|
||||||
|
|
||||||
hasEmailChanged := false
|
hasEmailChanged := false
|
||||||
|
|
|
@ -35,36 +35,36 @@ func UpdateUser(ctx context.Context, params model.UpdateUserInput) (*model.User,
|
||||||
return res, fmt.Errorf(`User not found`)
|
return res, fmt.Errorf(`User not found`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.GivenName != nil && user.GivenName != *params.GivenName {
|
if params.GivenName != nil && user.GivenName != params.GivenName {
|
||||||
user.GivenName = *params.GivenName
|
user.GivenName = params.GivenName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.FamilyName != nil && user.FamilyName != *params.FamilyName {
|
if params.FamilyName != nil && user.FamilyName != params.FamilyName {
|
||||||
user.FamilyName = *params.FamilyName
|
user.FamilyName = params.FamilyName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.MiddleName != nil && user.MiddleName != *params.MiddleName {
|
if params.MiddleName != nil && user.MiddleName != params.MiddleName {
|
||||||
user.MiddleName = *params.MiddleName
|
user.MiddleName = params.MiddleName
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Nickname != nil && user.Nickname != *params.Nickname {
|
if params.Nickname != nil && user.Nickname != params.Nickname {
|
||||||
user.Nickname = *params.Nickname
|
user.Nickname = params.Nickname
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Birthdate != nil && user.Birthdate != *params.Birthdate {
|
if params.Birthdate != nil && user.Birthdate != params.Birthdate {
|
||||||
user.Birthdate = *params.Birthdate
|
user.Birthdate = params.Birthdate
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Gender != nil && user.Gender != *params.Gender {
|
if params.Gender != nil && user.Gender != params.Gender {
|
||||||
user.Gender = *params.Gender
|
user.Gender = params.Gender
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.PhoneNumber != nil && user.PhoneNumber != *params.PhoneNumber {
|
if params.PhoneNumber != nil && user.PhoneNumber != params.PhoneNumber {
|
||||||
user.PhoneNumber = *params.PhoneNumber
|
user.PhoneNumber = params.PhoneNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Picture != nil && user.Picture != *params.Picture {
|
if params.Picture != nil && user.Picture != params.Picture {
|
||||||
user.Picture = *params.Picture
|
user.Picture = params.Picture
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Email != nil && user.Email != *params.Email {
|
if params.Email != nil && user.Email != *params.Email {
|
||||||
|
@ -137,9 +137,9 @@ func UpdateUser(ctx context.Context, params model.UpdateUserInput) (*model.User,
|
||||||
res = &model.User{
|
res = &model.User{
|
||||||
ID: params.ID,
|
ID: params.ID,
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
Picture: &user.Picture,
|
Picture: user.Picture,
|
||||||
GivenName: &user.GivenName,
|
GivenName: user.GivenName,
|
||||||
FamilyName: &user.FamilyName,
|
FamilyName: user.FamilyName,
|
||||||
Roles: strings.Split(user.Roles, ","),
|
Roles: strings.Split(user.Roles, ","),
|
||||||
CreatedAt: &user.CreatedAt,
|
CreatedAt: &user.CreatedAt,
|
||||||
UpdatedAt: &user.UpdatedAt,
|
UpdatedAt: &user.UpdatedAt,
|
||||||
|
|
23
server/router/router.go
Normal file
23
server/router/router.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/authorizerdev/authorizer/server/handlers"
|
||||||
|
"github.com/authorizerdev/authorizer/server/middlewares"
|
||||||
|
"github.com/gin-contrib/location"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitRouter() *gin.Engine {
|
||||||
|
router := gin.Default()
|
||||||
|
router.Use(location.Default())
|
||||||
|
router.Use(middlewares.GinContextToContextMiddleware())
|
||||||
|
router.Use(middlewares.CORSMiddleware())
|
||||||
|
|
||||||
|
router.GET("/", handlers.PlaygroundHandler())
|
||||||
|
router.POST("/graphql", handlers.GraphqlHandler())
|
||||||
|
router.GET("/verify_email", handlers.VerifyEmailHandler())
|
||||||
|
router.GET("/oauth_login/:oauth_provider", handlers.OAuthLoginHandler())
|
||||||
|
router.GET("/oauth_callback/:oauth_provider", handlers.OAuthCallbackHandler())
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package integration_test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -6,32 +6,31 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/authorizerdev/authorizer/server/constants"
|
"github.com/authorizerdev/authorizer/server/constants"
|
||||||
|
"github.com/authorizerdev/authorizer/server/db"
|
||||||
"github.com/authorizerdev/authorizer/server/env"
|
"github.com/authorizerdev/authorizer/server/env"
|
||||||
"github.com/authorizerdev/authorizer/server/middlewares"
|
"github.com/authorizerdev/authorizer/server/router"
|
||||||
"github.com/gin-contrib/location"
|
"github.com/authorizerdev/authorizer/server/session"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCors(t *testing.T) {
|
func TestCors(t *testing.T) {
|
||||||
constants.DATABASE_TYPE = "sqlite"
|
constants.ENV_PATH = "../../.env.sample"
|
||||||
constants.DATABASE_URL = "data.db"
|
constants.DATABASE_URL = "../../data.db"
|
||||||
constants.ENV_PATH = "../../.env.local"
|
|
||||||
env.InitEnv()
|
env.InitEnv()
|
||||||
r := gin.Default()
|
db.InitDB()
|
||||||
r.Use(location.Default())
|
session.InitSession()
|
||||||
r.Use(middlewares.GinContextToContextMiddleware())
|
router := router.InitRouter()
|
||||||
r.Use(middlewares.CORSMiddleware())
|
|
||||||
allowedOrigin := "http://localhost:8080" // The allowed origin that you want to check
|
allowedOrigin := "http://localhost:8080" // The allowed origin that you want to check
|
||||||
notAllowedOrigin := "http://myapp.com"
|
notAllowedOrigin := "http://myapp.com"
|
||||||
|
|
||||||
server := httptest.NewServer(r)
|
server := httptest.NewServer(router)
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
req, _ := http.NewRequest(
|
req, _ := http.NewRequest(
|
||||||
"GET",
|
"GET",
|
||||||
"http://"+server.Listener.Addr().String()+"/api",
|
"http://"+server.Listener.Addr().String()+"/graphql",
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
req.Header.Add("Origin", allowedOrigin)
|
req.Header.Add("Origin", allowedOrigin)
|
|
@ -1,4 +1,4 @@
|
||||||
package env
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -10,13 +10,11 @@ import (
|
||||||
|
|
||||||
func TestEnvs(t *testing.T) {
|
func TestEnvs(t *testing.T) {
|
||||||
constants.ENV_PATH = "../../.env.sample"
|
constants.ENV_PATH = "../../.env.sample"
|
||||||
constants.DATABASE_TYPE = "sqlite"
|
// env.InitEnv()
|
||||||
constants.DATABASE_URL = "data.db"
|
|
||||||
InitEnv()
|
|
||||||
|
|
||||||
assert.Equal(t, constants.ADMIN_SECRET, "admin")
|
assert.Equal(t, constants.ADMIN_SECRET, "admin")
|
||||||
assert.Equal(t, constants.ENV, "production")
|
assert.Equal(t, constants.ENV, "production")
|
||||||
assert.Equal(t, constants.DATABASE_URL, "data.db")
|
assert.Equal(t, constants.DATABASE_URL, "../../data.db")
|
||||||
assert.Equal(t, constants.DATABASE_TYPE, enum.Sqlite.String())
|
assert.Equal(t, constants.DATABASE_TYPE, enum.Sqlite.String())
|
||||||
assert.True(t, constants.DISABLE_EMAIL_VERIFICATION)
|
assert.True(t, constants.DISABLE_EMAIL_VERIFICATION)
|
||||||
assert.True(t, constants.DISABLE_MAGIC_LINK_LOGIN)
|
assert.True(t, constants.DISABLE_MAGIC_LINK_LOGIN)
|
29
server/test/signup_test.go
Normal file
29
server/test/signup_test.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
||||||
|
"github.com/authorizerdev/authorizer/server/resolvers"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSQLSignUp(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest("POST", "/graphql", nil)
|
||||||
|
c, _ := gin.CreateTestContext(w)
|
||||||
|
ctx := context.WithValue(req.Context(), "GinContextKey", c)
|
||||||
|
|
||||||
|
res, err := resolvers.Signup(ctx, model.SignUpInput{
|
||||||
|
Email: "test@yopmail.com",
|
||||||
|
Password: "test",
|
||||||
|
ConfirmPassword: "test",
|
||||||
|
})
|
||||||
|
log.Println("=> signup err:", err)
|
||||||
|
log.Println("=> singup res:", res)
|
||||||
|
assert.Equal(t, "success", "success")
|
||||||
|
}
|
|
@ -1,15 +1,16 @@
|
||||||
package utils
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/utils"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetHostName(t *testing.T) {
|
func TestGetHostName(t *testing.T) {
|
||||||
authorizer_url := "http://test.herokuapp.com:80"
|
authorizer_url := "http://test.herokuapp.com:80"
|
||||||
|
|
||||||
host, port := GetHostParts(authorizer_url)
|
host, port := utils.GetHostParts(authorizer_url)
|
||||||
expectedHost := "test.herokuapp.com"
|
expectedHost := "test.herokuapp.com"
|
||||||
|
|
||||||
assert.Equal(t, host, expectedHost, "hostname should be equal")
|
assert.Equal(t, host, expectedHost, "hostname should be equal")
|
||||||
|
@ -19,7 +20,7 @@ func TestGetHostName(t *testing.T) {
|
||||||
func TestGetDomainName(t *testing.T) {
|
func TestGetDomainName(t *testing.T) {
|
||||||
authorizer_url := "http://test.herokuapp.com"
|
authorizer_url := "http://test.herokuapp.com"
|
||||||
|
|
||||||
got := GetDomainName(authorizer_url)
|
got := utils.GetDomainName(authorizer_url)
|
||||||
want := "herokuapp.com"
|
want := "herokuapp.com"
|
||||||
|
|
||||||
assert.Equal(t, got, want, "domain name should be equal")
|
assert.Equal(t, got, want, "domain name should be equal")
|
35
server/test/validator_test.go
Normal file
35
server/test/validator_test.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/constants"
|
||||||
|
"github.com/authorizerdev/authorizer/server/utils"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsValidEmail(t *testing.T) {
|
||||||
|
validEmail := "lakhan@gmail.com"
|
||||||
|
invalidEmail1 := "lakhan"
|
||||||
|
invalidEmail2 := "lakhan.me"
|
||||||
|
|
||||||
|
assert.True(t, utils.IsValidEmail(validEmail), "it should be valid email")
|
||||||
|
assert.False(t, utils.IsValidEmail(invalidEmail1), "it should be invalid email")
|
||||||
|
assert.False(t, utils.IsValidEmail(invalidEmail2), "it should be invalid email")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsValidOrigin(t *testing.T) {
|
||||||
|
// don't use portocal(http/https) for ALLOWED_ORIGINS while testing,
|
||||||
|
// as we trim them off while running the main function
|
||||||
|
constants.ALLOWED_ORIGINS = []string{"localhost:8080", "*.google.com", "*.google.in", "*abc.*"}
|
||||||
|
|
||||||
|
assert.False(t, utils.IsValidOrigin("http://myapp.com"), "it should be invalid origin")
|
||||||
|
assert.False(t, utils.IsValidOrigin("http://appgoogle.com"), "it should be invalid origin")
|
||||||
|
assert.True(t, utils.IsValidOrigin("http://app.google.com"), "it should be valid origin")
|
||||||
|
assert.False(t, utils.IsValidOrigin("http://app.google.ind"), "it should be invalid origin")
|
||||||
|
assert.True(t, utils.IsValidOrigin("http://app.google.in"), "it should be valid origin")
|
||||||
|
assert.True(t, utils.IsValidOrigin("http://xyx.abc.com"), "it should be valid origin")
|
||||||
|
assert.True(t, utils.IsValidOrigin("http://xyx.abc.in"), "it should be valid origin")
|
||||||
|
assert.True(t, utils.IsValidOrigin("http://xyxabc.in"), "it should be valid origin")
|
||||||
|
assert.True(t, utils.IsValidOrigin("http://localhost:8080"), "it should be valid origin")
|
||||||
|
}
|
|
@ -9,22 +9,22 @@ import (
|
||||||
|
|
||||||
func GetResUser(user db.User) *model.User {
|
func GetResUser(user db.User) *model.User {
|
||||||
isEmailVerified := user.EmailVerifiedAt > 0
|
isEmailVerified := user.EmailVerifiedAt > 0
|
||||||
isPhoneVerified := user.PhoneNumberVerifiedAt > 0
|
isPhoneVerified := user.PhoneNumberVerifiedAt != nil
|
||||||
return &model.User{
|
return &model.User{
|
||||||
ID: user.ID,
|
ID: user.ID,
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
EmailVerified: isEmailVerified,
|
EmailVerified: isEmailVerified,
|
||||||
SignupMethods: user.SignupMethods,
|
SignupMethods: user.SignupMethods,
|
||||||
GivenName: &user.GivenName,
|
GivenName: user.GivenName,
|
||||||
FamilyName: &user.FamilyName,
|
FamilyName: user.FamilyName,
|
||||||
MiddleName: &user.MiddleName,
|
MiddleName: user.MiddleName,
|
||||||
Nickname: &user.Nickname,
|
Nickname: user.Nickname,
|
||||||
PreferredUsername: &user.Email,
|
PreferredUsername: &user.Email,
|
||||||
Gender: &user.Gender,
|
Gender: user.Gender,
|
||||||
Birthdate: &user.Birthdate,
|
Birthdate: user.Birthdate,
|
||||||
PhoneNumber: &user.PhoneNumber,
|
PhoneNumber: user.PhoneNumber,
|
||||||
PhoneNumberVerified: &isPhoneVerified,
|
PhoneNumberVerified: &isPhoneVerified,
|
||||||
Picture: &user.Picture,
|
Picture: user.Picture,
|
||||||
Roles: strings.Split(user.Roles, ","),
|
Roles: strings.Split(user.Roles, ","),
|
||||||
CreatedAt: &user.CreatedAt,
|
CreatedAt: &user.CreatedAt,
|
||||||
UpdatedAt: &user.UpdatedAt,
|
UpdatedAt: &user.UpdatedAt,
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/authorizerdev/authorizer/server/constants"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIsValidEmail(t *testing.T) {
|
|
||||||
validEmail := "lakhan@gmail.com"
|
|
||||||
invalidEmail1 := "lakhan"
|
|
||||||
invalidEmail2 := "lakhan.me"
|
|
||||||
|
|
||||||
assert.True(t, IsValidEmail(validEmail), "it should be valid email")
|
|
||||||
assert.False(t, IsValidEmail(invalidEmail1), "it should be invalid email")
|
|
||||||
assert.False(t, IsValidEmail(invalidEmail2), "it should be invalid email")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsValidOrigin(t *testing.T) {
|
|
||||||
// don't use portocal(http/https) for ALLOWED_ORIGINS while testing,
|
|
||||||
// as we trim them off while running the main function
|
|
||||||
constants.ALLOWED_ORIGINS = []string{"localhost:8080", "*.google.com", "*.google.in", "*abc.*"}
|
|
||||||
|
|
||||||
assert.False(t, IsValidOrigin("http://myapp.com"), "it should be invalid origin")
|
|
||||||
assert.False(t, IsValidOrigin("http://appgoogle.com"), "it should be invalid origin")
|
|
||||||
assert.True(t, IsValidOrigin("http://app.google.com"), "it should be valid origin")
|
|
||||||
assert.False(t, IsValidOrigin("http://app.google.ind"), "it should be invalid origin")
|
|
||||||
assert.True(t, IsValidOrigin("http://app.google.in"), "it should be valid origin")
|
|
||||||
assert.True(t, IsValidOrigin("http://xyx.abc.com"), "it should be valid origin")
|
|
||||||
assert.True(t, IsValidOrigin("http://xyx.abc.in"), "it should be valid origin")
|
|
||||||
assert.True(t, IsValidOrigin("http://xyxabc.in"), "it should be valid origin")
|
|
||||||
assert.True(t, IsValidOrigin("http://localhost:8080"), "it should be valid origin")
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user