fixing some issues

This commit is contained in:
sbouaram 2023-07-18 12:38:07 +02:00
parent bac60f049f
commit bde22b9ce9
6 changed files with 30 additions and 47 deletions

View File

@ -12,21 +12,15 @@ func ReadConfig() (*viper.Viper, error) {
"dev_env": false, "dev_env": false,
"storage": map[string]any{ "storage": map[string]any{
"db": map[string]any{ "db": map[string]any{
"type": "mongodb", "type": "psql",
"mongodb": map[string]any{
"host": "localhost",
"port": "27017",
"db_name": "coopgo_platform",
"collections": map[string]any{
"users": "users",
},
},
"psql": map[string]any{ "psql": map[string]any{
"host": "localhost", "user": "postgres",
"port": "5432", "password": "postgres",
"dbname": "coopgo_platform", "host": "localhost",
"sslmode": "disable", "port": "5432",
"schema": "mobilityaccounts", "dbname": "coopgo_platform",
"sslmode": "disable",
"schema": "mobilityaccounts",
"tables": map[string]any{ "tables": map[string]any{
"accounts": "accounts", "accounts": "accounts",
"accounts_auth_local": "accounts_auth_local", "accounts_auth_local": "accounts_auth_local",

5
go.mod
View File

@ -7,12 +7,12 @@ require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/gorilla/csrf v1.7.1 github.com/gorilla/csrf v1.7.1
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/hashicorp/hcl/v2 v2.16.2
github.com/lib/pq v1.10.2 github.com/lib/pq v1.10.2
github.com/mitchellh/mapstructure v1.5.0 github.com/mitchellh/mapstructure v1.5.0
github.com/ory/fosite v0.42.2 github.com/ory/fosite v0.42.2
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
github.com/spf13/viper v1.15.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.etcd.io/etcd/client/v3 v3.5.6
go.mongodb.org/mongo-driver v1.11.4 go.mongodb.org/mongo-driver v1.11.4
golang.org/x/crypto v0.6.0 golang.org/x/crypto v0.6.0
@ -39,6 +39,7 @@ require (
github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // 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/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect github.com/klauspost/compress v1.13.6 // indirect
github.com/magiconair/properties v1.8.7 // 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/cobra v1.0.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // 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/subosito/gotenv v1.4.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect github.com/xdg-go/scram v1.1.1 // indirect

4
go.sum Executable file → Normal file
View File

@ -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 h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= 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/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 h1:D1XO3LVEYroYskEsoSiGItp9RUxG6jWnCVvrqH0HHQM=
github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60= 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= 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.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= 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.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/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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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.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.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.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.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=

View File

@ -13,23 +13,26 @@ import (
) )
func (h MobilityAccountsHandler) Login(username string, password string, namespace string) (*storage.Account, error) { func (h MobilityAccountsHandler) Login(username string, password string, namespace string) (*storage.Account, error) {
if password == "" { if password == "" {
return nil, errors.New("empty password not allowed") return nil, errors.New("empty password not allowed")
} }
u := strings.ToLower(username) u := strings.ToLower(username)
account, err := h.storage.DB.LocalAuthentication(namespace, &u, nil, nil) account, err := h.storage.DB.LocalAuthentication(namespace, &u, nil, nil)
if err != nil { if err != nil {
fmt.Println(err)
return nil, err return nil, err
} }
if err = bcrypt.CompareHashAndPassword([]byte(account.Authentication.Local.Password), []byte(password)); err != nil { if err = bcrypt.CompareHashAndPassword([]byte(account.Authentication.Local.Password), []byte(password)); err != nil {
fmt.Println(err)
return nil, err return nil, err
} }
return account, nil return account, nil
} }
func (h MobilityAccountsHandler) Register(account storage.Account) (*storage.Account, error) { func (h MobilityAccountsHandler) Register(account storage.Account) (*storage.Account, error) {
if account.ID != "" { if account.ID != "" {
return nil, errors.New("id should be empty") 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) 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 // Validate data schemas
@ -146,7 +146,6 @@ func (h MobilityAccountsHandler) UpdatePhoneNumber(accountid, phone_number strin
if err == nil && account.ID != account2.ID { if err == nil && account.ID != account2.ID {
return errors.New("user with this phone number already exists") return errors.New("user with this phone number already exists")
} }
account.Authentication.Local.PhoneNumber = &phone_number account.Authentication.Local.PhoneNumber = &phone_number
account.Authentication.Local.PhoneNumberValidation.Validated = verified account.Authentication.Local.PhoneNumberValidation.Validated = verified
account.Authentication.Local.PhoneNumberValidation.ValidationCode = verification_code 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) account.Authentication.Local.Password = string(hashedPassword)
if err = h.storage.DB.UpdateAccount(*account); err != nil { if err = h.storage.DB.UpdateAccount(*account); err != nil {
fmt.Println(err)
return err return err
} }

View File

@ -115,28 +115,23 @@ func (psql PostgresqlStorage) LocalAuthentication(namespace string, username *st
var ( var (
data, metadata, emailValidation, phoneValidation []byte 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 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 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{} account.Authentication.Local = &LocalAuth{}
err := psql.DbConnection.QueryRow(req, namespace, requested_value).Scan( err := psql.DbConnection.QueryRow(req).Scan(
&account.ID, &account.ID,
&account.Namespace, &data, &metadata, &account.Namespace, &data, &metadata,
&account.Authentication.Local.Username, &account.Authentication.Local.Username,
@ -145,7 +140,6 @@ func (psql PostgresqlStorage) LocalAuthentication(namespace string, username *st
&emailValidation, &emailValidation,
&account.Authentication.Local.PhoneNumber, &account.Authentication.Local.PhoneNumber,
&phoneValidation) &phoneValidation)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -19,7 +19,7 @@ func init() {
cfg.SetDefault("storage.db.psql.port", "5432") cfg.SetDefault("storage.db.psql.port", "5432")
cfg.SetDefault("storage.db.psql.user", "postgres") cfg.SetDefault("storage.db.psql.user", "postgres")
cfg.SetDefault("storage.db.psql.password", "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.sslmode", "disable")
cfg.SetDefault("storage.db.psql.schema", "mobilityaccounts") cfg.SetDefault("storage.db.psql.schema", "mobilityaccounts")
cfg.SetDefault("storage.db.psql.tables.accounts", "accounts") cfg.SetDefault("storage.db.psql.tables.accounts", "accounts")