Skip to content

Commit

Permalink
stop request on 401
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Oct 29, 2014
1 parent b5a3c48 commit 04ea7b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"

"github.com/bmizerany/pat"
Expand Down Expand Up @@ -50,7 +51,15 @@ func NewAPI(g *gobot.Gobot) *api {
// ServeHTTP calls api handlers and then serves request using api router
func (a *api) ServeHTTP(res http.ResponseWriter, req *http.Request) {
for _, handler := range a.handlers {
handler(res, req)
rec := httptest.NewRecorder()
handler(rec, req)
for k, v := range rec.Header() {
res.Header()[k] = v
}
if rec.Code == http.StatusUnauthorized {
http.Error(res, "Not Authorized", http.StatusUnauthorized)
return
}
}
a.router.ServeHTTP(res, req)
}
Expand Down
2 changes: 1 addition & 1 deletion api/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

// BasicAuth returns basic auth handler.
// Inspired by https://github.com/codegangsta/martini-contrib/blob/master/auth/
func BasicAuth(username, password string) http.HandlerFunc {
// Inspired by https://github.com/codegangsta/martini-contrib/blob/master/auth/
return func(res http.ResponseWriter, req *http.Request) {
if !secureCompare(req.Header.Get("Authorization"),
"Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)),
Expand Down

0 comments on commit 04ea7b0

Please sign in to comment.