forked from aarondl/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorer.go
More file actions
245 lines (208 loc) · 6.18 KB
/
Copy pathstorer.go
File metadata and controls
245 lines (208 loc) · 6.18 KB
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
package authboss
import (
"errors"
"fmt"
"reflect"
"time"
"unicode"
)
// UserNotFound should be returned from Get when the record is not found.
var UserNotFound = errors.New("User not found")
// TokenNotFound should be returned from UseToken when the record is not found.
var TokenNotFound = errors.New("Token not found")
// StorageOptions is a map depicting the things a module must be able to store.
type StorageOptions map[string]DataType
// Storer must be implemented in order to store the user's attributes somewhere.
// The type of store is up to the developer implementing it, and all it has to
// do is be able to store several simple types.
type Storer interface {
// Create is the same as put, except it refers to a non-existent key.
Create(key string, attr Attributes) error
// Put is for storing the attributes passed in. The type information can
// help serialization without using type assertions.
Put(key string, attr Attributes) error
// Get is for retrieving attributes for a given key. The return value
// must be a struct thot contains a field with the correct type as shown
// by attrMeta. If the key is not found in the data store simply
// return nil, UserNotFound.
Get(key string, attrMeta AttributeMeta) (interface{}, error)
}
// TokenStorer must be implemented in order to satisfy the remember module's
// storage requirements.
type TokenStorer interface {
Storer
// AddToken saves a new token for the key.
AddToken(key, token string) error
// DelTokens removes all tokens for a given key.
DelTokens(key string) error
// UseToken finds the token, removes the key/token entry in the store
// and returns the key that was found. If the token could not be found
// return "", TokenNotFound
UseToken(token string) (key string, err error)
}
// DataType represents the various types that clients must be able to store.
type DataType int
const (
Integer DataType = iota
String
DateTime
)
var dateTimeType = reflect.TypeOf(time.Time{})
func (d DataType) String() string {
switch d {
case Integer:
return "Integer"
case String:
return "String"
case DateTime:
return "DateTime"
}
return ""
}
// AttributeMeta stores type information for attributes.
type AttributeMeta map[string]DataType
// Names returns the names of all the attributes.
func (a AttributeMeta) Names() []string {
names := make([]string, len(a))
i := 0
for n, _ := range a {
names[i] = n
i++
}
return names
}
// Attribute is data along with type information.
type Attribute struct {
Type DataType
Value interface{}
}
// Attributes is just a key-value mapping of data.
type Attributes map[string]Attribute
// Names returns the names of all the attributes.
func (a Attributes) Names() []string {
names := make([]string, len(a))
i := 0
for n, _ := range a {
names[i] = n
i++
}
return names
}
// Bind the data in the attributes to the given struct. This means the
// struct creator must have read the documentation and decided what fields
// will be needed ahead of time.
func (a Attributes) Bind(strct interface{}) error {
structType := reflect.TypeOf(strct)
if structType.Kind() != reflect.Ptr {
return errors.New("Bind: Must pass in a struct pointer.")
}
structVal := reflect.ValueOf(strct).Elem()
structType = structVal.Type()
for k, v := range a {
if _, has := structType.FieldByName(k); !has {
return fmt.Errorf("Bind: Struct was missing %s field, type: %v", k, v.Type)
}
field := structVal.FieldByName(k)
if !field.CanSet() {
return fmt.Errorf("Bind: Found field %s, but was not writeable.", k)
}
fieldKind := field.Kind()
fieldType := field.Type()
switch v.Type {
case Integer:
if fieldKind != reflect.Int {
return fmt.Errorf("Bind: Field %s's type should be %s but was %s", k, reflect.Int.String(), fieldType)
}
field.SetInt(int64(v.Value.(int)))
case String:
if fieldKind != reflect.String {
return fmt.Errorf("Bind: Field %s's type should be %s but was %s", k, reflect.String.String(), fieldType)
}
field.SetString(v.Value.(string))
case DateTime:
timeType := dateTimeType
if fieldType != timeType {
return fmt.Errorf("Bind: Field %s's type should be %s but was %s", k, timeType.String(), fieldType)
}
field.Set(reflect.ValueOf(v.Value))
}
}
return nil
}
// Unbind is the opposite of Bind, taking a struct in and producing a list of attributes.
func Unbind(intf interface{}) Attributes {
structValue := reflect.ValueOf(intf)
if structValue.Kind() == reflect.Ptr {
structValue = structValue.Elem()
}
structType := structValue.Type()
attr := make(Attributes)
for i := 0; i < structValue.NumField(); i++ {
field := structValue.Field(i)
name := structType.Field(i).Name
if unicode.IsLower(rune(name[0])) {
continue // Unexported
}
switch field.Kind() {
case reflect.Struct:
if field.Type() == dateTimeType {
attr[name] = Attribute{
Type: DateTime,
Value: field.Interface(),
}
}
case reflect.Int:
attr[name] = Attribute{
Type: Integer,
Value: field.Interface(),
}
case reflect.String:
attr[name] = Attribute{
Type: String,
Value: field.Interface(),
}
}
}
return attr
}
/*type postgresStorer struct {
*sql.DB
}
type postgresUser struct {
// Anything Else
CustomAttribute string
// AuthBoss attributes.
Email string
}
func (p *postgresStorer) Put(key string, attr Attributes) error {
u := postgresUser{}
if err := attr.Bind(&u); err != nil {
panic("I should have written my user struct better!")
}
_, err := p.Exec("update users set email = $1 where id = $2", u.Email, key)
if err != nil {
return err
}
return nil
}
func (p *postgresStorer) Create(key string, attr Attributes) error {
u := postgresUser{
CustomAttribute: "DefaultValue",
}
if err := attr.Bind(&u); err != nil {
panic("I should have written my user struct better!")
}
_, err := p.Exec("insert into users (custom_attribute, email) values ($1)", u.CustomAttribute, u.Email)
if err != nil {
return err
}
return nil
}
func (p *postgresStorer) Get(key string, attrMeta AttributeMeta) (interface{}, error) {
row := p.QueryRow(`select * from users where key = $1`, key)
u := postgresUser{}
if err := row.Scan(&u.CustomAttribute, &u.Email); err != nil {
return nil, err
}
return u, nil
}*/