-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauth.go
60 lines (53 loc) · 1.21 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
package main
import (
"encoding/json"
"log"
)
const (
authFileName = "auth.db"
)
type AuthToken struct {
Token string `json:token`
Email string `json:email`
}
type TokenDB []AuthToken
func makeTokenDB(b []byte) TokenDB {
var tokens TokenDB
err := json.Unmarshal(b, &tokens)
if err != nil {
log.Println("error reading auth token db:", err)
}
for i, entry := range tokens {
if entry.Token == "" {
log.Printf("token field empty or missing in entry #%d", i)
return nil
}
if entry.Email == "" {
log.Printf("email field empty or missing in entry #%d", i)
return nil
}
}
log.Printf("found %d auth tokens\n", len(tokens))
return tokens
}
func (db TokenDB) findToken(token string) (email string) {
for _, i := range db {
if i.Token == token {
email = i.Email
return
}
}
return
}
// isAuthorized tries to find the auth token given in entry.
// It will the change the entry parameter by replacing the auth
// token with the associated email address. This is to have the
// auth token not end up in the secret database.
func (db TokenDB) isAuthorized(entry *StoreEntry) bool {
email := db.findToken(entry.AuthToken)
if email == "" {
return false
}
entry.AuthToken = email
return true
}