-
Notifications
You must be signed in to change notification settings - Fork 343
/
auth.go
189 lines (161 loc) · 4.36 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
package auth
import (
"errors"
"fmt"
"net/http"
"strings"
"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
checkSecureOAuthTokenintrospectionAnyClaims
checkSecureOAuthTokenintrospectionAllClaims
checkSecureOAuthTokenintrospectionAnyKV
checkSecureOAuthTokenintrospectionAllKV
checkOIDCUserInfo
checkOIDCAnyClaims
checkOIDCAllClaims
checkOIDCQueryClaims
)
type rejectReason string
const (
missingBearerToken rejectReason = "missing-bearer-token"
missingToken rejectReason = "missing-token"
missingJWKS rejectReason = "missing-jwks"
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 "
// tokenKey defined at https://tools.ietf.org/html/rfc7662#section-2.1
tokenKey = "token"
scopeKey = "scope"
uidKey = "uid"
)
type kv map[string][]string
type requestError struct {
err error
}
var (
errUnsupportedClaimSpecified = errors.New("unsupported claim specified in filter")
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, strings.Join(v, " "))
}
return strings.Join(res, ",")
}
func (err *requestError) Error() string {
return err.err.Error()
}
func requestErrorf(f string, args ...interface{}) error {
return &requestError{
err: fmt.Errorf(f, args...),
}
}
func getToken(r *http.Request) (string, bool) {
h := r.Header.Get(authHeaderName)
if !strings.HasPrefix(h, authHeaderPrefix) {
return "", false
}
return h[len(authHeaderPrefix):], true
}
func reject(
ctx filters.FilterContext,
status int,
username string,
reason rejectReason,
hostname,
debuginfo string,
) {
if debuginfo == "" {
ctx.Logger().Debugf(
"Rejected: status: %d, username: %s, reason: %s.",
status, username, reason,
)
} else {
ctx.Logger().Debugf(
"Rejected: status: %d, username: %s, reason: %s, info: %s.",
status, username, reason, debuginfo,
)
}
ctx.StateBag()[logfilter.AuthUserKey] = username
ctx.StateBag()[logfilter.AuthRejectReasonKey] = string(reason)
rsp := &http.Response{
StatusCode: status,
Header: make(map[string][]string),
}
if hostname != "" {
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2
rsp.Header.Add("WWW-Authenticate", hostname)
}
ctx.Serve(rsp)
}
func unauthorized(ctx filters.FilterContext, username string, reason rejectReason, hostname, debuginfo string) {
reject(ctx, http.StatusUnauthorized, username, reason, hostname, debuginfo)
}
func forbidden(ctx filters.FilterContext, username string, reason rejectReason, debuginfo string) {
reject(ctx, http.StatusForbidden, username, reason, "", debuginfo)
}
func authorized(ctx filters.FilterContext, username string) {
ctx.StateBag()[logfilter.AuthUserKey] = username
}
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
}