-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthorize.go
229 lines (201 loc) · 5.53 KB
/
authorize.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
package secure
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
// AuthorizationLevel indicates the access level for given API
type AuthorizationLevel int
const (
NOAUTH AuthorizationLevel = iota
READ
READWRITE
ADMIN
)
// LevelFromString finds the level corresponding
func LevelFromString(val string) (AuthorizationLevel, error) {
switch val {
case "readonly":
return READ, nil
case "readwrite":
return READWRITE, nil
case "admin":
return ADMIN, nil
case "noauth":
return NOAUTH, nil
default:
return 0, fmt.Errorf("invalid authorization string")
}
}
// LevelFromString finds the level corresponding
func StringFromLevel(level AuthorizationLevel) (string, error) {
switch level {
case READ:
return "readonly", nil
case READWRITE:
return "readwrite", nil
case ADMIN:
return "admin", nil
case NOAUTH:
return "noauth", nil
default:
return "", fmt.Errorf("invalid authorization string")
}
}
type Authorizer interface {
// Authorize determines whether the user (email address) has the specified authorization level
Authorize(user string, level AuthorizationLevel) bool
// Level returns the authorization level for the user
Level(user string) (AuthorizationLevel, error)
}
// DatastoreAuthorize implements Authorizer using the appdata-store cloud function http interface.
type DatastoreAuthorize struct {
httpAddr string
token string
userPermissions map[string]string
}
// NewFileAuthorizer creates a FileAuthorize object
func NewDatastoreAuthorizer(httpAddr string, token string) (DatastoreAuthorize, error) {
authList, err := loadDatastoreUsers(httpAddr, token)
if err != nil {
return DatastoreAuthorize{}, err
}
return DatastoreAuthorize{httpAddr, token, authList}, nil
}
func loadDatastoreUsers(httpAddr, token string) (map[string]string, error) {
datastoreClient := http.Client{
Timeout: time.Second * 60,
}
req, err := http.NewRequest(http.MethodGet, httpAddr+"/users", nil)
if err != nil {
return nil, fmt.Errorf("request failed")
}
req.Header.Set("Authorization", "Bearer "+token)
res, err := datastoreClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed")
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("request failed")
}
var authlist map[string]string
jsonErr := json.Unmarshal(body, &authlist)
if jsonErr != nil {
return nil, fmt.Errorf("could not retrieve authorization information")
}
return authlist, nil
}
func (a DatastoreAuthorize) Authorize(user string, level AuthorizationLevel) bool {
// if the authorization level is disabled, return true
if level == NOAUTH {
return true
}
perm, ok := a.userPermissions[user]
found := ok
if !ok {
// re-check file in case user was recently added
if authList2, err := loadDatastoreUsers(a.httpAddr, a.token); err == nil {
a.userPermissions = authList2
perm, found = a.userPermissions[user]
}
}
if found {
// authorization level of user satisfies requeted level
if templevel, err := LevelFromString(perm); templevel >= level && err == nil {
return true
}
}
return false
}
func (a DatastoreAuthorize) Level(user string) (AuthorizationLevel, error) {
perm, ok := a.userPermissions[user]
found := ok
if !ok {
// re-check file in case user was recently added
if authList2, err := loadDatastoreUsers(a.httpAddr, a.token); err == nil {
a.userPermissions = authList2
perm, found = a.userPermissions[user]
}
}
if found {
// authorization level of user satisfies requeted level
if level1, err := LevelFromString(perm); err == nil {
return level1, nil
} else {
return 0, err
}
}
return NOAUTH, nil
}
// FileAuthorize implements Authorizer using a JSON file for authorization
// that is dictionary of emails with values "readonly", "readwrite", "admin".
type FileAuthorize struct {
fileName string
userPermissions map[string]string
}
func loadUsers(authFile string) (map[string]string, error) {
fin, err := os.Open(authFile)
defer fin.Close()
if err != nil {
err = fmt.Errorf("%s cannot be read", authFile)
return nil, err
}
byteData, _ := ioutil.ReadAll(fin)
var authlist map[string]string
json.Unmarshal(byteData, &authlist)
return authlist, nil
}
// NewFileAuthorizer creates a FileAuthorize object
func NewFileAuthorizer(authFile string) (FileAuthorize, error) {
authList, err := loadUsers(authFile)
if err != nil {
return FileAuthorize{}, err
}
return FileAuthorize{authFile, authList}, nil
}
func (a FileAuthorize) Authorize(user string, level AuthorizationLevel) bool {
// if the authorization level is disabled, return true
if level == NOAUTH {
return true
}
perm, ok := a.userPermissions[user]
found := ok
if !ok {
// re-check file in case user was recently added
if authList2, err := loadUsers(a.fileName); err == nil {
a.userPermissions = authList2
perm, found = a.userPermissions[user]
}
}
if found {
// authorization level of user satisfies requeted level
if templevel, err := LevelFromString(perm); templevel >= level && err == nil {
return true
}
}
return false
}
func (a FileAuthorize) Level(user string) (AuthorizationLevel, error) {
perm, ok := a.userPermissions[user]
found := ok
if !ok {
// re-check file in case user was recently added
if authList2, err := loadUsers(a.fileName); err == nil {
a.userPermissions = authList2
perm, found = a.userPermissions[user]
}
}
if found {
// authorization level of user satisfies requeted level
if level1, err := LevelFromString(perm); err == nil {
return level1, nil
} else {
return 0, err
}
}
return NOAUTH, nil
}