Loading go-packages/meep-users/db.go +13 −13 Original line number Diff line number Diff line Loading @@ -22,6 +22,8 @@ import ( "strings" log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger" _ "github.com/lib/pq" ) // DB Config Loading Loading @@ -55,7 +57,6 @@ type User struct { // Connector - Implements a Postgis SQL DB connector type Connector struct { name string namespace string dbName string db *sql.DB connected bool Loading @@ -72,7 +73,6 @@ func NewConnector(name, user, pwd, host, port string) (pc *Connector, err error) // Create new connector pc = new(Connector) pc.name = name pc.namespace = "default" // Connect to Postgis DB for retry := 0; retry <= DbMaxRetryCount; retry++ { Loading Loading @@ -166,9 +166,9 @@ func (pc *Connector) CreateTables() (err error) { _, err = pc.db.Exec(`CREATE TABLE ` + UsersTable + ` ( id SERIAL PRIMARY KEY, username varchar(36) NOT NULL UNIQUE, password varchar(36) NOT NULL, role varchar(36) NOT NULL DEFAULT 'user' sboxname varchar(36) NOT NULL DEFAULT '' password varchar(100) NOT NULL, role varchar(36) NOT NULL DEFAULT 'user', sboxname varchar(11) NOT NULL DEFAULT '' )`) if err != nil { log.Error(err.Error()) Loading Loading @@ -236,7 +236,7 @@ func (pc *Connector) UpdateUser(username string, password string, role string, s if password != "" { query := `UPDATE ` + UsersTable + ` SET password = crypt('`+password+`', gen_salt('bf')) WHERE name = ($1)` WHERE username = ($1)` _, err = pc.db.Exec(query, username) if err != nil { log.Error(err.Error()) Loading @@ -251,7 +251,7 @@ func (pc *Connector) UpdateUser(username string, password string, role string, s } query := `UPDATE ` + UsersTable + ` SET role = $2 WHERE name = ($1)` WHERE username = ($1)` _, err = pc.db.Exec(query, username, role) if err != nil { log.Error(err.Error()) Loading @@ -262,7 +262,7 @@ func (pc *Connector) UpdateUser(username string, password string, role string, s if sboxname != "" { query := `UPDATE ` + UsersTable + ` SET sboxname = $2 WHERE name = ($1)` WHERE username = ($1)` _, err = pc.db.Exec(query, username, sboxname) if err != nil { log.Error(err.Error()) Loading @@ -286,7 +286,7 @@ func (pc *Connector) GetUser(username string) (user *User, err error) { rows, err = pc.db.Query(` SELECT id, username, password, role, sboxname FROM `+UsersTable+` WHERE name = ($1)`, username) WHERE username = ($1)`, username) if err != nil { log.Error(err.Error()) return nil, err Loading Loading @@ -359,7 +359,7 @@ func (pc *Connector) DeleteUser(username string) (err error) { return err } _, err = pc.db.Exec(`DELETE FROM `+UsersTable+` WHERE name = ($1)`, username) _, err = pc.db.Exec(`DELETE FROM `+UsersTable+` WHERE username = ($1)`, username) if err != nil { log.Error(err.Error()) return err Loading Loading @@ -389,7 +389,7 @@ func (pc *Connector) IsValidUser(username string) (valid bool, err error){ rows, err := pc.db.Query(` SELECT id FROM `+UsersTable+` WHERE name = ($1)`, username) WHERE username = ($1)`, username) if err != nil { log.Error(err.Error()) return false, err Loading Loading @@ -423,8 +423,8 @@ func (pc *Connector) AuthenticateUser(username string, password string) (authent rows, err := pc.db.Query(` SELECT id FROM `+UsersTable+` WHERE name = ($1) AND password = crypt('($2)', password)` , username, password) WHERE username = ($1) AND password = crypt('`+password+`', password)` , username) if err != nil { log.Error(err.Error()) return false, err Loading go-packages/meep-users/db_test.go +5 −6 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ import ( const ( pcName = "pc" pcNamespace = "postgis-ns" pcDBUser = "postgres" pcDBPwd = "pwd" pcDBHost = "localhost" Loading @@ -46,7 +45,7 @@ const ( role2 = "user" role3 = "super" sboxname0 = "" sboxname0 = "123456789012345" // more than 11 chars sboxname1 = "sbox-1" sboxname2 = "sbox-2" sboxname3 = "sbox-3" Loading Loading @@ -175,7 +174,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username1, password2) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading @@ -202,7 +201,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username2, password1) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading @@ -229,7 +228,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username3, password2) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading Loading @@ -268,7 +267,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username1,password1) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading Loading
go-packages/meep-users/db.go +13 −13 Original line number Diff line number Diff line Loading @@ -22,6 +22,8 @@ import ( "strings" log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger" _ "github.com/lib/pq" ) // DB Config Loading Loading @@ -55,7 +57,6 @@ type User struct { // Connector - Implements a Postgis SQL DB connector type Connector struct { name string namespace string dbName string db *sql.DB connected bool Loading @@ -72,7 +73,6 @@ func NewConnector(name, user, pwd, host, port string) (pc *Connector, err error) // Create new connector pc = new(Connector) pc.name = name pc.namespace = "default" // Connect to Postgis DB for retry := 0; retry <= DbMaxRetryCount; retry++ { Loading Loading @@ -166,9 +166,9 @@ func (pc *Connector) CreateTables() (err error) { _, err = pc.db.Exec(`CREATE TABLE ` + UsersTable + ` ( id SERIAL PRIMARY KEY, username varchar(36) NOT NULL UNIQUE, password varchar(36) NOT NULL, role varchar(36) NOT NULL DEFAULT 'user' sboxname varchar(36) NOT NULL DEFAULT '' password varchar(100) NOT NULL, role varchar(36) NOT NULL DEFAULT 'user', sboxname varchar(11) NOT NULL DEFAULT '' )`) if err != nil { log.Error(err.Error()) Loading Loading @@ -236,7 +236,7 @@ func (pc *Connector) UpdateUser(username string, password string, role string, s if password != "" { query := `UPDATE ` + UsersTable + ` SET password = crypt('`+password+`', gen_salt('bf')) WHERE name = ($1)` WHERE username = ($1)` _, err = pc.db.Exec(query, username) if err != nil { log.Error(err.Error()) Loading @@ -251,7 +251,7 @@ func (pc *Connector) UpdateUser(username string, password string, role string, s } query := `UPDATE ` + UsersTable + ` SET role = $2 WHERE name = ($1)` WHERE username = ($1)` _, err = pc.db.Exec(query, username, role) if err != nil { log.Error(err.Error()) Loading @@ -262,7 +262,7 @@ func (pc *Connector) UpdateUser(username string, password string, role string, s if sboxname != "" { query := `UPDATE ` + UsersTable + ` SET sboxname = $2 WHERE name = ($1)` WHERE username = ($1)` _, err = pc.db.Exec(query, username, sboxname) if err != nil { log.Error(err.Error()) Loading @@ -286,7 +286,7 @@ func (pc *Connector) GetUser(username string) (user *User, err error) { rows, err = pc.db.Query(` SELECT id, username, password, role, sboxname FROM `+UsersTable+` WHERE name = ($1)`, username) WHERE username = ($1)`, username) if err != nil { log.Error(err.Error()) return nil, err Loading Loading @@ -359,7 +359,7 @@ func (pc *Connector) DeleteUser(username string) (err error) { return err } _, err = pc.db.Exec(`DELETE FROM `+UsersTable+` WHERE name = ($1)`, username) _, err = pc.db.Exec(`DELETE FROM `+UsersTable+` WHERE username = ($1)`, username) if err != nil { log.Error(err.Error()) return err Loading Loading @@ -389,7 +389,7 @@ func (pc *Connector) IsValidUser(username string) (valid bool, err error){ rows, err := pc.db.Query(` SELECT id FROM `+UsersTable+` WHERE name = ($1)`, username) WHERE username = ($1)`, username) if err != nil { log.Error(err.Error()) return false, err Loading Loading @@ -423,8 +423,8 @@ func (pc *Connector) AuthenticateUser(username string, password string) (authent rows, err := pc.db.Query(` SELECT id FROM `+UsersTable+` WHERE name = ($1) AND password = crypt('($2)', password)` , username, password) WHERE username = ($1) AND password = crypt('`+password+`', password)` , username) if err != nil { log.Error(err.Error()) return false, err Loading
go-packages/meep-users/db_test.go +5 −6 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ import ( const ( pcName = "pc" pcNamespace = "postgis-ns" pcDBUser = "postgres" pcDBPwd = "pwd" pcDBHost = "localhost" Loading @@ -46,7 +45,7 @@ const ( role2 = "user" role3 = "super" sboxname0 = "" sboxname0 = "123456789012345" // more than 11 chars sboxname1 = "sbox-1" sboxname2 = "sbox-2" sboxname3 = "sbox-3" Loading Loading @@ -175,7 +174,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username1, password2) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading @@ -202,7 +201,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username2, password1) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading @@ -229,7 +228,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username3, password2) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading Loading @@ -268,7 +267,7 @@ func TestPostgisCreateUser(t *testing.T) { t.Fatalf("Failed to authenticate user") } valid,err = pc.AuthenticateUser(username1,password1) if err == nil || valid { if err != nil || valid { t.Fatalf("Wrong user authentication") } Loading