This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.go
93 lines (82 loc) · 3.08 KB
/
middleware.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
package main
import (
"crypto/subtle"
"fmt"
"net/http"
"time"
)
// middleware handles request logging
func middleware(handler http.HandlerFunc) http.HandlerFunc {
// no-auth middware func
return func(w http.ResponseWriter, r *http.Request) {
// poor man's logging:
log.Infoln(r.Method, r.URL.Path, time.Now())
// If this server is operating behind a proxy, but we still want to force
// users to use https, cfg.ProxyForceHttps == true will listen for the common
// X-Forward-Proto & redirect to https
if cfg.ProxyForceHttps {
if r.Header.Get("X-Forwarded-Proto") == "http" {
w.Header().Set("Connection", "close")
url := "https://" + r.Host + r.URL.String()
http.Redirect(w, r, url, http.StatusMovedPermanently)
return
}
}
addCORSHeaders(w, r)
// TODO - Strict Transport config?
// if cfg.TLS {
// // If TLS is enabled, set 1 week strict TLS, 1 week for now to prevent catastrophic mess-ups
// w.Header().Add("Strict-Transport-Security", "max-age=604800")
// }
handler(w, r)
}
}
// authMiddleware adds http basic auth if configured
func authMiddleware(handler http.HandlerFunc) http.HandlerFunc {
// return auth middleware if configuration settings are present
if cfg.HttpAuthUsername != "" && cfg.HttpAuthPassword != "" {
return func(w http.ResponseWriter, r *http.Request) {
// poor man's logging:
fmt.Println(r.Method, r.URL.Path, time.Now())
// If this server is operating behind a proxy, but we still want to force
// users to use https, cfg.ProxyForceHttps == true will listen for the common
// X-Forward-Proto & redirect to https
if cfg.ProxyForceHttps {
if r.Header.Get("X-Forwarded-Proto") == "http" {
w.Header().Set("Connection", "close")
url := "https://" + r.Host + r.URL.String()
http.Redirect(w, r, url, http.StatusMovedPermanently)
return
}
}
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(cfg.HttpAuthUsername)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(cfg.HttpAuthPassword)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="Please enter your username and password for this site"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("access denied \n"))
return
}
// TODO - Strict Transport config?
// if cfg.TLS {
// // If TLS is enabled, set 1 week strict TLS, 1 week for now to prevent catastrophic mess-ups
// w.Header().Add("Strict-Transport-Security", "max-age=604800")
// }
handler(w, r)
}
}
// no-auth middware func
return middleware(handler)
}
// addCORSHeaders adds CORS header info for whitelisted servers
func addCORSHeaders(w http.ResponseWriter, r *http.Request) {
// origin := r.Header.Get("Origin")
// for _, o := range cfg.AllowedOrigins {
// if origin == o {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Authorization")
w.Header().Set("Access-Control-Allow-Credentials", "true")
// return
// }
// }
}