Cleanup and autobuild file
This commit is contained in:
parent
2f0e142c92
commit
f583b02d88
|
@ -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 {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strings"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
type BasicAuthInfo struct {
|
||||
|
|
|
@ -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:
|
|
@ -124,4 +124,3 @@ func (c *Context) QueryValueWithDefault(key string, def string) string {
|
|||
}
|
||||
return val
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
14
server.go
14
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)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"git.thirdmartini.com/pub/snap/auth"
|
||||
)
|
||||
|
||||
|
||||
var rootExpected = []byte(`
|
||||
<html lang="en">
|
||||
<head>
|
||||
|
@ -38,7 +37,6 @@ User: admin
|
|||
</html>
|
||||
`)
|
||||
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue