/
auth.go
389 lines (297 loc) · 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package core
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"errors"
"hash"
"net/http"
"strings"
"sync"
"time"
"code.google.com/p/go.crypto/bcrypt"
"github.com/garyburd/redigo/redis"
"github.com/kr/fernet"
"labix.org/v2/mgo/bson"
)
//Allow only verified ip's from config
func (s *Server) baseAuth(w http.ResponseWriter, r *http.Request) {
//ip := strings.Split(r.RemoteAddr, ":")
//fmt.Println(r.RemoteAddr)
//s.logger.SetPrefix("BaseAuth:")
//s.logger.Printf("%v %v", r.Method, r.URL.Path)
//TODO:IPV6 local loop back address resolves to ::1. Find a better solution
// if !s.ipAllowed(ip[0]) {
// http.Error(w, fmt.Sprintf("Unauthorized ip:%s not allowed", ip[0]), http.StatusUnauthorized)
// return
//
// }
}
//Allow ip
func (s *Server) ipAllowed(IP string) bool {
for _, client := range s.config.Clients {
if IP == client.IP {
return true
}
}
return false
}
//Authenticate developer. Access Token is generated using a shared secret between
//Hamster and the client. The shared secret is manually configured in hamster.toml.
//TODO: find a better implementation
func (s *Server) developerAuth(w http.ResponseWriter, r *http.Request) {
s.logger.SetPrefix("DeveloperAuth:")
//skip access token check if logging in
if r.Method == "POST" && r.URL.Path == "/api/v1/developers/login/" {
return
}
accessToken := r.Header.Get("X-Access-Token")
if accessToken == "" {
s.unauthorized(r, w, errors.New("token is empty"), "access token invalid")
return
}
//check POST /api/v1/developers
if r.Method == "POST" && r.URL.Path == "/api/v1/developers/" {
//check auth
if !s.validateSharedToken(accessToken) {
s.unauthorized(r, w, errors.New("shared token is old"), "access token invalid")
}
return
}
if _, ok := s.validateAccessLoginToken(accessToken); !ok {
s.unauthorized(r, w, errors.New("token is old"), "access token invalid")
return
}
}
//Validate access token
func (s *Server) validateAccessLoginToken(token string) (string, bool) {
btok, err := base64.URLEncoding.DecodeString(token)
if err != nil {
return "", false
}
k := fernet.MustDecodeKeys(s.config.Clients["browser"].Secret)
email := fernet.VerifyAndDecrypt(btok, 60*time.Second, k)
c := s.redisConn()
defer c.Close()
status, err := redis.String(c.Do("GET", email))
if err != nil {
return "", false
}
if status == "loggedin" {
return string(email), true
}
return "", false
}
//Validate access token
func (s *Server) validateSharedToken(token string) bool {
btok, err := base64.URLEncoding.DecodeString(token)
if err != nil {
return false
}
k := fernet.MustDecodeKeys(s.config.Clients["browser"].Secret)
sharedToken := fernet.VerifyAndDecrypt(btok, 60*10*time.Second, k)
if string(sharedToken) == string(s.config.Clients["browser"].Token) {
return true
}
return false
}
//Authenticates object level requests
func (s *Server) objectAuth(w http.ResponseWriter, r *http.Request) {
s.logger.SetPrefix("ObjectAuth:")
apiToken := r.Header.Get("X-Api-Token")
apiSecret := r.Header.Get("X-Api-Secret")
if apiToken == "" || apiSecret == "" {
s.unauthorized(r, w, errors.New("token or secret invalid"), "access token invalid")
return
}
if !s.validateAPIToken(apiToken, apiSecret) {
s.unauthorized(r, w, errors.New("token match failed"), "access token failed")
return
}
}
func (s *Server) validateAPIToken(token string, secret string) bool {
if ok, hash, salt := s.getHashSalt(token); ok {
//fmt.Println("found key in redis")
if matchPassword(decodeToken(secret), hash, salt) {
return true
}
}
if ok, hash, salt := s.getHashSaltFromDB(token); ok {
if matchPassword(decodeToken(secret), hash, salt) {
return true
}
}
return false
}
func (s *Server) getHashSalt(token string) (bool, string, string) {
c := s.redisConn()
defer c.Close()
hash, err := redis.String(c.Do("GET", token+":hash"))
if err != nil {
return false, "", ""
}
salt, err := redis.String(c.Do("GET", token+":salt"))
if err != nil {
return false, "", ""
}
return true, hash, salt
}
func (s *Server) getHashSaltFromDB(token string) (bool, string, string) {
//get collection
appID := decodeToken(token)
session := s.db.GetSession()
defer session.Close()
c := session.DB("").C(aName)
app := app{}
//TODO:select fields
if err := c.FindId(bson.ObjectIdHex(appID)).One(&app); err != nil {
//fmt.Println("app not found\n")
return false, "", ""
}
//cache hash-salt in redis
rc := s.redisConn()
defer rc.Close()
rc.Do("SET", token+":hash", app.Hash)
rc.Do("SET", token+":salt", app.Salt)
return true, app.Hash, app.Salt
}
//Get Basic user password
func getUserPassword(r *http.Request) (string, string) {
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 || s[0] != "Basic" {
return "", ""
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
return "", ""
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return "", ""
}
return pair[0], pair[1]
}
type hmacFactory struct {
hashFunc hash.Hash
m sync.Mutex
}
/*Generates hash*/
func (h *hmacFactory) generateHash(data []byte) []byte {
h.m.Lock()
defer h.m.Unlock()
h.hashFunc.Reset()
h.hashFunc.Write(data)
return h.hashFunc.Sum(nil)
}
/*Encrypts the password and returns password hash*/
func (h *hmacFactory) encrypt(password []byte, cost int) ([]byte, error) {
return bcrypt.GenerateFromPassword(h.generateHash(password), cost)
}
func (h *hmacFactory) compare(hash, password []byte) error {
return bcrypt.CompareHashAndPassword(hash, h.generateHash(password))
}
/*Returns a hmac type*/
func newHMAC(hash func() hash.Hash, salt []byte) *hmacFactory {
hm := &hmacFactory{
hashFunc: hmac.New(hash, salt),
m: sync.Mutex{},
}
return hm
}
/*Encrypts password. Returns hash+salt*/
func encryptPassword(password string) (string, string, error) {
salt, err0 := genUUID(16)
if err0 != nil {
return "", "", err0
}
hm := newHMAC(sha512.New, []byte(salt))
pass := []byte(password)
encrypted, err := hm.encrypt(pass, bcrypt.DefaultCost)
if err != nil {
return "", "", err
}
return string(encrypted), salt, nil
}
/*Match encrypted string*/
func matchPassword(password string, hash string, salt string) bool {
p := []byte(password)
h := []byte(hash)
s := []byte(salt)
hm := newHMAC(sha512.New, s)
err := hm.compare(h, p)
if err != nil {
return false
}
return true
}
/*Encode to base64*/
func encodeBase64Token(hexVal string) string {
token := base64.URLEncoding.EncodeToString([]byte(hexVal))
return token
}
/*Decode from base64*/
func decodeToken(token string) string {
hexVal, err := base64.URLEncoding.DecodeString(token)
if err != nil {
return ""
}
return string(hexVal)
}
/*Generate uuid*/
func genUUID(size int) (string, error) {
uuid := make([]byte, size)
n, err := rand.Read(uuid)
if n != len(uuid) || err != nil {
return "", err
}
uuid[8] = 0x80
uuid[4] = 0x40
return hex.EncodeToString(uuid), nil
}
/*
Return a random 16-byte base64 alphabet string
*/
func randomKey() string {
k := make([]byte, 12)
for bytes := 0; bytes < len(k); {
n, err := rand.Read(k[bytes:])
if err != nil {
panic("rand.Read() failed")
}
bytes += n
}
return base64.StdEncoding.EncodeToString(k)
}
//Generate time based access token using shared secret. See fernet project
//for more details
func (s *Server) genAccessToken(email string) (string, error) {
//encrypt token
k := fernet.MustDecodeKeys(s.config.Clients["browser"].Secret)
tok, err := fernet.EncryptAndSign([]byte(email), k[0])
if err != nil {
}
token := base64.URLEncoding.EncodeToString(tok)
//cache it
c := s.redisConn()
defer c.Close()
c.Do("SET", email, "loggedin")
return token, nil
}
//Logout developer
//TODO: find a better way to handle login/logout
func (s *Server) logout(email string) error {
c := s.redisConn()
defer c.Close()
status, err := redis.String(c.Do("GET", email))
if err != nil {
return err
}
if status == "loggedin" {
c.Do("SET", email, "") //log out
} else {
return errors.New("user is not logged in")
}
return nil
}