-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
183 lines (159 loc) · 4.89 KB
/
users.go
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
package goodidea
import (
"context"
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"fmt"
"log/slog"
"net/http"
"strings"
"time"
//"github.com/rs/xid"
)
//go:generate go run static_pages.go
// users within the system
//
// id - unique ID for the user, an xid prefixed with 'u'
// name - the username for the given user
// salt - the salt used befored hashing the passwd
// passwd - the user's password used for logging in
// createdAt - the date only, day the user signed up
// admin - if the user is an admin user or not
type user struct {
ID string `json:"id" validate:"required,u20"`
Name string `json:"name" validate:"required,max=48"`
Salt string `json:"-" validate:"omitempty,len=8"`
Passwd string `json:"passwd" validate:"required,sha256"`
CreatedAt time.Time `json:"createdAt"`
Admin bool `json:"admin"`
}
// sessions are for user logins
type session struct {
//session IDs should be cryptographically secure
SessionID string `json:"sessionId"`
//the user in question
UserID string `json:"userId" validate:"required,u20"`
//When the token was
CreatedAt time.Time `json:"createdAt"`
}
func handleSignUp(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
slog.Error("Error parsing sign-up form", "error", err)
fmt.Fprintf(w, "<p>Oops! Looks like the form wasn't submitted correctly</p>")
return
}
pw := strings.TrimSpace(r.FormValue("password"))
if pw == "" {
fmt.Fprintf(w, "<p>Oops! Looks like the no password was submitted</p>")
return
}
if len(pw) < 12 {
fmt.Fprintf(w, "<p>Oops! Looks like the password is not long enough (<12)</p>")
return
}
u := strings.TrimSpace(r.FormValue("username"))
if u != "" {
fmt.Fprintf(w, "<p>Oops! Looks like the no username was submitted</p>")
return
}
exists, err := checkNameExistence(u)
if err != nil {
slog.Error("Error checking for username existence", "error", err)
fmt.Fprintf(w, "<p>Oops! Server error attempting to sign you up</p>")
return
}
if exists {
fmt.Fprintf(w, "<p>Sorry, that username is already taken.</p>")
return
}
if err := createUser(u, pw); err != nil {
slog.Error("Error checking for username existence", "error", err)
fmt.Fprintf(w, "<p>Oops! Server error attempting to sign you up</p>")
return
}
http.Redirect(w, r, "/login", http.StatusFound)
}
func createUser(name, passwd string) error {
s := generateRandomSalt()
p := hashPassword(passwd, s)
return insertUser(name, p, string(s))
}
func checkNameExistence(u string) (bool, error) {
ctx := context.TODO()
var exists bool
q := "SELECT EXISTS(SELECT 1 FROM users WHERE name= $1)"
if err := DB.QueryRow(ctx, q).Scan(&exists); err != nil {
slog.Error("Unable to query the database for username", "name", u, "error", err)
return exists, err
}
return exists, nil
}
// persistSession in-case of server reload
func persistSession(s *session) error {
ctx := context.TODO()
q := "INSERT INTO sessions(session_id, user_id, created_at) VALUES ($1, $2, $3)"
_, err := DB.Exec(ctx, q, s.SessionID, s.UserID, s.CreatedAt)
return err
}
// Create a new user
func insertUser(name, passwd, salt string) error {
ctx := context.TODO()
q := "INSERT INTO users(name, passwd, salt) VALUES ($1, $2, $3)"
_, err := DB.Exec(ctx, q, name, passwd, salt)
return err
}
// createUserSession – Create a cryptographically secure 64 length string to use
// as a session ID
func createUserSession(u *user) (*session, error) {
b := make([]byte, 32)
_, err := rand.Read(b[:])
if err != nil {
return nil, err
}
var sessionId = hex.EncodeToString(b)
return &session{
SessionID: sessionId,
UserID: u.ID,
CreatedAt: time.Now(),
}, nil
}
// getUser by username to check password in a loging
func getUser(name string) (u *user, err error) {
ctx := context.Background()
q := `SELECT id, salt, passwd FROM users WHERE name = $1`
if err := DB.QueryRow(ctx, q).Scan(&u.ID, &u.Salt, &u.Passwd); err != nil {
return u, err
}
u.Name = name
return u, nil
}
// Check if two passwords match
func doPasswordsMatch(hashedPassword, currPassword string, salt []byte) bool {
var currPasswordHash = hashPassword(currPassword, salt)
return hashedPassword == currPasswordHash
}
func generateRandomSalt() []byte {
var salt = make([]byte, 8)
_, err := rand.Read(salt[:])
if err != nil {
panic(err)
}
return salt
}
// Combine password and salt then hash them using the SHA-512
// hashing algorithm and then return the hashed password
// as a hex string
func hashPassword(password string, salt []byte) string {
// Convert password string to byte slice
var passwordBytes = []byte(password)
// Append salt to password
passwordBytes = append(passwordBytes, salt...)
var sha512Hasher = sha512.New()
sha512Hasher.Write(passwordBytes)
var hashedPasswordBytes = sha512Hasher.Sum(nil)
// Convert the hashed password to a hex string
var hashedPasswordHex = hex.EncodeToString(hashedPasswordBytes)
return hashedPasswordHex
}