summaryrefslogtreecommitdiff
path: root/db/user.go
blob: 1aff73fc9b500b5eb2d802166e0d6df43e52f6c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package db

import (
	"golang.org/x/crypto/argon2"
	"crypto/rand"
	"encoding/base64"
	"database/sql"
)


func getNextUserId(db *sql.DB) (int, error) {
	row := db.QueryRow("select coalesce(max(userid), 0) from users;")
	if row.Err() != nil {
		return -1, row.Err()
	}

	var id int
	err := row.Scan(&id)
	if err != nil {
		return -1, err
	}
	return id+1, nil
}


func hashPassword(password string, salt []byte) []byte {
	return argon2.IDKey([]byte(password), salt, 1, 64*1024, 4, 32)
}


func (p *Phlox) CreateUser(username, password string) (User, error) {
	user := User{}

	userId, err := getNextUserId(p.db)
	if err != nil {
		return user, err
	}

	salt := make([]byte, 10)
	_, err = rand.Read(salt)
	if err != nil {
		return user, err
	}

	hash := hashPassword(password, salt)

	hash64 := base64.StdEncoding.EncodeToString(hash)
	salt64 := base64.StdEncoding.EncodeToString(salt)

	_, err = p.db.Exec("insert into users (userid, username, passwordhash, salt) values (?, ?, ?, ?)", userId, username, hash64, salt64)
	if err != nil {
		return user, err
	}

	user.Id = userId
	user.Name = username
	user.PasswordHash = hash
	user.Salt = salt

	return user, nil
}



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 := hashPassword(password, user.Salt)
	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
	var hash64 string
	var salt64 string
	err := s.Scan(&userid, &username, &hash64, &salt64)
	if err != nil {
		return User{}, err
	}

	hash, err := base64.StdEncoding.DecodeString(hash64)
	if err != nil {
		return User{}, err
	}
	salt, err := base64.StdEncoding.DecodeString(salt64)
	if err != nil {
		return User{}, err
	}

	user := User{
		Id: userid,
		Name: username,
		PasswordHash: hash,
		Salt: salt,
	}

	return user, nil
}


func (p *Phlox) AuthenticateUser(username, password string) (bool, User, error) {
	row := p.db.QueryRow("select * from users where username = ?;", username)
	user, err := extractUser(row)
	if err != nil {
		return false, User{}, err
	}

	hash := hashPassword(password, user.Salt)
	for i, v := range user.PasswordHash {
		if v != hash[i] { return false, user, nil; }
	}
	return true, user, nil
}


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)
	if err != nil {
		return User{}, err
	}

	return user, nil
}


func (p *Phlox) AllUsers() ([]User, error) {
	users := make([]User, 0)
	rows, err := p.db.Query("select * from users;")
	if err != nil {
		return users, err
	}
	defer rows.Close()

	for rows.Next() {
		user, err := extractUser(rows)
		if err != nil {
			return users, err
		}
		users = append(users, user)
	}

	return users, nil
}