-
Notifications
You must be signed in to change notification settings - Fork 4
/
oauthserver.go
385 lines (319 loc) · 8.75 KB
/
oauthserver.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
package main
import (
"log"
"github.com/tappleby/slack_auth_proxy/slack"
"net/http"
"fmt"
"net/http/httputil"
"strings"
"github.com/gorilla/securecookie"
"time"
"html/template"
"encoding/base64"
)
const (
signInPath = "/oauth2/sign_in"
oauthStartPath = "/oauth2/start"
oauthCallbackPath = "/oauth2/callback"
staticDir = "/_slackproxy"
)
var (
oauthTemplates = template.Must(template.ParseGlob("templates/*.html"))
)
type OAuthServer struct {
CookieKey string
Validator func(*slack.Auth, *UpstreamConfiguration) bool
HtpasswdFile* HtpasswdFile
slackOauth *slack.OAuthClient
serveMux *http.ServeMux
staticHandler http.Handler
secureCookie *securecookie.SecureCookie
upstreamsConfig UpstreamConfigurationMap
config *Configuration
}
func NewOauthServer(slackOauth *slack.OAuthClient, config *Configuration) *OAuthServer {
serveMux := http.NewServeMux()
upstreamsPathMap := make(UpstreamConfigurationMap)
for _, upstream := range config.Upstreams {
u := upstream.HostURL
path := u.Path
u.Path = ""
if path == "" {
path = "/"
}
log.Printf("mapping %s => %s", path, u)
serveMux.Handle(path, httputil.NewSingleHostReverseProxy(u))
upstreamsPathMap[path] = upstream
}
decode64 := func(name, s string) []byte {
dec := base64.StdEncoding
bs, err := dec.DecodeString(s)
if err != nil {
log.Fatalf("Could not decode %s key: %s", name, err)
}
return bs
}
hashKey := decode64("cookie_hash_key", config.CookieHashKey)
blockKey := decode64("cookie_block_key", config.CookieBlockKey)
secureCookie := securecookie.New(hashKey, blockKey)
return &OAuthServer{
CookieKey: "_slackauthproxy",
Validator: NewValidator(),
serveMux: serveMux,
slackOauth: slackOauth,
upstreamsConfig: upstreamsPathMap,
secureCookie: secureCookie,
staticHandler: http.FileServer(http.Dir("static")),
config: config,
}
}
func (s *OAuthServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
var ok bool
var user string
// check if this is a redirect back at the end of oauth
if s.config.Debug {
remoteIP := req.Header.Get("X-Real-IP")
if remoteIP == "" {
remoteIP = req.RemoteAddr
}
log.Printf("%s %s %s", remoteIP, req.Method, req.URL.Path)
}
reqPath := req.URL.Path
if reqPath == signInPath {
redirect, err := s.GetRedirect(req)
if err != nil {
s.ErrorPage(rw, 500, "Internal Error", err.Error())
return
}
user, ok = s.ManualSignIn(rw, req)
if ok {
auth := &slack.Auth{
Username: user,
}
encoded, err := s.secureCookie.Encode(s.CookieKey, auth)
if err != nil {
log.Printf("Error encoding cookie %s", err.Error())
s.ErrorPage(rw, 500, "Internal Error", "Error encoding auth cookie")
} else {
s.SetCookie(rw, req, encoded)
http.Redirect(rw, req, redirect, 302)
}
} else {
s.handleSignIn(rw, req)
}
return
} else if reqPath == oauthStartPath {
s.handleOAuthStart(rw, req)
return
} else if (reqPath == oauthCallbackPath) {
s.handleOAuthCallback(rw, req)
return
} else if (strings.HasPrefix(reqPath, staticDir)) {
req.URL.Path = strings.Replace(reqPath, staticDir, "", 1)
s.staticHandler.ServeHTTP(rw, req);
return;
}
handler, pattern := s.serveMux.Handler(req)
upstreamConfig := s.upstreamsConfig.Find(pattern)
if upstreamConfig == nil {
http.NotFound(rw, req)
return
}
if !ok {
cookie, _ := req.Cookie(s.CookieKey)
if cookie != nil {
auth := new(slack.Auth)
s.secureCookie.Decode(s.CookieKey, cookie.Value, &auth);
ok = s.Validator(auth, upstreamConfig)
user = auth.Username
}
}
if !ok {
user, basicOk := s.CheckBasicAuth(req)
if basicOk {
auth := &slack.Auth{
Username: user,
}
ok = s.Validator(auth, upstreamConfig)
}
}
if !ok {
log.Printf("invalid cookie")
s.handleSignIn(rw, req)
return
}
if s.config.PassBasicAuth {
req.SetBasicAuth(user, "")
req.Header["X-Forwarded-User"] = []string{user}
}
handler.ServeHTTP(rw, req)
}
func (s *OAuthServer) GetRedirect(req *http.Request) (string, error) {
err := req.ParseForm()
if err != nil {
return "", err
}
redirect := req.FormValue("rd")
if redirect == "" || redirect == signInPath {
redirect = "/"
}
return redirect, err
}
func (s *OAuthServer) handleSignIn(rw http.ResponseWriter, req *http.Request) {
s.ClearCookie(rw, req)
t := struct {
Title string
SignInUrl string
Redirect string
HtPasswd bool
BasicRequested bool
}{
Title: "Sign in",
SignInUrl: signInPath,
Redirect: req.URL.RequestURI(),
HtPasswd: s.HtpasswdFile != nil,
BasicRequested: req.FormValue("basic") == "1",
}
s.renderTemplate(rw, "sign_in", t)
}
func (s *OAuthServer) handleOAuthStart(rw http.ResponseWriter, req *http.Request) {
redirect, err := s.GetRedirect(req)
if err != nil {
s.ErrorPage(rw, 500, "Internal Error", err.Error())
return
}
http.Redirect(rw, req, s.slackOauth.LoginUrl(redirect).String(), 302)
}
func (s *OAuthServer) handleOAuthCallback(rw http.ResponseWriter, req *http.Request) {
// finish the oauth cycle
err := req.ParseForm()
if err != nil {
s.ErrorPage(rw, 500, "Internal Error", err.Error())
return
}
errorString := req.Form.Get("error")
if errorString != "" {
s.ErrorPage(rw, 403, "Permission Denied", errorString)
return
}
access, err := s.slackOauth.RedeemCode(req.Form.Get("code"))
if err != nil {
log.Printf("error redeeming code %s", err.Error())
s.ErrorPage(rw, 500, "Internal Error", err.Error())
return
}
cl := slack.NewClient(access.Token)
auth, err := cl.Auth.Test()
if err != nil {
log.Printf("error redeeming code %s", err.Error())
s.ErrorPage(rw, 500, "Internal Error", err.Error())
return
}
encoded, err := s.secureCookie.Encode(s.CookieKey, auth)
if err != nil {
log.Printf("Error encoding cookie %s", err.Error())
s.ErrorPage(rw, 500, "Internal Error", "Error encoding auth cookie")
}
redirect := req.Form.Get("state")
if redirect == "" {
redirect = "/"
}
upstreamConfig := s.upstreamsConfig.Find(redirect)
if upstreamConfig == nil {
s.ErrorPage(rw, 500, "Internal Error", fmt.Sprintf("Could not find upstream config for %s", redirect))
return
}
if s.Validator(auth, upstreamConfig) {
log.Printf("authenticating %s completed", auth.Username)
s.SetCookie(rw, req, encoded)
http.Redirect(rw, req, redirect, 302)
} else {
s.ErrorPage(rw, 403, "Permission Denied", "Invalid Account")
}
}
func (s *OAuthServer) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
log.Printf("ErrorPage %d %s %s", code, title, message)
rw.WriteHeader(code)
t := struct {
Title string
Message string
}{
Title: fmt.Sprintf("%d %s", code, title),
Message: message,
}
s.renderTemplate(rw, "error", t)
}
func (s *OAuthServer) SetCookie(rw http.ResponseWriter, req *http.Request, val string) {
cookie := &http.Cookie{
Name: s.CookieKey,
Value: val,
Path: "/",
Domain: s.getCookieDomain(req),
Expires: time.Now().Add(time.Duration(168) * time.Hour), // 7 days
HttpOnly: true,
// Secure: req. ... ? set if X-Scheme: https ?
}
http.SetCookie(rw, cookie)
}
func (s *OAuthServer) ClearCookie(rw http.ResponseWriter, req *http.Request) {
cookie := &http.Cookie{
Name: s.CookieKey,
Value: "",
Path: "/",
Domain: s.getCookieDomain(req),
Expires: time.Now().Add(time.Duration(1) * time.Hour * -1),
HttpOnly: true,
}
http.SetCookie(rw, cookie)
}
func (s *OAuthServer) CheckBasicAuth(req *http.Request) (string, bool) {
if s.HtpasswdFile == nil {
return "", false
}
str := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
if len(str) != 2 || str[0] != "Basic" {
return "", false
}
b, err := base64.StdEncoding.DecodeString(str[1])
if err != nil {
return "", false
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return "", false
}
if s.HtpasswdFile.Validate(pair[0], pair[1]) {
log.Printf("authenticated %s via basic auth", pair[0])
return pair[0], true
}
return "", false
}
func (s *OAuthServer) ManualSignIn(rw http.ResponseWriter, req *http.Request) (string, bool) {
if req.Method != "POST" || s.HtpasswdFile == nil {
return "", false
}
user := req.FormValue("username")
passwd := req.FormValue("password")
if user == "" {
return "", false
}
// check auth
if s.HtpasswdFile.Validate(user, passwd) {
log.Printf("authenticated %s via manual sign in", user)
return user, true
}
return "", false
}
func (s *OAuthServer) renderTemplate(rw http.ResponseWriter, tmpl string, data interface {}) {
err := oauthTemplates.ExecuteTemplate(rw, tmpl+".html", data)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
func (s *OAuthServer) getCookieDomain(req *http.Request) string {
domain := strings.Split(req.Host, ":")[0]
if s.config.CookieDomain != "" && strings.HasSuffix(domain, s.config.CookieDomain) {
domain = s.config.CookieDomain
}
return domain
}