-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
49 lines (41 loc) · 957 Bytes
/
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
package main
import (
"net/http"
"time"
)
func getSessionToken(r *http.Request) string {
return r.Header.Get("Authorization")
}
func getUsername(r *http.Request, secret string) string {
// This function is only called in endpoints wrapped around
// `requireLogin` middleware so this function can assume that some user
// is logged in
token := getSessionToken(r)
username, err := verifySignature(token, secret, 7*24*time.Hour)
if err != nil {
panic(err)
}
return username
}
func validateToken(secret, token string) bool {
_, err := verifySignature(token, secret, 7*24*time.Hour)
return err == nil
}
func login(name string, pwd [64]byte, secret string, userStore userStore) (ok bool, token string) {
correctPwd, err := userStore.userPassword(name)
if err != nil {
ok = false
return
}
if correctPwd != pwd {
ok = false
return
}
token, err = sign(name, secret)
if err != nil {
ok = false
return
}
ok = true
return
}