This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
200 lines (167 loc) · 4.02 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
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
package splenda
import (
"crypto/hmac"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
)
type clock interface {
Now() time.Time
}
type realclock struct{}
func (realclock) Now() time.Time {
return time.Now()
}
// Auth implements Splenda's user authentication.
type Auth struct {
db *DB
cur string
keys map[string][]byte
clock clock
}
// NewAuth creates a new authenticator with a single key version.
// TODO: Support multiple key versions.
func NewAuth(db *DB, keystr string) (*Auth, error) {
key, err := base64.StdEncoding.DecodeString(keystr)
if err != nil {
return nil, fmt.Errorf("invalid keystr: %v", err)
}
return &Auth{
db: db,
cur: "1",
keys: map[string][]byte{
"1": key,
},
clock: realclock{},
}, nil
}
// ListUsers lists all the currently registered users.
func (a *Auth) ListUsers() ([]string, error) {
return a.db.ListUsers()
}
// NewUser creates a new game user.
func (a *Auth) NewUser(id, pw string) error {
if id == "" {
return errors.New("bad request: no id")
}
if pw == "" {
return errors.New("bad request: no password")
}
return a.db.NewUser(id, a.hashPW(pw))
}
// Login logs in a user and returns a session ID.
func (a *Auth) Login(id, pw string) (string, error) {
if id == "" {
return "", errors.New("bad request: no id")
}
if pw == "" {
return "", errors.New("bad request: no password")
}
hash, err := a.db.GetUserHash(id)
if err != nil {
return "", err
}
if !a.verifyPW(pw, hash) {
return "", errors.New("bad request: invalid password")
}
return a.generateSID(id), nil
}
// Authorize verifies a session ID to confirm that it's real, returning the
// associated user ID if it is.
func (a *Auth) Authorize(sid string) (string, error) {
id, ok := a.verifySID(sid)
if !ok {
return "", errors.New("invalid session id")
}
return id, nil
}
// HashPW hashes a password for storage in the database.
func (a *Auth) hashPW(pw string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(pw), bcrypt.MinCost)
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(hash)
}
// VerifyPW verifies a password against the hash from the database.
func (a *Auth) verifyPW(pw, hash string) bool {
hb, err := base64.StdEncoding.DecodeString(hash)
if err != nil {
return false
}
return bcrypt.CompareHashAndPassword(hb, []byte(pw)) == nil
}
type sid struct {
ID string `json:"i"`
Expires int64 `json:"e"`
}
// GenerateSID generates a session ID for the given user ID.
func (a *Auth) generateSID(id string) string {
sid := sid{
ID: id,
Expires: a.clock.Now().Add(14 * 24 * time.Hour).Unix(),
}
bs, err := json.Marshal(&sid)
if err != nil {
panic(err)
}
key := a.keys[a.cur]
sig := hash(bs, key)
return a.cur +
"." +
base64.StdEncoding.EncodeToString(bs) +
"." +
base64.StdEncoding.EncodeToString(sig)
}
// VerifySID verifies that a session ID is legit and returns the associated user ID.
func (a *Auth) verifySID(in string) (string, bool) {
parts := strings.Split(in, ".")
if len(parts) != 3 {
// SID is malformed.
return "", false
}
key, ok := a.keys[parts[0]]
if !ok {
// SID prefix contains an invalid key ID.
return "", false
}
bs, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
// SID body is invalid base-64.
return "", false
}
sig, err := base64.StdEncoding.DecodeString(parts[2])
if err != nil {
// SID signature is invalid base-64.
return "", false
}
asig := hash(bs, key)
if subtle.ConstantTimeCompare(sig, asig) != 1 {
// Signature mismatch.
return "", false
}
sid := sid{}
if err := json.Unmarshal(bs, &sid); err != nil {
// Signature matches but body is invalid JSON; lolwut?
return "", false
}
expires := time.Unix(sid.Expires, 0)
if expires.Before(a.clock.Now()) {
// SID has expired.
return "", false
}
return sid.ID, true
}
func hash(bs, key []byte) []byte {
hasher := hmac.New(sha256.New, key)
if _, err := hasher.Write(bs); err != nil {
panic(err)
}
return hasher.Sum(nil)
}