diff --git a/main.go b/main.go index 82c4a15..c915806 100644 --- a/main.go +++ b/main.go @@ -38,12 +38,13 @@ type VerifyBody struct { type VerifyResponse struct { Data struct { - Valid bool `json:"valid"` - RedirectURL *string `json:"redirectUrl"` - Username *string `json:"username,omitempty"` - Email *string `json:"email,omitempty"` - Name *string `json:"name,omitempty"` - ResponseHeaders map[string]string `json:"responseHeaders,omitempty"` + HeaderAuthChallenged bool `json:"headerAuthChallenged"` + Valid bool `json:"valid"` + RedirectURL *string `json:"redirectUrl"` + Username *string `json:"username,omitempty"` + Email *string `json:"email,omitempty"` + Name *string `json:"name,omitempty"` + ResponseHeaders map[string]string `json:"responseHeaders,omitempty"` } `json:"data"` } @@ -204,6 +205,20 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } } + if result.Data.HeaderAuthChallenged { + fmt.Println("Badger: challenging client for header authentication") + rw.Header().Add("WWW-Authenticate", "Basic realm=\"pangolin\"") + + if result.Data.RedirectURL != nil && *result.Data.RedirectURL != "" { + rw.Header().Set("Content-Type", "text/html; charset=utf-8") + rw.WriteHeader(http.StatusUnauthorized) + rw.Write([]byte(p.renderRedirectPage(*result.Data.RedirectURL))) + } else { + http.Error(rw, "Unauthorized", http.StatusUnauthorized) + } + return + } + if result.Data.RedirectURL != nil && *result.Data.RedirectURL != "" { fmt.Println("Badger: Redirecting to", *result.Data.RedirectURL) http.Redirect(rw, req, *result.Data.RedirectURL, http.StatusFound) @@ -254,3 +269,47 @@ func (p *Badger) getScheme(req *http.Request) string { } return "http" } + +func (p *Badger) renderRedirectPage(redirectURL string) string { + return fmt.Sprintf(` + +
+ +Redirecting...
+If you are not redirected automatically, click here.
+