-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorer.go
131 lines (104 loc) · 2.34 KB
/
storer.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
package main
import (
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
"gopkg.in/authboss.v0"
)
var nextUserID int
type User struct {
ID int
Name string
// Auth
//Email string
//Password string
// OAuth2
Oauth2Uid string
Oauth2Provider string
Oauth2Token string
Oauth2Refresh string
Oauth2Expiry time.Time
Email string
GooglePicture string
GoogleName string
GoogleLink string
// Confirm
//ConfirmToken string
//Confirmed bool
// Lock
//AttemptNumber int64
//AttemptTime time.Time
//Locked time.Time
// Recover
//RecoverToken string
//RecoverTokenExpiry time.Time
// Remember is in another table
}
type MemStorer struct {
Users map[string]User
Tokens map[string][]string
}
func NewMemStorer() *MemStorer {
return &MemStorer{
Users: make(map[string]User),
Tokens: make(map[string][]string),
}
}
func (s MemStorer) Create(key string, attr authboss.Attributes) error {
var user User
if err := attr.Bind(&user, true); err != nil {
return err
}
user.ID = nextUserID
nextUserID++
s.Users[key] = user
//fmt.Println("Create")
//spew.Dump(s.Users)
return nil
}
func (s MemStorer) Put(key string, attr authboss.Attributes) error {
return s.Create(key, attr)
}
func (s MemStorer) Get(key string) (result interface{}, err error) {
user, ok := s.Users[key]
if !ok {
return nil, authboss.ErrUserNotFound
}
return &user, nil
}
func (s MemStorer) PutOAuth(uid, provider string, attr authboss.Attributes) error {
return s.Create(uid+provider, attr)
}
func (s MemStorer) GetOAuth(uid, provider string) (result interface{}, err error) {
user, ok := s.Users[uid+provider]
if !ok {
return nil, authboss.ErrUserNotFound
}
return &user, nil
}
func (s MemStorer) AddToken(key, token string) error {
s.Tokens[key] = append(s.Tokens[key], token)
fmt.Println("AddToken")
spew.Dump(s.Tokens)
return nil
}
func (s MemStorer) DelTokens(key string) error {
delete(s.Tokens, key)
fmt.Println("DelTokens")
spew.Dump(s.Tokens)
return nil
}
func (s MemStorer) UseToken(givenKey, token string) error {
toks, ok := s.Tokens[givenKey]
if !ok {
return authboss.ErrTokenNotFound
}
for i, tok := range toks {
if tok == token {
toks[i], toks[len(toks)-1] = toks[len(toks)-1], toks[i]
s.Tokens[givenKey] = toks[:len(toks)-1]
return nil
}
}
return authboss.ErrTokenNotFound
}