-
Notifications
You must be signed in to change notification settings - Fork 21
/
auth.go
60 lines (55 loc) · 1.55 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 auth implements http request authentication
package auth
import (
"errors"
"github.com/MG-RAST/AWE/lib/auth/clientgroup"
"github.com/MG-RAST/AWE/lib/auth/globus"
"github.com/MG-RAST/AWE/lib/auth/oauth"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core"
e "github.com/MG-RAST/AWE/lib/errors"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/user"
)
// authCache is a
var authCache cache
var authMethods []func(string) (*user.User, error)
func Initialize() {
authCache = cache{m: make(map[string]cacheValue)}
authMethods = []func(string) (*user.User, error){}
if len(conf.AUTH_OAUTH) > 0 {
authMethods = append(authMethods, oauth.Auth)
}
if conf.GLOBUS_TOKEN_URL != "" && conf.GLOBUS_PROFILE_URL != "" {
authMethods = append(authMethods, globus.Auth)
}
}
func Authenticate(header string) (u *user.User, err error) {
if u = authCache.lookup(header); u != nil {
return u, nil
} else {
for _, auth := range authMethods {
u, err = auth(header)
if err != nil {
// log actual error, return consistant invalid auth to user
last_position := len(header)
if last_position > 10 {
last_position = 10
}
logger.Error("(auth.Authenticate) err=%s (header=%s)", err.Error(), header[0:last_position]+"...")
err = nil
}
if u != nil {
authCache.add(header, u)
return
}
}
}
return nil, errors.New(e.InvalidAuth)
}
func AuthenticateClientGroup(header string) (cg *core.ClientGroup, err error) {
if cg, err = clientgroup.Auth(header); err != nil {
return nil, err
}
return cg, nil
}