From 416055e02b74f640ab6030deaa9f7767221a49cd Mon Sep 17 00:00:00 2001 From: sanine-a Date: Mon, 1 May 2023 14:07:39 -0500 Subject: add additional configuration functions for users and endpoints --- db/db.go | 3 +++ db/endpoint.go | 13 ++++++++++++- db/user.go | 28 +++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 4 deletions(-) (limited to 'db') diff --git a/db/db.go b/db/db.go index d9faff3..a9a9a2e 100644 --- a/db/db.go +++ b/db/db.go @@ -41,6 +41,7 @@ type Model interface { GetSchemaVersion() (int, error) CreateUser(username, password string) (User, error) + SetPassword(user User, password string) error AuthenticateUser(username, password string) (User, error) GetById(id string) (User, error) AllUsers() ([]User, error) @@ -53,6 +54,8 @@ type Model interface { CreateEndpoint(name, path, address string) (Endpoint, error) DeleteEndpoint(endpoint Endpoint) error + SetEndpointPath(endpoint Endpoint, path string) error + SetEndpointAddress(endpoint Endpoint, address string) error GetEndpointByName(name string) (Endpoint, error) GetEndpointByPath(path string) (Endpoint, error) GetEndpointByAddress(address string) (Endpoint, error) diff --git a/db/endpoint.go b/db/endpoint.go index f22d77b..cb59de5 100644 --- a/db/endpoint.go +++ b/db/endpoint.go @@ -23,7 +23,7 @@ func (p *Phlox) CreateEndpoint(name, path, address string) (Endpoint, error) { Path: path, Address: address, } - + return endpoint, nil } @@ -46,6 +46,17 @@ func extractEndpoint(s Scanner) (Endpoint, error) { } +func (p *Phlox) SetEndpointPath(endpoint Endpoint, path string) error { + _, err := p.db.Exec("update endpoints set path=? where endpointid=?", path, endpoint.Id) + return err +} + +func (p *Phlox) SetEndpointAddress(endpoint Endpoint, address string) error { + _, err := p.db.Exec("update endpoints set address=? where endpointid=?", address, endpoint.Id) + return err +} + + func queryEndpoint(p *Phlox, query, param string) (Endpoint, error) { row := p.db.QueryRow(query, param) endpoint, err := extractEndpoint(row) diff --git a/db/user.go b/db/user.go index 37c0744..c1f2efe 100644 --- a/db/user.go +++ b/db/user.go @@ -30,6 +30,17 @@ func saltPassword(password string, salt []byte) []byte { } +func hashPassword(password string, salt []byte) ([]byte, error) { + salted := saltPassword(password, salt) + hash, err := bcrypt.GenerateFromPassword(salted, bcrypt.DefaultCost) + if err != nil { + return []byte{}, err + } + + return hash, nil +} + + func (p *Phlox) CreateUser(username, password string) (User, error) { user := User{} @@ -44,9 +55,7 @@ func (p *Phlox) CreateUser(username, password string) (User, error) { return user, err } - salted := saltPassword(password, salt) - - hash, err := bcrypt.GenerateFromPassword(salted, bcrypt.DefaultCost) + hash, err := hashPassword(password, salt) if err != nil { return user, err } @@ -69,6 +78,19 @@ func (p *Phlox) CreateUser(username, password string) (User, error) { +func (p *Phlox) SetPassword(user User, password string) error { + hash, err := hashPassword(password, user.Salt) + if err != nil { + return err + } + hash64 := base64.StdEncoding.EncodeToString(hash) + + _, err = p.db.Exec("update users set passwordhash=? where userid=?;", hash64, user.Id) + return err +} + + + func extractUser(s Scanner) (User, error) { var userid int var username string -- cgit v1.2.1