diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-01 14:07:39 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-01 14:07:39 -0500 |
commit | 416055e02b74f640ab6030deaa9f7767221a49cd (patch) | |
tree | ae6f1696404f083c805297bbd025a240841e641e | |
parent | aa7c3eb23c31fa6051ee0caaebea47c8225d55a4 (diff) |
add additional configuration functions for users and endpoints
-rw-r--r-- | db/db.go | 3 | ||||
-rw-r--r-- | db/endpoint.go | 13 | ||||
-rw-r--r-- | db/user.go | 28 |
3 files changed, 40 insertions, 4 deletions
@@ -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) @@ -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 |