Permalink
Browse files

Add support for http basic authentication

Change-Id: Ib5406e3cae876e1262f8e214e3eebaf86c3c4a6e
  • Loading branch information...
1 parent ea85aca commit a2f6f5b056cd50e437e89b42d4b11af69885ab80 @bluesalt bluesalt committed Sep 6, 2012
Showing with 134 additions and 1 deletion.
  1. +3 −0 config.go
  2. +29 −0 http_auth.go
  3. +83 −0 http_auth_test.go
  4. +19 −1 http_handler.go
View
@@ -7,4 +7,7 @@ type Config struct {
Port int
EnableLOC bool
+
+ User string
+ Password string
}
View
@@ -0,0 +1,29 @@
+package steno
+
+import (
+ "encoding/base64"
+ "net/http"
+ "strings"
+)
+
+func checkAuth(req *http.Request, user string, password string) bool {
+
+ if user == "" && password == "" {
+ return true
+ }
+
+ authParts := strings.Split(req.Header.Get("Authorization"), " ")
+ if len(authParts) != 2 || authParts[0] != "Basic" {
+ return false
+ }
+ code, err := base64.StdEncoding.DecodeString(authParts[1])
+ if err != nil {
+ return false
+ }
+ userPass := strings.Split(string(code), ":")
+ if len(userPass) != 2 || userPass[0] != user || userPass[1] != password {
+ return false
+ }
+
+ return true
+}
View
@@ -0,0 +1,83 @@
+package steno
+
+import (
+ "fmt"
+ . "launchpad.net/gocheck"
+ "net/http"
+ "net/http/httptest"
+)
+
+type HttpAuthSuite struct {
+ basicAuth *BasicAuth
+}
+
+var _ = Suite(&HttpAuthSuite{})
+
+func (s *HttpAuthSuite) SetUpSuite(c *C) {
+ mux := http.NewServeMux()
+ mux.HandleFunc(HTTP_REGEXP_PATH, regExpHandler)
+ mux.HandleFunc(HTTP_LOGGER_PATH, loggerHandler)
+ mux.HandleFunc(HTTP_LIST_LOGGERS_PATH, loggersListHandler)
+
+ s.basicAuth = &BasicAuth{
+ handler: mux,
+ }
+
+ cfg := Config{}
+ cfg.User = "jeff"
+ cfg.Password = "li"
+ cfg.Sinks = []Sink{newNullSink()}
+ Init(&cfg)
+}
+
+func (s *HttpAuthSuite) TearDownSuite(c *C) {
+ config = Config{}
+
+ s.basicAuth = nil
+}
+
+func (s *HttpAuthSuite) SetUpTest(c *C) {
+ loggers = make(map[string]*BaseLogger)
+}
+
+func (s *HttpAuthSuite) TearDownTest(c *C) {
+ loggerRegexp = nil
+ loggerRegexpLevel = nil
+
+ loggers = nil
+}
+
+func testGetMethodStatusCode(req *http.Request, c *C) {
+ r, _ := http.DefaultClient.Do(req)
+ c.Assert(r.StatusCode, Equals, http.StatusUnauthorized)
+
+ req.SetBasicAuth("jeff", "li")
+ r, _ = http.DefaultClient.Do(req)
+ c.Assert(r.StatusCode, Equals, http.StatusOK)
+}
+
+func (s *HttpAuthSuite) TestGetRegexpWithAuth(c *C) {
+ ts := httptest.NewServer(s.basicAuth)
+ defer ts.Close()
+
+ req, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", ts.URL, HTTP_REGEXP_PATH), nil)
+ testGetMethodStatusCode(req, c)
+}
+
+func (s *HttpAuthSuite) TestGetLoggerWithAuth(c *C) {
+ ts := httptest.NewServer(s.basicAuth)
+ defer ts.Close()
+
+ NewLogger("foobar")
+
+ req, _ := http.NewRequest("GET", fmt.Sprintf("%s%s%s", ts.URL, HTTP_LOGGER_PATH, "foobar"), nil)
+ testGetMethodStatusCode(req, c)
+}
+
+func (s *HttpAuthSuite) TestGetLoggersWithAuth(c *C) {
+ ts := httptest.NewServer(s.basicAuth)
+ defer ts.Close()
+
+ req, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", ts.URL, HTTP_LIST_LOGGERS_PATH), nil)
+ testGetMethodStatusCode(req, c)
+}
View
@@ -99,16 +99,34 @@ func loggersListHandler(w http.ResponseWriter, r *http.Request) {
}
}
+type BasicAuth struct {
+ handler http.Handler
+}
+
+func (a *BasicAuth) ServeHTTP(w http.ResponseWriter, req *http.Request) {
+ if !checkAuth(req, config.User, config.Password) {
+ w.Header().Set("WWW-Authenticate", "Basic")
+ w.WriteHeader(http.StatusUnauthorized)
+ w.Write([]byte("401 Unauthorized\n"))
+ } else {
+ a.handler.ServeHTTP(w, req)
+ }
+}
+
func initHttpServer(port int) {
mux := http.NewServeMux()
mux.HandleFunc(HTTP_REGEXP_PATH, regExpHandler)
mux.HandleFunc(HTTP_LOGGER_PATH, loggerHandler)
mux.HandleFunc(HTTP_LIST_LOGGERS_PATH, loggersListHandler)
+ basicAuth := &BasicAuth{
+ handler: mux,
+ }
+
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
- Handler: mux,
+ Handler: basicAuth,
}
go server.ListenAndServe()

0 comments on commit a2f6f5b

Please sign in to comment.