DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 5cb0ede24d635ae70163096f3119b99b72887786 felu/internal/db/users.go -rw-r--r-- 4.3 KiB
5cb0ede2Jonni Liljamo feat: handle root A 29 days ago
                                                                                
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

package db

import (
	"database/sql"
	"errors"

	"github.com/matthewhartstonge/argon2"
	"github.com/oklog/ulid/v2"
)

// CreateUser creates a user.
func CreateUser(email string, pwd string) error {
	argon := argon2.DefaultConfig()
	encoded, err := argon.HashEncoded([]byte(pwd))
	if err != nil {
		return err
	}

	ulid := ulid.Make().String()

	_, err = DBConn.Exec(`INSERT INTO users(id, email, pwd) VALUES ($1, $2, $3)`,
		ulid, email, string(encoded))
	if err != nil {
		return err
	}

	return nil
}

// CreateAdmin creates an admin user.
func CreateAdmin(email string, pwd string) error {
	argon := argon2.DefaultConfig()
	encoded, err := argon.HashEncoded([]byte(pwd))
	if err != nil {
		return err
	}

	ulid := ulid.Make().String()

	_, err = DBConn.Exec(`INSERT INTO users(id, email, pwd, is_admin) VALUES ($1, $2, $3, TRUE)`,
		ulid, email, string(encoded))
	if err != nil {
		return err
	}

	return nil
}

// User contains a users relevant information.
type User struct {
	ID      string
	Email   string
	IsAdmin bool
}

// FetchUserWithCreds fetches a users information based on given credentials.
func FetchUserWithCreds(email string, pwd string) (*User, error) {
	user := User{Email: email}
	var encodedPwd string
	err := DBConn.QueryRow(`SELECT id, pwd FROM users WHERE email = $1`,
		email).Scan(&user.ID, &encodedPwd)
	if err == sql.ErrNoRows {
		return nil, errors.New("User not found")
	}
	if err != nil {
		return nil, errors.New("User query failed")
	}

	ok, err := argon2.VerifyEncoded([]byte(pwd), []byte(encodedPwd))
	if err != nil {
		return nil, errors.New("User not found")
	}
	if !ok {
		return nil, errors.New("User not found")
	}

	return &user, nil
}

// FetchUserWithID fetches a users information.
func FetchUserWithID(id string) (*User, error) {
	user := User{ID: id}
	err := DBConn.QueryRow(`SELECT email, is_admin FROM users WHERE id = $1`,
		id).Scan(&user.Email, &user.IsAdmin)
	if err == sql.ErrNoRows {
		return nil, errors.New("User not found")
	}
	if err != nil {
		return nil, errors.New("User query failed")
	}

	return &user, nil
}

// FetchAllUsers fetches all users.
func FetchAllUsers() ([]User, error) {
	rows, err := DBConn.Query(`SELECT id, email, is_admin FROM users`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var users []User
	for rows.Next() {
		var user User
		err = rows.Scan(&user.ID, &user.Email, &user.IsAdmin)
		if err != nil {
			return nil, err
		}
		users = append(users, user)
	}
	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return users, nil
}

// FetchUserEmail fetches a users email address.
func FetchUserEmail(id string) (string, error) {
	var email string
	err := DBConn.QueryRow(`SELECT email FROM users WHERE id = $1`,
		id).Scan(&email)
	if err != nil {
		return "", err
	}

	return email, nil
}

// DeleteUser deletes a user.
func DeleteUser(id string) error {
	err := DeleteDomainsForUser(id)
	if err != nil {
		return err
	}
	_, err = DBConn.Exec(`DELETE FROM users WHERE id = $1`, id)
	if err != nil {
		return err
	}
	return nil
}

// UpdateUserEmail updates a users email address.
func UpdateUserEmail(id string, email string) error {
	_, err := DBConn.Exec(`UPDATE users SET email = $1 WHERE id = $2`,
		email, id)
	if err != nil {
		return err
	}
	return nil
}

// UpdateUserPassword updates a users password.
func UpdateUserPassword(id string, pwd string) error {
	argon := argon2.DefaultConfig()
	encoded, err := argon.HashEncoded([]byte(pwd))
	if err != nil {
		return err
	}

	_, err = DBConn.Exec(`UPDATE users SET pwd = $1 WHERE id = $2`,
		string(encoded), id)
	if err != nil {
		return err
	}

	return nil
}

// VerifyUserPassword verifies a users password.
func VerifyUserPassword(id string, pwd string) bool {
	// FIXME: Currently doesn't return any errors, and just return false in error cases
	// I mean... Shouldn't really error, but who knows
	var encodedPwd string
	err := DBConn.QueryRow(`SELECT pwd FROM users WHERE id = $1`,
		id).Scan(&encodedPwd)
	if err == sql.ErrNoRows {
		return false
	}
	if err != nil {
		return false
	}

	ok, err := argon2.VerifyEncoded([]byte(pwd), []byte(encodedPwd))
	if err != nil {
		return false
	}
	if !ok {
		return false
	}

	return true
}