Compare commits
No commits in common. "8d47146a7c94b869d582ce3e21addfae0950f81f" and "50895249df218d4395e0b54d2017fa79bd523cff" have entirely different histories.
8d47146a7c
...
50895249df
@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
@ -33,64 +32,3 @@ func OpenDB(dbName string) (*DB, error) {
|
||||
|
||||
return &db, nil
|
||||
}
|
||||
|
||||
func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool) error {
|
||||
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating password hash: %v", err)
|
||||
}
|
||||
|
||||
if !permissionsOK(writer, editor, admin) {
|
||||
return fmt.Errorf("error with mutually exclusive permissions: writer = %v, editor = %v, admin = %v",
|
||||
writer, editor, admin)
|
||||
}
|
||||
|
||||
query := `
|
||||
INSERT INTO users
|
||||
(username, password, first_name, last_name, writer, editor, admin)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
_, err = db.Exec(query, user, hashedPass, first, last, writer, editor, admin)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error inserting user into DB: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
|
||||
var oldHashedPass string
|
||||
|
||||
selectQuery := `
|
||||
SELECT password FROM
|
||||
users
|
||||
WHERE
|
||||
id = ?
|
||||
`
|
||||
row := db.QueryRow(selectQuery, id)
|
||||
if err := row.Scan(&oldHashedPass); err != nil {
|
||||
return fmt.Errorf("error reading password from DB: %v", err)
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(oldHashedPass), []byte(oldPass)); err != nil {
|
||||
return fmt.Errorf("error checking password: %v", err)
|
||||
}
|
||||
|
||||
newHashedPass, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating password hash: %v", err)
|
||||
}
|
||||
|
||||
updateQuery := `
|
||||
UPDATE users
|
||||
SET password = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
_, err = db.Exec(updateQuery, newHashedPass, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error updating password in DB: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -50,9 +50,3 @@ func getCredentials() (string, string, error) {
|
||||
|
||||
return user, pass, nil
|
||||
}
|
||||
|
||||
func permissionsOK(writer, editor, admin bool) bool {
|
||||
return writer && !editor && !admin ||
|
||||
!writer && editor && !admin ||
|
||||
!writer && !editor && admin
|
||||
}
|
||||
|
1
go.mod
1
go.mod
@ -7,7 +7,6 @@ require (
|
||||
github.com/gorilla/feeds v1.1.2
|
||||
github.com/microcosm-cc/bluemonday v1.0.26
|
||||
github.com/yuin/goldmark v1.7.0
|
||||
golang.org/x/crypto v0.14.0
|
||||
golang.org/x/term v0.17.0
|
||||
)
|
||||
|
||||
|
2
go.sum
2
go.sum
@ -16,8 +16,6 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
|
||||
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
|
Loading…
x
Reference in New Issue
Block a user