-
Notifications
You must be signed in to change notification settings - Fork 23
/
auth.go
487 lines (398 loc) · 9.44 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package goexpose
import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"encoding/json"
"net/url"
"github.com/nmcclain/ldap"
)
var (
ErrUnauthorized = errors.New("unauthorized")
ErrBlacklisted = errors.New("user is blacklisted")
ErrNotWhitelisted = errors.New("user is not whitelisted")
ErrBlacklistWhitelistProvided = errors.New("blacklist and whitelist set, that doesn't make sense.")
ErrUnknownNetwork = errors.New("unknown network")
ErrURLInvalidTemplate = errors.New("url is invalid template")
)
/*
Authorizer implements authorization
*/
type Authorizer interface {
Authorize(r *http.Request) error
}
func init() {
RegisterAuthorizer("basic", BasicAuthorizerFactory)
RegisterAuthorizer("ldap", LDAPAuthorizerFactory)
RegisterAuthorizer("http", HttpAuthorizerFactory)
}
/*
AuthFactory returns new authorizer
*/
type AuthorizerFactory func(config *AuthorizerConfig) (Authorizer, error)
var (
authorizers = map[string]AuthorizerFactory{}
authorizerslock = &sync.RWMutex{}
)
/*
Register authorizer
*/
func RegisterAuthorizer(id string, factory AuthorizerFactory) {
authorizerslock.Lock()
defer authorizerslock.Unlock()
if _, ok := authorizers[id]; ok {
panic(fmt.Sprintf("authorizer %s already registered", id))
}
authorizers[id] = factory
}
/*
AuthorizerExists returns if exists authorizer by given id
*/
func AuthorizerExists(id string) (ok bool) {
authorizerslock.RLock()
defer authorizerslock.RUnlock()
_, ok = authorizers[id]
return
}
/*
Returns authorizers for given config
First step is that it validates authorizers
*/
func GetAuthorizers(config *Config) (result Authorizers, err error) {
result = Authorizers{}
authorizerslock.RLock()
defer authorizerslock.RUnlock()
var authorizer Authorizer
for an, ac := range config.Authorizers {
// validate authorizer config
if err = ac.Validate(); err != nil {
return
}
var (
factory AuthorizerFactory
ok bool
)
if factory, ok = authorizers[ac.Type]; !ok {
err = fmt.Errorf("authorizer %s does not exist", ac.Type)
return
}
if authorizer, err = factory(ac); err != nil {
return
}
result[an] = authorizer
}
// check task authorizers
for i, ec := range config.Endpoints {
for _, tc := range ec.Methods {
for _, a := range tc.Authorizers {
if _, ok := result[a]; !ok {
err = fmt.Errorf("task %d, invalid authorizer `%s`.", i, a)
return
}
}
}
}
return
}
/*
Authorizers will have method that will check all authorizers
*/
type Authorizers map[string]Authorizer
/*
Try all authorizers, first that will fail with error, that error will be returned
*/
func (a Authorizers) Authorize(r *http.Request, config *EndpointConfig) (err error) {
check := []string{}
for _, an := range config.Authorizers {
check = append(check, an)
}
for _, an := range config.Methods[r.Method].Authorizers {
check = append(check, an)
}
for _, an := range check {
authorizer := a[an]
if err = authorizer.Authorize(r); err != nil {
return
}
}
return
}
/*
Returns names of all authorizerse
*/
func (a Authorizers) Names() []string {
result := make([]string, 0, len(a))
for k, _ := range a {
result = append(result, k)
}
return result
}
/*
Basic auth provides method GetBasicAuth from request headers
*/
type BasicAuthorizer struct {
config *BasicAuthorizerConfig
}
type BasicAuthorizerConfig struct {
Username string `json:"username"`
Password string `json:"password"`
}
func BasicAuthorizerFactory(ac *AuthorizerConfig) (result Authorizer, err error) {
config := &BasicAuthorizerConfig{}
if err = json.Unmarshal(ac.Config, config); err != nil {
return
}
result = &BasicAuthorizer{
config: config,
}
return
}
var (
ErrInvalidAuthorizationHeader = errors.New("invalid authorization header")
)
/*
Return username and password
*/
func (a *BasicAuthorizer) GetBasicAuth(r *http.Request) (username, password string, err error) {
header := r.Header.Get("Authorization")
splitted := strings.SplitN(header, " ", 2)
if len(splitted) != 2 {
err = ErrInvalidAuthorizationHeader
return
}
if splitted[0] != "Basic" {
err = ErrInvalidAuthorizationHeader
return
}
var data []byte
if data, err = base64.StdEncoding.DecodeString(splitted[1]); err != nil {
err = ErrInvalidAuthorizationHeader
return
}
up := strings.SplitN(string(data), ":", 2)
if len(up) != 2 {
err = ErrInvalidAuthorizationHeader
return
}
username, password = up[0], up[1]
return
}
/*
Check username and password
*/
func (b *BasicAuthorizer) Authorize(r *http.Request) (err error) {
var username, password string
if username, password, err = b.GetBasicAuth(r); err != nil {
return
}
if username != b.config.Username || password != b.config.Password {
return ErrUnauthorized
}
return
}
/*
LDAP authorizer.
This authorizer handles username/password authentication from LDAP server.
It also supports blacklist/whitelist of users that are allowed to access goexpose endpoint.
*/
const (
LDAP_DEFAULT_HOST = "localhost"
LDAP_DEFAULT_PORT = 389
LDAP_DEFAULT_NETWORK = "tcp"
)
type LDAPAuthorizerConfig struct {
Host string `json:"host"`
Port int `json:"port"`
Network string `json:"network"`
Whitelist []string `json:"whitelist"`
Blacklist []string `json:"blacklist"`
}
/*
Validate configuration
*/
func (l *LDAPAuthorizerConfig) Validate() (err error) {
if len(l.Whitelist) > 0 && len(l.Blacklist) > 0 {
return ErrBlacklistWhitelistProvided
}
found := false
for _, an := range []string{"tcp", "tls"} {
if an == l.Network {
found = true
}
}
if !found {
return ErrUnknownNetwork
}
return
}
func LDAPAuthorizerFactory(ac *AuthorizerConfig) (result Authorizer, err error) {
config := &LDAPAuthorizerConfig{
Host: LDAP_DEFAULT_HOST,
Port: LDAP_DEFAULT_PORT,
Network: LDAP_DEFAULT_NETWORK,
}
if err = json.Unmarshal(ac.Config, config); err != nil {
return
}
if err = config.Validate(); err != nil {
return
}
result = &LDAPAuthorizer{
config: config,
basic: &BasicAuthorizer{},
}
return
}
/*
LDAPAuthorizer
Main ldap authorizer implementation
*/
type LDAPAuthorizer struct {
config *LDAPAuthorizerConfig
basic *BasicAuthorizer
}
func (l *LDAPAuthorizer) Authorize(r *http.Request) (err error) {
var (
username string
password string
)
if username, password, err = l.basic.GetBasicAuth(r); err != nil {
return
}
var conn *ldap.Conn
// dial correct network
fullhost := fmt.Sprintf("%s:%d", l.config.Host, l.config.Port)
if l.config.Network == "tcp" {
conn, err = ldap.Dial("tcp", fullhost)
} else if l.config.Network == "tls" {
conn, err = ldap.DialTLS("tcp", fullhost, nil)
}
// check dial error
if err != nil {
return
}
// check blacklist
if len(l.config.Blacklist) > 0 {
for _, bl := range l.config.Blacklist {
if bl == username {
return ErrBlacklisted
}
}
}
// check whitelist
if len(l.config.Whitelist) > 0 {
found := false
for _, wl := range l.config.Whitelist {
if wl == username {
found = true
break
}
}
if !found {
return ErrNotWhitelisted
}
}
// check ldap for username/password
if err = conn.Bind(username, password); err != nil {
return err
}
return
}
/*
http authorizer
http authorizer basically makes http request to check username and password against web service.
*/
func HttpAuthorizerFactory(ac *AuthorizerConfig) (result Authorizer, err error) {
var (
config *HttpAuthorizerConfig
)
// get config
if config, err = NewHttpAuthorizerConfig(ac); err != nil {
return
}
ha := &HttpAuthorizer{
config: config,
}
result = ha
return
}
/*
HttpAuthorizer implementation
*/
type HttpAuthorizer struct {
config *HttpAuthorizerConfig
}
/*
Authorize main routine to allow user access
*/
func (h *HttpAuthorizer) Authorize(r *http.Request) (err error) {
// prepare data for template interpolation
data := map[string]interface{}{
"username": "phonkee",
"password": "password",
}
var (
url, method, body string
)
url, err = h.config.RenderURL(data)
method, err = h.config.RenderMethod(data)
body, err = h.config.RenderData(data)
var (
response *http.Response
)
// use Requester
if _, response, err = NewRequester().DoNew(method, url, strings.NewReader(body)); err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return ErrUnauthorized
}
return
}
/*
NewHttpAuthorizerConfig
Returns fresh copy of AuthorizerConfi
*/
func NewHttpAuthorizerConfig(ac *AuthorizerConfig) (hac *HttpAuthorizerConfig, err error) {
hac = &HttpAuthorizerConfig{
Method: "GET",
Data: "",
}
if err = json.Unmarshal(ac.Config, hac); err != nil {
return
}
var (
parsed *url.URL
)
if parsed, err = url.Parse(hac.URL); err != nil {
return
}
// update url
hac.URL = parsed.String()
// trim spaces
hac.URL = strings.TrimSpace(hac.URL)
hac.Data = strings.TrimSpace(hac.Data)
hac.Method = strings.TrimSpace(hac.Method)
return
}
/*
HttpAuthorizerConfig implementation
configuration for HttpAuthorizer
*/
type HttpAuthorizerConfig struct {
URL string `json:"url"`
Data string `json:"data"`
Method string `json:"method"`
}
func (h *HttpAuthorizerConfig) RenderURL(data map[string]interface{}) (result string, err error) {
return Interpolate(h.URL, data)
}
func (h *HttpAuthorizerConfig) RenderData(data map[string]interface{}) (result string, err error) {
return Interpolate(h.Data, data)
}
func (h *HttpAuthorizerConfig) RenderMethod(data map[string]interface{}) (result string, err error) {
return Interpolate(h.Method, data)
}