@@ -20,12 +20,16 @@ import (
23+ // SignatureHeader is the name of the request header containing the GAP Signature
23 SignatureHeader = "GAP-Signature"
25 SignatureHeader = "GAP-Signature"
25 httpScheme = "http"
27 httpScheme = "http"
26 httpsScheme = "https"
28 httpsScheme = "https"
31+// SignatureHeaders contains the headers to be signed by the hmac algorithm
29var SignatureHeaders = []string{
33var SignatureHeaders = []string{
30 "Content-Length",
34 "Content-Length",
31 "Content-Md5",
35 "Content-Md5",
@@ -39,6 +43,7 @@ var SignatureHeaders = []string{
39 "Gap-Auth",
43 "Gap-Auth",
46+// OAuthProxy is the main authentication proxy
42type OAuthProxy struct {
47type OAuthProxy struct {
43 CookieSeed string
48 CookieSeed string
44 CookieName string
49 CookieName string
@@ -79,12 +84,15 @@ type OAuthProxy struct {
79 Footer string
84 Footer string
87+// UpstreamProxy represents an upstream server to proxy to
82type UpstreamProxy struct {
88type UpstreamProxy struct {
83 upstream string
89 upstream string
84 handler http.Handler
90 handler http.Handler
85 auth hmacauth.HmacAuth
91 auth hmacauth.HmacAuth
94+// ServeHTTP proxies requests to the upstream provider while signing the
88func (u *UpstreamProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
96func (u *UpstreamProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
89 w.Header().Set("GAP-Upstream-Address", u.upstream)
97 w.Header().Set("GAP-Upstream-Address", u.upstream)
90 if u.auth != nil {
98 if u.auth != nil {
@@ -94,9 +102,12 @@ func (u *UpstreamProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
94 u.handler.ServeHTTP(w, r)
102 u.handler.ServeHTTP(w, r)
105+// NewReverseProxy creates a new reverse proxy for proxying requests to upstream
97func NewReverseProxy(target *url.URL) (proxy *httputil.ReverseProxy) {
107func NewReverseProxy(target *url.URL) (proxy *httputil.ReverseProxy) {
98 return httputil.NewSingleHostReverseProxy(target)
108 return httputil.NewSingleHostReverseProxy(target)
100func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
111func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
101 director := proxy.Director
112 director := proxy.Director
102 proxy.Director = func(req *http.Request) {
113 proxy.Director = func(req *http.Request) {
@@ -107,6 +118,7 @@ func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
107 req.URL.RawQuery = ""
118 req.URL.RawQuery = ""
110func setProxyDirector(proxy *httputil.ReverseProxy) {
122func setProxyDirector(proxy *httputil.ReverseProxy) {
111 director := proxy.Director
123 director := proxy.Director
112 proxy.Director = func(req *http.Request) {
124 proxy.Director = func(req *http.Request) {
@@ -116,10 +128,13 @@ func setProxyDirector(proxy *httputil.ReverseProxy) {
116 req.URL.RawQuery = ""
128 req.URL.RawQuery = ""
132+// NewFileServer creates a http.Handler to serve files from the filesystem
119func NewFileServer(path string, filesystemPath string) (proxy http.Handler) {
133func NewFileServer(path string, filesystemPath string) (proxy http.Handler) {
120 return http.StripPrefix(path, http.FileServer(http.Dir(filesystemPath)))
134 return http.StripPrefix(path, http.FileServer(http.Dir(filesystemPath)))
137+// NewOAuthProxy creates a new instance of OOuthProxy from the options provided
123func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
138func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
124 serveMux := http.NewServeMux()
139 serveMux := http.NewServeMux()
125 var auth hmacauth.HmacAuth
140 var auth hmacauth.HmacAuth
@@ -214,6 +229,8 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
232+// GetRedirectURI returns the redirectURL that the upstream OAuth Provider will
233+// redirect clients to once authenticated
217func (p *OAuthProxy) GetRedirectURI(host string) string {
234func (p *OAuthProxy) GetRedirectURI(host string) string {
218 // default to the request Host if not set
235 // default to the request Host if not set
219 if p.redirectURL.Host != "" {
236 if p.redirectURL.Host != "" {
@@ -259,6 +276,8 @@ func (p *OAuthProxy) redeemCode(host, code string) (s *providers.SessionState, e
279+// MakeSessionCookie creates an http.Cookie containing the authenticated user's
280+// authentication details
262func (p *OAuthProxy) MakeSessionCookie(req *http.Request, value string, expiration time.Duration, now time.Time) *http.Cookie {
281func (p *OAuthProxy) MakeSessionCookie(req *http.Request, value string, expiration time.Duration, now time.Time) *http.Cookie {
263 if value != "" {
282 if value != "" {
264 value = cookie.SignedValue(p.CookieSeed, p.CookieName, value, now)
283 value = cookie.SignedValue(p.CookieSeed, p.CookieName, value, now)
@@ -270,6 +289,7 @@ func (p *OAuthProxy) MakeSessionCookie(req *http.Request, value string, expirati
270 return p.makeCookie(req, p.CookieName, value, expiration, now)
289 return p.makeCookie(req, p.CookieName, value, expiration, now)
292+// MakeCSRFCookie creates a cookie for CSRF
273func (p *OAuthProxy) MakeCSRFCookie(req *http.Request, value string, expiration time.Duration, now time.Time) *http.Cookie {
293func (p *OAuthProxy) MakeCSRFCookie(req *http.Request, value string, expiration time.Duration, now time.Time) *http.Cookie {
274 return p.makeCookie(req, p.CSRFCookieName, value, expiration, now)
294 return p.makeCookie(req, p.CSRFCookieName, value, expiration, now)
@@ -296,14 +316,19 @@ func (p *OAuthProxy) makeCookie(req *http.Request, name string, value string, ex
319+// ClearCSRFCookie creates a cookie to unset the CSRF cookie stored in the user's
299func (p *OAuthProxy) ClearCSRFCookie(rw http.ResponseWriter, req *http.Request) {
321func (p *OAuthProxy) ClearCSRFCookie(rw http.ResponseWriter, req *http.Request) {
300 http.SetCookie(rw, p.MakeCSRFCookie(req, "", time.Hour*-1, time.Now()))
322 http.SetCookie(rw, p.MakeCSRFCookie(req, "", time.Hour*-1, time.Now()))
325+// SetCSRFCookie adds a CSRF cookie to the response
303func (p *OAuthProxy) SetCSRFCookie(rw http.ResponseWriter, req *http.Request, val string) {
326func (p *OAuthProxy) SetCSRFCookie(rw http.ResponseWriter, req *http.Request, val string) {
304 http.SetCookie(rw, p.MakeCSRFCookie(req, val, p.CookieExpire, time.Now()))
327 http.SetCookie(rw, p.MakeCSRFCookie(req, val, p.CookieExpire, time.Now()))
330+// ClearSessionCookie creates a cookie to unset the user's authentication cookie
331+// stored in the user's session
307func (p *OAuthProxy) ClearSessionCookie(rw http.ResponseWriter, req *http.Request) {
332func (p *OAuthProxy) ClearSessionCookie(rw http.ResponseWriter, req *http.Request) {
308 clr := p.MakeSessionCookie(req, "", time.Hour*-1, time.Now())
333 clr := p.MakeSessionCookie(req, "", time.Hour*-1, time.Now())
309 http.SetCookie(rw, clr)
334 http.SetCookie(rw, clr)
@@ -316,10 +341,12 @@ func (p *OAuthProxy) ClearSessionCookie(rw http.ResponseWriter, req *http.Reques
344+// SetSessionCookie adds the user's session cookie to the response
319func (p *OAuthProxy) SetSessionCookie(rw http.ResponseWriter, req *http.Request, val string) {
345func (p *OAuthProxy) SetSessionCookie(rw http.ResponseWriter, req *http.Request, val string) {
320 http.SetCookie(rw, p.MakeSessionCookie(req, val, p.CookieExpire, time.Now()))
346 http.SetCookie(rw, p.MakeSessionCookie(req, val, p.CookieExpire, time.Now()))
349+// LoadCookiedSession reads the user's authentication details from the request
323func (p *OAuthProxy) LoadCookiedSession(req *http.Request) (*providers.SessionState, time.Duration, error) {
350func (p *OAuthProxy) LoadCookiedSession(req *http.Request) (*providers.SessionState, time.Duration, error) {
324 var age time.Duration
351 var age time.Duration
325 c, err := req.Cookie(p.CookieName)
352 c, err := req.Cookie(p.CookieName)
@@ -341,6 +368,7 @@ func (p *OAuthProxy) LoadCookiedSession(req *http.Request) (*providers.SessionSt
341 return session, age, nil
368 return session, age, nil
371+// SaveSession creates a new session cookie value and sets this on the response
344func (p *OAuthProxy) SaveSession(rw http.ResponseWriter, req *http.Request, s *providers.SessionState) error {
372func (p *OAuthProxy) SaveSession(rw http.ResponseWriter, req *http.Request, s *providers.SessionState) error {
345 value, err := p.provider.CookieForSession(s, p.CookieCipher)
373 value, err := p.provider.CookieForSession(s, p.CookieCipher)
346 if err != nil {
374 if err != nil {
@@ -350,16 +378,19 @@ func (p *OAuthProxy) SaveSession(rw http.ResponseWriter, req *http.Request, s *p
350 return nil
378 return nil
381+// RobotsTxt disallows scraping pages from the OAuthProxy
353func (p *OAuthProxy) RobotsTxt(rw http.ResponseWriter) {
382func (p *OAuthProxy) RobotsTxt(rw http.ResponseWriter) {
354 rw.WriteHeader(http.StatusOK)
383 rw.WriteHeader(http.StatusOK)
355 fmt.Fprintf(rw, "User-agent: *\nDisallow: /")
384 fmt.Fprintf(rw, "User-agent: *\nDisallow: /")
387+// PingPage responds 200 OK to requests
358func (p *OAuthProxy) PingPage(rw http.ResponseWriter) {
388func (p *OAuthProxy) PingPage(rw http.ResponseWriter) {
359 rw.WriteHeader(http.StatusOK)
389 rw.WriteHeader(http.StatusOK)
360 fmt.Fprintf(rw, "OK")
390 fmt.Fprintf(rw, "OK")
393+// ErrorPage writes an error response
363func (p *OAuthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
394func (p *OAuthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
364 log.Printf("ErrorPage %d %s %s", code, title, message)
395 log.Printf("ErrorPage %d %s %s", code, title, message)
365 rw.WriteHeader(code)
396 rw.WriteHeader(code)
@@ -375,6 +406,7 @@ func (p *OAuthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, m
375 p.templates.ExecuteTemplate(rw, "error.html", t)
406 p.templates.ExecuteTemplate(rw, "error.html", t)
409+// SignInPage writes the sing in template to the response
378func (p *OAuthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
410func (p *OAuthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
379 p.ClearSessionCookie(rw, req)
411 p.ClearSessionCookie(rw, req)
380 rw.WriteHeader(code)
412 rw.WriteHeader(code)
@@ -407,6 +439,7 @@ func (p *OAuthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
407 p.templates.ExecuteTemplate(rw, "sign_in.html", t)
439 p.templates.ExecuteTemplate(rw, "sign_in.html", t)
442+// ManualSignIn handles basic auth logins to the proxy
410func (p *OAuthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (string, bool) {
443func (p *OAuthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (string, bool) {
411 if req.Method != "POST" || p.HtpasswdFile == nil {
444 if req.Method != "POST" || p.HtpasswdFile == nil {
412 return "", false
445 return "", false
@@ -424,6 +457,8 @@ func (p *OAuthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (st
424 return "", false
457 return "", false
460+// GetRedirect reads the query parameter to get the URL to redirect clients to
461+// once authenticated with the OAuthProxy
427func (p *OAuthProxy) GetRedirect(req *http.Request) (redirect string, err error) {
462func (p *OAuthProxy) GetRedirect(req *http.Request) (redirect string, err error) {
428 err = req.ParseForm()
463 err = req.ParseForm()
429 if err != nil {
464 if err != nil {
@@ -438,11 +473,13 @@ func (p *OAuthProxy) GetRedirect(req *http.Request) (redirect string, err error)
476+// IsWhitelistedRequest is used to check if auth should be skipped for this request
441func (p *OAuthProxy) IsWhitelistedRequest(req *http.Request) (ok bool) {
477func (p *OAuthProxy) IsWhitelistedRequest(req *http.Request) (ok bool) {
442 isPreflightRequestAllowed := p.skipAuthPreflight && req.Method == "OPTIONS"
478 isPreflightRequestAllowed := p.skipAuthPreflight && req.Method == "OPTIONS"
443 return isPreflightRequestAllowed || p.IsWhitelistedPath(req.URL.Path)
479 return isPreflightRequestAllowed || p.IsWhitelistedPath(req.URL.Path)
482+// IsWhitelistedPath is used to check if the request path is allowed without auth
446func (p *OAuthProxy) IsWhitelistedPath(path string) (ok bool) {
483func (p *OAuthProxy) IsWhitelistedPath(path string) (ok bool) {
447 for _, u := range p.compiledRegex {
484 for _, u := range p.compiledRegex {
448 ok = u.MatchString(path)
485 ok = u.MatchString(path)
@@ -484,6 +521,7 @@ func (p *OAuthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
524+// SignIn serves a page prompting users to sign in
487func (p *OAuthProxy) SignIn(rw http.ResponseWriter, req *http.Request) {
525func (p *OAuthProxy) SignIn(rw http.ResponseWriter, req *http.Request) {
488 redirect, err := p.GetRedirect(req)
526 redirect, err := p.GetRedirect(req)
489 if err != nil {
527 if err != nil {
@@ -505,11 +543,13 @@ func (p *OAuthProxy) SignIn(rw http.ResponseWriter, req *http.Request) {
546+// SignOut sends a response to clear the authentication cookie
508func (p *OAuthProxy) SignOut(rw http.ResponseWriter, req *http.Request) {
547func (p *OAuthProxy) SignOut(rw http.ResponseWriter, req *http.Request) {
509 p.ClearSessionCookie(rw, req)
548 p.ClearSessionCookie(rw, req)
510 http.Redirect(rw, req, "/", 302)
549 http.Redirect(rw, req, "/", 302)
552+// OAuthStart starts the OAuth2 authentication flow
513func (p *OAuthProxy) OAuthStart(rw http.ResponseWriter, req *http.Request) {
553func (p *OAuthProxy) OAuthStart(rw http.ResponseWriter, req *http.Request) {
514 nonce, err := cookie.Nonce()
554 nonce, err := cookie.Nonce()
515 if err != nil {
555 if err != nil {
@@ -526,6 +566,8 @@ func (p *OAuthProxy) OAuthStart(rw http.ResponseWriter, req *http.Request) {
526 http.Redirect(rw, req, p.provider.GetLoginURL(redirectURI, fmt.Sprintf("%v:%v", nonce, redirect)), 302)
566 http.Redirect(rw, req, p.provider.GetLoginURL(redirectURI, fmt.Sprintf("%v:%v", nonce, redirect)), 302)
569+// OAuthCallback is the OAuth2 authentication flow callback that finishes the
570+// OAuth2 authentication flow
529func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
571func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
530 remoteAddr := getRemoteAddr(req)
572 remoteAddr := getRemoteAddr(req)
@@ -587,6 +629,7 @@ func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
632+// AuthenticateOnly checks whether the user is currently logged in
590func (p *OAuthProxy) AuthenticateOnly(rw http.ResponseWriter, req *http.Request) {
633func (p *OAuthProxy) AuthenticateOnly(rw http.ResponseWriter, req *http.Request) {
591 status := p.Authenticate(rw, req)
634 status := p.Authenticate(rw, req)
592 if status == http.StatusAccepted {
635 if status == http.StatusAccepted {
@@ -596,6 +639,8 @@ func (p *OAuthProxy) AuthenticateOnly(rw http.ResponseWriter, req *http.Request)
642+// Proxy proxies the user request if the user is authenticated else it prompts
643+// them to authenticate
599func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
644func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
600 status := p.Authenticate(rw, req)
645 status := p.Authenticate(rw, req)
601 if status == http.StatusInternalServerError {
646 if status == http.StatusInternalServerError {
@@ -612,6 +657,7 @@ func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
660+// Authenticate checks whether a user is authenticated
615func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int {
661func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int {
616 var saveSession, clearSession, revalidated bool
662 var saveSession, clearSession, revalidated bool
617 remoteAddr := getRemoteAddr(req)
663 remoteAddr := getRemoteAddr(req)
@@ -711,6 +757,8 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
711 return http.StatusAccepted
757 return http.StatusAccepted
760+// CheckBasicAuth checks the requests Authorization header for basic auth
761+// credentials and authenticates these against the proxies HtpasswdFile
714func (p *OAuthProxy) CheckBasicAuth(req *http.Request) (*providers.SessionState, error) {
762func (p *OAuthProxy) CheckBasicAuth(req *http.Request) (*providers.SessionState, error) {
715 if p.HtpasswdFile == nil {
763 if p.HtpasswdFile == nil {
716 return nil, nil
764 return nil, nil