DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 62e1b4ec72bb9bab9b588792f172631deb78e38a felu/internal/db/users.go -rw-r--r-- 3.6 KiB
62e1b4ecJonni Liljamo feat: logout 1 year, 25 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
/*
 * Copyright (C) 2023 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"
)

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
}

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
}

type User struct {
	Id    string
	Email   string
	IsAdmin bool
}

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
}

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
}

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
}

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
}

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
}

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
}

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
}