diff --git a/auth/auth.go b/auth/auth.go index 426f6ab..e0a5024 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -4,7 +4,6 @@ import ( "net/http" ) - type AuthData struct { User string Group string @@ -20,7 +19,6 @@ type Authenticator interface { DoAuth(w http.ResponseWriter, r *http.Request) (*AuthData, bool) } - //---------------------------------------------------------------------------------------------------------------------- func NewAuth(kind string) AuthManager { switch kind { @@ -33,4 +31,4 @@ func NewAuth(kind string) AuthManager { default: return NewNoAuth() } -} \ No newline at end of file +} diff --git a/auth/basic.go b/auth/basic.go index 47d5687..f63d054 100644 --- a/auth/basic.go +++ b/auth/basic.go @@ -1,9 +1,9 @@ package auth import ( + "encoding/base64" "net/http" "strings" - "encoding/base64" ) type BasicAuthInfo struct { diff --git a/build.yaml b/build.yaml new file mode 100644 index 0000000..f55205c --- /dev/null +++ b/build.yaml @@ -0,0 +1,27 @@ +# +# Build config for ThirdMartini go-builder +# +gobuild.version.required: 1.0 +gobuild.work.dir: build + +properties: + version: git rev-parse --short=12 HEAD + +jobs: + # + # Format source code + format: + - name: Format source code + type: golang + source: git.thirdmartini.com/pub/snap/... + options: + flags: + + # + # Run some tests + test: + - name: Running unit tests + type: golang + source: git.thirdmartini.com/pub/snap/... + options: + flags: diff --git a/context.go b/context.go index e7bd0f5..da3d3b3 100644 --- a/context.go +++ b/context.go @@ -124,4 +124,3 @@ func (c *Context) QueryValueWithDefault(key string, def string) string { } return val } - diff --git a/examples/simple/main.go b/examples/simple/main.go index de48927..b239e80 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -13,18 +13,16 @@ func handlerAuthenticated(c *snap.Context) { c.Reply("snap/example/simple 1.0 (authenticated)") } - func mustRunServer(auth auth.Authenticator) { srv := snap.New("127.0.0.1:9000", "./", nil) srv.SetDebug(true) srv.HandleFunc("/", handler) - srv.HandleFuncCustomAuth(auth,"/auth", "", handlerAuthenticated) + srv.HandleFuncCustomAuth(auth, "/auth", "", handlerAuthenticated) srv.Serve() } - func main() { auth := auth.NewAuth("basic") auth.AddUser("admin", "admin", "password") diff --git a/server.go b/server.go index dda593c..b389919 100644 --- a/server.go +++ b/server.go @@ -55,7 +55,7 @@ func (s *Server) authenticated(auth auth.Authenticator, redirect string, handle if redirect != "" { http.Redirect(w, r, redirect, http.StatusSeeOther) } else { - http.Error(w, "Not authorized", http.StatusForbidden) + http.Error(w, "Not authorized", http.StatusUnauthorized) } } else { c := s.makeContext(rec, w, r) @@ -167,12 +167,12 @@ func (s *Server) renderError(w http.ResponseWriter, code int, msg string) { w.Write([]byte(msg)) } -func (s *Server) HandleFuncAuthenticated(path,redirect string, f func(c *Context)) *mux.Route { +func (s *Server) HandleFuncAuthenticated(path, redirect string, f func(c *Context)) *mux.Route { if s.auth == nil { return nil } - return s.router.HandleFunc(path, s.authenticated(s.auth,redirect, f)) + return s.router.HandleFunc(path, s.authenticated(s.auth, redirect, f)) } func (s *Server) HandleFuncCustomAuth(auth auth.Authenticator, path, redirect string, f func(c *Context)) *mux.Route { @@ -180,7 +180,7 @@ func (s *Server) HandleFuncCustomAuth(auth auth.Authenticator, path, redirect st log.Warn("Nil auth on", path) return nil } - return s.router.HandleFunc(path, s.authenticated(auth,redirect, f)) + return s.router.HandleFunc(path, s.authenticated(auth, redirect, f)) } func (s *Server) HandleFunc(path string, f func(c *Context)) *mux.Route { @@ -248,9 +248,9 @@ func (s *Server) WithDebug(debugURL string) *Server { } func (s *Server) Dump() { - fmt.Printf( " Path: %s\n", s.path) - fmt.Printf( " Theme: %s\n", s.theme) - fmt.Printf( " Templates: %s\n", s.templates) + fmt.Printf(" Path: %s\n", s.path) + fmt.Printf(" Theme: %s\n", s.theme) + fmt.Printf(" Templates: %s\n", s.templates) } diff --git a/server_test.go b/server_test.go index 703bb42..ef2fe46 100644 --- a/server_test.go +++ b/server_test.go @@ -11,7 +11,6 @@ import ( "git.thirdmartini.com/pub/snap/auth" ) - var rootExpected = []byte(` @@ -38,7 +37,6 @@ User: admin `) - func handlerRoot(c *Context) { c.RenderEx("index.html", nil) } @@ -51,7 +49,7 @@ func handlerTest(c *Context) { c.RenderEx("test.html", nil) } -func doTestRequest(t *testing.T, req *http.Request) ([]byte, int, error){ +func doTestRequest(t *testing.T, req *http.Request) ([]byte, int, error) { resp, err := http.DefaultClient.Do(req) assert.Nil(t, err) assert.NotNil(t, resp) @@ -70,7 +68,7 @@ func TestServer_BasicAuth(t *testing.T) { s := New("", "/test", auth) s.SetDebug(true) s.SetTemplatePath("test/templates") - s.WithStaticFiles("/static", "test/static" ) + s.WithStaticFiles("/static", "test/static") s.WithTheme("skin/") s.HandleFunc("/", handlerRoot) s.HandleFuncAuthenticated("/login", "", handlerLogin) @@ -89,26 +87,26 @@ func TestServer_BasicAuth(t *testing.T) { assert.Equal(t, rootExpected, data) // Check GOOD password - req, err = http.NewRequest(http.MethodGet, ts.URL +"/login", nil) + req, err = http.NewRequest(http.MethodGet, ts.URL+"/login", nil) assert.Nil(t, err) - req.SetBasicAuth("admin","test") + req.SetBasicAuth("admin", "test") data, code, err = doTestRequest(t, req) assert.Nil(t, err) assert.Equal(t, http.StatusOK, code) assert.Equal(t, rootExpected, data) // Check BAD password - req, err = http.NewRequest(http.MethodGet, ts.URL +"/login", nil) + req, err = http.NewRequest(http.MethodGet, ts.URL+"/login", nil) assert.Nil(t, err) - req.SetBasicAuth("admin","badpass") + req.SetBasicAuth("admin", "badpass") data, code, err = doTestRequest(t, req) assert.Nil(t, err) - assert.Equal(t, http.StatusForbidden, code) + assert.Equal(t, http.StatusUnauthorized, code) // Check GOOD password and context in templates - req, err = http.NewRequest(http.MethodGet, ts.URL +"/test", nil) + req, err = http.NewRequest(http.MethodGet, ts.URL+"/test", nil) assert.Nil(t, err) - req.SetBasicAuth("admin","test") + req.SetBasicAuth("admin", "test") data, code, err = doTestRequest(t, req) assert.Nil(t, err) assert.Equal(t, http.StatusOK, code) @@ -116,7 +114,7 @@ func TestServer_BasicAuth(t *testing.T) { // Test Static Content // Check GOOD password and context in templates - req, err = http.NewRequest(http.MethodGet, ts.URL +"/static/skin/skin.html", nil) + req, err = http.NewRequest(http.MethodGet, ts.URL+"/static/skin/skin.html", nil) assert.Nil(t, err) data, code, err = doTestRequest(t, req) assert.Nil(t, err) @@ -149,4 +147,4 @@ func TestServer_TokenAuth(t *testing.T) { assert.Nil(t, err) assert.Equal(t, http.StatusOK, code) assert.Equal(t, rootExpected, data) -} \ No newline at end of file +}