-
-
Notifications
You must be signed in to change notification settings - Fork 554
/
auth.go
134 lines (117 loc) · 2.8 KB
/
auth.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
package auth
import (
"encoding/csv"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"github.com/gorilla/sessions"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/bcrypt"
)
type Users struct {
Lookup map[string]string
store sessions.Store
sessionTimeout int
}
type UserPW struct {
Name string
Pass string
}
func read(r io.Reader) ([]UserPW, error) {
reader := csv.NewReader(r)
reader.Comma = ':'
reader.Comment = '#'
reader.TrimLeadingSpace = true
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
result := []UserPW{}
for _, record := range records {
if len(record) != 2 {
return nil, errors.New("malformed users file")
}
result = append(result, UserPW{Name: record[0], Pass: record[1]})
}
return result, nil
}
func ReadPasswordsFile(path string, secret []byte, sessionTimeout int) (*Users, error) {
users := &Users{
Lookup: map[string]string{},
sessionTimeout: sessionTimeout,
store: sessions.NewCookieStore(secret),
}
if path == "" {
log.Info().Msg("Users file not specified")
return users, nil
}
file, err := os.Open(path)
if err != nil {
return users, err
}
defer file.Close()
userPws, err := read(file)
if err != nil {
return users, err
}
for _, record := range userPws {
users.Lookup[record.Name] = record.Pass
}
log.Info().Int("amount", len(users.Lookup)).Msg("Loaded Users")
return users, nil
}
type Response struct {
Message string `json:"message"`
}
func (u *Users) CurrentUser(r *http.Request) (string, bool) {
s, _ := u.store.Get(r, "user")
user, ok := s.Values["user"].(string)
if !ok {
return "guest", ok
}
return user, ok
}
func (u *Users) Logout(w http.ResponseWriter, r *http.Request) {
session := sessions.NewSession(u.store, "user")
session.IsNew = true
if err := u.store.Save(r, w, session); err != nil {
w.WriteHeader(500)
_ = json.NewEncoder(w).Encode(&Response{
Message: err.Error(),
})
return
}
w.WriteHeader(200)
}
func (u *Users) Authenticate(w http.ResponseWriter, r *http.Request) {
user := r.FormValue("user")
pass := r.FormValue("pass")
if !u.Validate(user, pass) {
w.WriteHeader(401)
_ = json.NewEncoder(w).Encode(&Response{
Message: "could not authenticate",
})
return
}
session := sessions.NewSession(u.store, "user")
session.IsNew = true
session.Options.MaxAge = u.sessionTimeout
session.Values["user"] = user
if err := u.store.Save(r, w, session); err != nil {
w.WriteHeader(500)
_ = json.NewEncoder(w).Encode(&Response{
Message: err.Error(),
})
return
}
w.WriteHeader(200)
_ = json.NewEncoder(w).Encode(&Response{
Message: "authenticated",
})
}
func (u Users) Validate(user, password string) bool {
realPassword, exists := u.Lookup[user]
return exists && bcrypt.CompareHashAndPassword([]byte(realPassword), []byte(password)) == nil
}