This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for http basic authentication
Change-Id: Ib5406e3cae876e1262f8e214e3eebaf86c3c4a6e
- Loading branch information
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,7 @@ type Config struct { | |
Port int | ||
|
||
EnableLOC bool | ||
|
||
User string | ||
Password string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters