forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
137 lines (118 loc) · 3.37 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
package auth
import (
"errors"
"net/http"
"strings"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters"
logfilter "github.com/zalando/skipper/filters/log"
)
type roleCheckType int
const (
checkOAuthTokeninfoAnyScopes roleCheckType = iota
checkOAuthTokeninfoAllScopes
checkOAuthTokeninfoAnyKV
checkOAuthTokeninfoAllKV
checkOAuthTokenintrospectionAnyClaims
checkOAuthTokenintrospectionAllClaims
checkOAuthTokenintrospectionAnyKV
checkOAuthTokenintrospectionAllKV
checkUnknown
)
type rejectReason string
const (
missingBearerToken rejectReason = "missing-bearer-token"
missingToken rejectReason = "missing-token"
authServiceAccess rejectReason = "auth-service-access"
invalidSub rejectReason = "invalid-sub-in-token"
inactiveToken rejectReason = "inactive-token"
invalidToken rejectReason = "invalid-token"
invalidScope rejectReason = "invalid-scope"
invalidClaim rejectReason = "invalid-claim"
invalidFilter rejectReason = "invalid-filter"
invalidAccess rejectReason = "invalid-access"
)
const (
AuthUnknown = "authUnknown"
authHeaderName = "Authorization"
authHeaderPrefix = "Bearer "
// accessTokenKey defined at https://tools.ietf.org/html/rfc7662#section-2.1
accessTokenKey = "access_token"
scopeKey = "scope"
uidKey = "uid"
)
type kv map[string]string
var (
errUnsupportedClaimSpecified = errors.New("unsupported claim specified in filter")
errInvalidAuthorizationHeader = errors.New("invalid authorization header")
errInvalidToken = errors.New("invalid token")
errInvalidTokenintrospectionData = errors.New("invalid tokenintrospection data")
)
func (kv kv) String() string {
var res []string
for k, v := range kv {
res = append(res, k, v)
}
return strings.Join(res, ",")
}
func getToken(r *http.Request) (string, error) {
h := r.Header.Get(authHeaderName)
if !strings.HasPrefix(h, authHeaderPrefix) {
return "", errInvalidAuthorizationHeader
}
return h[len(authHeaderPrefix):], nil
}
func unauthorized(ctx filters.FilterContext, uname string, reason rejectReason, hostname string) {
log.Debugf("uname: %s, reason: %s", uname, reason)
ctx.StateBag()[logfilter.AuthUserKey] = uname
ctx.StateBag()[logfilter.AuthRejectReasonKey] = string(reason)
rsp := &http.Response{
StatusCode: http.StatusUnauthorized,
Header: make(map[string][]string),
}
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2
rsp.Header.Add("WWW-Authenticate", hostname)
ctx.Serve(rsp)
}
func authorized(ctx filters.FilterContext, uname string) {
ctx.StateBag()[logfilter.AuthUserKey] = uname
}
func getStrings(args []interface{}) ([]string, error) {
s := make([]string, len(args))
var ok bool
for i, a := range args {
s[i], ok = a.(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
}
return s, nil
}
// all checks that all strings in the left are also in the
// right. Right can be a superset of left.
func all(left, right []string) bool {
for _, l := range left {
var found bool
for _, r := range right {
if l == r {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// intersect checks that one string in the left is also in the right
func intersect(left, right []string) bool {
for _, l := range left {
for _, r := range right {
if l == r {
return true
}
}
}
return false
}