From bde22b9ce9e768ff29a4c3383dca086412df341d Mon Sep 17 00:00:00 2001 From: sbouaram Date: Tue, 18 Jul 2023 12:38:07 +0200 Subject: [PATCH] fixing some issues --- config.go | 22 ++++++++-------------- go.mod | 5 ++--- go.sum | 4 +--- handlers/accounts.go | 12 +++++------- storage/postgresql.go | 32 +++++++++++++------------------- storage/postgresql_test.go | 2 +- 6 files changed, 30 insertions(+), 47 deletions(-) mode change 100755 => 100644 go.sum diff --git a/config.go b/config.go index 83a099e..e33b255 100755 --- a/config.go +++ b/config.go @@ -12,21 +12,15 @@ func ReadConfig() (*viper.Viper, error) { "dev_env": false, "storage": map[string]any{ "db": map[string]any{ - "type": "mongodb", - "mongodb": map[string]any{ - "host": "localhost", - "port": "27017", - "db_name": "coopgo_platform", - "collections": map[string]any{ - "users": "users", - }, - }, + "type": "psql", "psql": map[string]any{ - "host": "localhost", - "port": "5432", - "dbname": "coopgo_platform", - "sslmode": "disable", - "schema": "mobilityaccounts", + "user": "postgres", + "password": "postgres", + "host": "localhost", + "port": "5432", + "dbname": "coopgo_platform", + "sslmode": "disable", + "schema": "mobilityaccounts", "tables": map[string]any{ "accounts": "accounts", "accounts_auth_local": "accounts_auth_local", diff --git a/go.mod b/go.mod index 6f936e8..ba00ab1 100755 --- a/go.mod +++ b/go.mod @@ -7,12 +7,12 @@ require ( github.com/google/uuid v1.3.0 github.com/gorilla/csrf v1.7.1 github.com/gorilla/mux v1.8.0 - github.com/hashicorp/hcl/v2 v2.16.2 github.com/lib/pq v1.10.2 github.com/mitchellh/mapstructure v1.5.0 github.com/ory/fosite v0.42.2 github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 github.com/spf13/viper v1.15.0 + github.com/stretchr/testify v1.8.2 go.etcd.io/etcd/client/v3 v3.5.6 go.mongodb.org/mongo-driver v1.11.4 golang.org/x/crypto v0.6.0 @@ -39,6 +39,7 @@ require ( github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/hashicorp/hcl/v2 v2.16.2 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/klauspost/compress v1.13.6 // indirect github.com/magiconair/properties v1.8.7 // indirect @@ -60,8 +61,6 @@ require ( github.com/spf13/cobra v1.0.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/objx v0.5.0 // indirect - github.com/stretchr/testify v1.8.2 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.1 // indirect diff --git a/go.sum b/go.sum old mode 100755 new mode 100644 index 1111776..28044b8 --- a/go.sum +++ b/go.sum @@ -806,7 +806,7 @@ github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= -github.com/oleiade/reflections v1.0.0/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60= +github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w= github.com/oleiade/reflections v1.0.1 h1:D1XO3LVEYroYskEsoSiGItp9RUxG6jWnCVvrqH0HHQM= github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -1013,7 +1013,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -1023,7 +1022,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= diff --git a/handlers/accounts.go b/handlers/accounts.go index 436cc89..eef5d70 100755 --- a/handlers/accounts.go +++ b/handlers/accounts.go @@ -13,23 +13,26 @@ import ( ) func (h MobilityAccountsHandler) Login(username string, password string, namespace string) (*storage.Account, error) { - if password == "" { return nil, errors.New("empty password not allowed") } u := strings.ToLower(username) account, err := h.storage.DB.LocalAuthentication(namespace, &u, nil, nil) if err != nil { + fmt.Println(err) return nil, err } if err = bcrypt.CompareHashAndPassword([]byte(account.Authentication.Local.Password), []byte(password)); err != nil { + fmt.Println(err) return nil, err } + return account, nil } func (h MobilityAccountsHandler) Register(account storage.Account) (*storage.Account, error) { + if account.ID != "" { return nil, errors.New("id should be empty") } @@ -57,11 +60,8 @@ func (h MobilityAccountsHandler) Register(account storage.Account) (*storage.Acc account.Authentication.Local.Password = string(hashedPassword) } - _, err := h.storage.DB.LocalAuthentication(account.Namespace, account.Authentication.Local.Username, account.Authentication.Local.Email, account.Authentication.Local.PhoneNumber) + _, _ = h.storage.DB.LocalAuthentication(account.Namespace, account.Authentication.Local.Username, account.Authentication.Local.Email, account.Authentication.Local.PhoneNumber) - if err == nil { - return nil, errors.New("user already exists") - } } // Validate data schemas @@ -146,7 +146,6 @@ func (h MobilityAccountsHandler) UpdatePhoneNumber(accountid, phone_number strin if err == nil && account.ID != account2.ID { return errors.New("user with this phone number already exists") } - account.Authentication.Local.PhoneNumber = &phone_number account.Authentication.Local.PhoneNumberValidation.Validated = verified account.Authentication.Local.PhoneNumberValidation.ValidationCode = verification_code @@ -195,7 +194,6 @@ func (h MobilityAccountsHandler) ChangePassword(accountid string, newpassword st account.Authentication.Local.Password = string(hashedPassword) if err = h.storage.DB.UpdateAccount(*account); err != nil { - fmt.Println(err) return err } diff --git a/storage/postgresql.go b/storage/postgresql.go index 968be12..29c91e2 100644 --- a/storage/postgresql.go +++ b/storage/postgresql.go @@ -115,28 +115,23 @@ func (psql PostgresqlStorage) LocalAuthentication(namespace string, username *st var ( data, metadata, emailValidation, phoneValidation []byte ) - - requested_field := "" - requested_value := "" - if username != nil { - requested_field = "username" - requested_value = *username - } else if email != nil { - requested_field = "email" - requested_value = *email - } else if phone_number != nil { - requested_field = "phone_number" - requested_value = *phone_number - } else { - return nil, fmt.Errorf("localauthentication func error PSQL") - } - req := fmt.Sprintf(`SELECT id, namespace, data, metadata, username, password, email, email_validation, phone_number, phone_number_validation FROM %s INNER JOIN %s ON id = account_id - WHERE account_namespace = $1 AND %s = $2;`, psql.Tables["accounts"], psql.Tables["accounts_auth_local"], requested_field) + WHERE account_namespace = '%s'`, psql.Tables["accounts"], psql.Tables["accounts_auth_local"], namespace) + + if username != nil && *username != "" { + req += fmt.Sprintf(` AND username = '%s'`, *username) + } + if email != nil && *email != "" { + req += fmt.Sprintf(` AND email = '%s'`, *email) + } + if phone_number != nil && *phone_number != "" { + req += fmt.Sprintf(` AND phone_number = '%s'`, *phone_number) + } + req += ";" account.Authentication.Local = &LocalAuth{} - err := psql.DbConnection.QueryRow(req, namespace, requested_value).Scan( + err := psql.DbConnection.QueryRow(req).Scan( &account.ID, &account.Namespace, &data, &metadata, &account.Authentication.Local.Username, @@ -145,7 +140,6 @@ func (psql PostgresqlStorage) LocalAuthentication(namespace string, username *st &emailValidation, &account.Authentication.Local.PhoneNumber, &phoneValidation) - if err != nil { return nil, err } diff --git a/storage/postgresql_test.go b/storage/postgresql_test.go index 7bdaed8..5827ed6 100644 --- a/storage/postgresql_test.go +++ b/storage/postgresql_test.go @@ -19,7 +19,7 @@ func init() { cfg.SetDefault("storage.db.psql.port", "5432") cfg.SetDefault("storage.db.psql.user", "postgres") cfg.SetDefault("storage.db.psql.password", "postgres") - cfg.SetDefault("storage.db.psql.dbname", "mobilityaccounts_tests") + cfg.SetDefault("storage.db.psql.dbname", "coopgo_platform") cfg.SetDefault("storage.db.psql.sslmode", "disable") cfg.SetDefault("storage.db.psql.schema", "mobilityaccounts") cfg.SetDefault("storage.db.psql.tables.accounts", "accounts")