-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
70 lines (61 loc) · 1.45 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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strings"
)
type User struct {
//ID int
Username string
Password string // This is a bcyrpt hashed password
Identity string // Hashed username for downstream systems
}
var Users []User
// GetUserByUsername fetches a user by username from the database
func GetUserByUsername(username string) *User {
// For now, let's assume we get some user or nil if not found
for _, user := range Users {
if user.Username == username {
return &user
}
}
return nil
}
func readUsers() {
// default data is example/password
csvUsers := `example "$2a$14$HNOQGnDpfyF/95TT6VToEuyS4NCYKXH1pVlcq9fx9JaC/zBW.cn0i"`
// use file if it was given
var data io.Reader
if config.UserFile == "" {
data = strings.NewReader(csvUsers)
} else {
file, err := os.Open(config.UserFile)
if err != nil {
log.Fatalln("Error opening user file:", err)
}
defer file.Close()
data = file
}
// parse file as 2 columns. space separated
r := csv.NewReader(data)
r.TrimLeadingSpace = true
r.FieldsPerRecord = 2
r.Comma = ' '
// read all entries into user array
records, err := r.ReadAll()
if err != nil {
log.Fatalln("error reading users", err)
}
Users = make([]User, len(records))
for i, record := range records {
log.Println(fmt.Sprintf("User %d: %s %s", i, record[0], anonymize(record[1])))
Users[i] = User{
Username: record[0],
Password: record[1],
Identity: createHash(record[0]),
}
}
}