-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
190 lines (179 loc) · 4.46 KB
/
middlewares.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
package apiserver
import (
"net/http"
"regexp"
"strings"
"github.com/wrgl/core/pkg/auth"
)
type routeScope struct {
Pat *regexp.Regexp
Method string
Scope string
}
var routeScopes []routeScope
func init() {
patAuthenticate = regexp.MustCompile(`^/authenticate/`)
patConfig = regexp.MustCompile(`^/config/`)
patRefs = regexp.MustCompile(`^/refs/`)
patHead = regexp.MustCompile(`^heads/[-_0-9a-zA-Z]+/`)
patRefsHead = regexp.MustCompile(`^/refs/heads/[-_0-9a-zA-Z]+/`)
patUploadPack = regexp.MustCompile(`^/upload-pack/`)
patReceivePack = regexp.MustCompile(`^/receive-pack/`)
patCommits = regexp.MustCompile(`^/commits/`)
patSum = regexp.MustCompile(`^[0-9a-f]{32}/`)
patCommit = regexp.MustCompile(`^/commits/[0-9a-f]{32}/`)
patTables = regexp.MustCompile(`^/tables/`)
patTable = regexp.MustCompile(`^/tables/[0-9a-f]{32}/`)
patBlocks = regexp.MustCompile(`^blocks/`)
patTableBlocks = regexp.MustCompile(`^/tables/[0-9a-f]{32}/blocks/`)
patRows = regexp.MustCompile(`^rows/`)
patTableRows = regexp.MustCompile(`^/tables/[0-9a-f]{32}/rows/`)
patDiff = regexp.MustCompile(`^/diff/[0-9a-f]{32}/[0-9a-f]{32}/`)
routeScopes = []routeScope{
{
Pat: patAuthenticate,
Method: http.MethodPost,
},
{
Pat: patConfig,
Method: http.MethodGet,
Scope: auth.ScopeReadConfig,
},
{
Pat: patConfig,
Method: http.MethodPut,
Scope: auth.ScopeWriteConfig,
},
{
Pat: patRefsHead,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
{
Pat: patRefs,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
{
Pat: patUploadPack,
Method: http.MethodPost,
Scope: auth.ScopeRead,
},
{
Pat: patReceivePack,
Method: http.MethodPost,
Scope: auth.ScopeWrite,
},
{
Pat: patCommit,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
{
Pat: patCommits,
Method: http.MethodPost,
Scope: auth.ScopeWrite,
},
{
Pat: patCommits,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
{
Pat: patTableBlocks,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
{
Pat: patTableRows,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
{
Pat: patTable,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
{
Pat: patDiff,
Method: http.MethodGet,
Scope: auth.ScopeRead,
},
}
}
type authenticateMiddleware struct {
handler http.Handler
getAuthnS func(r *http.Request) auth.AuthnStore
}
func AuthenticateMiddleware(getAuthnS func(r *http.Request) auth.AuthnStore) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return &authenticateMiddleware{
handler: handler,
getAuthnS: getAuthnS,
}
}
}
func (m *authenticateMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if h := r.Header.Get("Authorization"); h != "" && strings.HasPrefix(h, "Bearer ") {
authnS := m.getAuthnS(r)
var claims *auth.Claims
var err error
r, claims, err = authnS.CheckToken(r, h[7:])
if err != nil {
if strings.HasPrefix(err.Error(), "unexpected signing method: ") || err.Error() == "invalid token" {
http.Error(rw, "invalid token", http.StatusUnauthorized)
return
}
panic(err)
}
r = setEmail(r, claims.Email)
}
m.handler.ServeHTTP(rw, r)
}
type authorizeMiddleware struct {
handler http.Handler
getAuthzS func(r *http.Request) auth.AuthzStore
pathPrefix *regexp.Regexp
}
func AuthorizeMiddleware(getAuthzS func(r *http.Request) auth.AuthzStore, pathPrefix *regexp.Regexp) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
m := &authorizeMiddleware{
handler: handler,
getAuthzS: getAuthzS,
pathPrefix: pathPrefix,
}
return m
}
}
func (m *authorizeMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
var route *routeScope
p := r.URL.Path
if m.pathPrefix != nil {
p = strings.TrimPrefix(p, m.pathPrefix.FindString(p))
}
for _, o := range routeScopes {
if o.Pat.MatchString(p) && o.Method == r.Method {
route = &o
break
}
}
if route == nil {
http.Error(rw, "not found", http.StatusNotFound)
return
}
if route.Scope != "" {
authzS := m.getAuthzS(r)
email := getEmail(r)
if ok, err := authzS.Authorized(r, email, route.Scope); err != nil {
panic(err)
} else if !ok {
if email == "" {
http.Error(rw, "unauthorized", http.StatusUnauthorized)
} else {
http.Error(rw, "forbidden", http.StatusForbidden)
}
return
}
}
m.handler.ServeHTTP(rw, r)
}