summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-05-02 14:00:28 -0500
committersanine-a <sanine.not@pm.me>2023-05-02 14:00:28 -0500
commit9fe48ae66c2cc8d6b46e44ae35e9447ab51d9dd6 (patch)
tree94aff558aedaf3c1f7452983887b6f3479c9cef7
parent4343925a09c1fd34da4b352ae9fa2510397ccdcd (diff)
add user.go
-rw-r--r--conf/main.go8
-rw-r--r--conf/user.go105
-rw-r--r--db/db.go2
-rw-r--r--db/user.go17
4 files changed, 132 insertions, 0 deletions
diff --git a/conf/main.go b/conf/main.go
index 052f086..f3a321e 100644
--- a/conf/main.go
+++ b/conf/main.go
@@ -33,12 +33,20 @@ func RegisterCommand(val int, cmd *Command) {
}
+func OpenDb() {
+ err := p.Open(params.DbFilename)
+ if err != nil { log.Fatal(err) }
+}
+
+
func main() {
p = &db.Phlox{}
cmds := map[int]*Command{
1: Create(),
2: SchemaVersion(),
+
+ 3: CreateUser(),
}
for val, cmd := range cmds {
diff --git a/conf/user.go b/conf/user.go
new file mode 100644
index 0000000..e4a0169
--- /dev/null
+++ b/conf/user.go
@@ -0,0 +1,105 @@
+package main
+
+import (
+ "os"
+ "fmt"
+ "log"
+ "strings"
+ "bufio"
+ "syscall"
+ "golang.org/x/term"
+ db "sanine.net/git/phlox/db"
+)
+
+
+func GetUsername(prompt string) (string, error) {
+ reader := bufio.NewReader(os.Stdin)
+
+ fmt.Print(prompt)
+ username, err := reader.ReadString('\n')
+ if err != nil {
+ return "", err
+ }
+
+ return strings.TrimSpace(username), nil
+}
+
+
+func GetPassword(prompt string) (string, error) {
+ fmt.Print(prompt)
+ bytes, err := term.ReadPassword(int(syscall.Stdin))
+ if err != nil {
+ return "", err
+ }
+
+ password := strings.TrimSpace(string(bytes))
+ return password, nil
+}
+
+
+func ConfirmPassword() string {
+ password, err := GetPassword("Enter Password: ")
+ if err != nil { log.Fatal(err) }
+ password1, err := GetPassword("Confirm Password: ")
+ if err != nil { log.Fatal(err) }
+ if password != password1 { log.Fatal("passwords do not match!") }
+ return password
+}
+
+
+func GetUser() User {
+ OpenDb()
+ username, err := GetUsername("Username: ")
+ if err != nil { log.Fatal(err) }
+ user, err := p.GetByUsername(username)
+ if err != nil { log.Fatal(err) }
+ return user
+}
+
+
+func CreateUser() *Command {
+ return &Command{
+ Flag: "create-user",
+ Usage: "create a new user",
+ Invoke: func() {
+ OpenDb()
+
+ username, err := GetUsername("Enter Username: ")
+ if err != nil { log.Fatal(err) }
+ password := ConfirmPassword()
+
+ _, err = p.CreateUser(username, password1)
+ if err != nil { log.Fatal(err) }
+ fmt.Printf("created user %v\n", username)
+ },
+ }
+}
+
+
+func DeleteUser() *Command {
+ return &Command{
+ Flag: "delete-user",
+ Usage: "delete an existing user",
+ Invoke: func() {
+ user := GetUser()
+ err := p.DeleteUser(user)
+ if err != nil { log.Fatal(err) }
+ fmt.Printf("deleted user %v\n", user.Name)
+ },
+ }
+}
+
+
+func UpdatePassword() *Command {
+ return &Command{
+ Flag: "update-password",
+ Usage: "update a user's password",
+ Invoke: func() {
+ user := GetUser()
+ password := ConfirmPassword()
+ err := SetPassword(user, password)
+ if err != nil { log.Fatal(err) }
+ fmt.Printf("updated password for user %v\n", user.Name)
+ },
+ }
+}
diff --git a/db/db.go b/db/db.go
index a9a9a2e..87784ff 100644
--- a/db/db.go
+++ b/db/db.go
@@ -41,8 +41,10 @@ type Model interface {
GetSchemaVersion() (int, error)
CreateUser(username, password string) (User, error)
+ DeleteUser(user User) error
SetPassword(user User, password string) error
AuthenticateUser(username, password string) (User, error)
+ GetByUsername(username string) (User, error)
GetById(id string) (User, error)
AllUsers() ([]User, error)
diff --git a/db/user.go b/db/user.go
index c1f2efe..61e05ad 100644
--- a/db/user.go
+++ b/db/user.go
@@ -78,6 +78,13 @@ func (p *Phlox) CreateUser(username, password string) (User, error) {
+func (p *Phlox) DeleteUser(user User) error {
+ _, err := p.db.Exec("delete from users where userid=?;", user.Id)
+ return err
+}
+
+
+
func (p *Phlox) SetPassword(user User, password string) error {
hash, err := hashPassword(password, user.Salt)
if err != nil {
@@ -140,6 +147,16 @@ func (p *Phlox) AuthenticateUser(username, password string) (bool, User, error)
}
+func (p *Phlox) GetByUsername(username string) (User, error) {
+ row := p.db.QueryRow("select * from users where username = ?;", username)
+ user, err := extractUser(row)
+ if err != nil {
+ return User{}, err
+ }
+ return user, nil
+}
+
+
func (p *Phlox) GetById(id int) (User, error) {
row := p.db.QueryRow("select * from users where userid = ?;", id)
user, err := extractUser(row)