From 4dc448ad3746e078dc980bb6c7c11c8e63d54c12 Mon Sep 17 00:00:00 2001 From: ssobolewski Date: Wed, 7 Feb 2018 15:25:00 -0700 Subject: [PATCH] cleanup som stuff snap things --- auth/auth.go | 6 ++-- auth/basic.go | 45 ++++++++++++---------------- context.go | 30 +++++++++++++++++++ server.go | 81 ++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 119 insertions(+), 43 deletions(-) create mode 100644 context.go diff --git a/auth/auth.go b/auth/auth.go index 9829f05..4f05c5b 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -1,7 +1,9 @@ package auth -import "net/http" +import ( + "net/http" +) type AuthManager interface { - DoAuth(h http.HandlerFunc) http.HandlerFunc + DoAuth(w http.ResponseWriter,r *http.Request) (string,bool) } diff --git a/auth/basic.go b/auth/basic.go index 48ca41f..990d89b 100644 --- a/auth/basic.go +++ b/auth/basic.go @@ -24,35 +24,26 @@ func (ba *BasicAuth) authenticate(user, password string) bool { return false } -func (ba *BasicAuth) DoAuth(h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) +func (ba *BasicAuth) DoAuth(w http.ResponseWriter,r *http.Request) (string, bool) { + w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) - s := strings.SplitN(r.Header.Get("Authorization"), " ", 2) - if len(s) != 2 { - http.Error(w, "Not authorized", 401) - return - } - - b, err := base64.StdEncoding.DecodeString(s[1]) - if err != nil { - http.Error(w, err.Error(), 401) - return - } - - pair := strings.SplitN(string(b), ":", 2) - if len(pair) != 2 { - http.Error(w, "Not authorized", 401) - return - } - - if !ba.authenticate(pair[0], pair[1]) { - http.Error(w, "Not authorized", 401) - return - } - - h.ServeHTTP(w, r) + s := strings.SplitN(r.Header.Get("Authorization"), " ", 2) + if len(s) != 2 { + return "", false } + + b, err := base64.StdEncoding.DecodeString(s[1]) + if err != nil { + return "", false + } + + pair := strings.SplitN(string(b), ":", 2) + if len(pair) != 2 { + http.Error(w, "Not authorized", 401) + return "", false + } + + return pair[0], ba.authenticate(pair[0], pair[1]) } diff --git a/context.go b/context.go new file mode 100644 index 0000000..f49b879 --- /dev/null +++ b/context.go @@ -0,0 +1,30 @@ +package snap + +import "net/http" + +type Context struct { + Username string + srv *server + w http.ResponseWriter + r *http.Request +} + + +func (c *Context) GetRequest() *http.Request { + return c.r +} + +func (c *Context) Writer() http.ResponseWriter { + return c.w +} + + +func (c *Context) GetUser() string { + return c.Username +} + + +func (c *Context) Render(tmpl string, content interface{}) { + c.srv.render(c.w, tmpl, content) +} + diff --git a/server.go b/server.go index f283bba..9ef7c34 100644 --- a/server.go +++ b/server.go @@ -8,18 +8,19 @@ import ( "path/filepath" "strings" "time" - - "fmt" - "git.thirdmartini.com/pub/fancylog" - "git.thirdmartini.com/pub/snap-serve/auth" "path" + "fmt" + + "git.thirdmartini.com/pub/fancylog" + "git.thirdmartini.com/pub/snap/auth" ) type Server interface { Serve() error ServeTLS(keyPath string, certPath string) error - HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) error - HandleFuncAuthenticated(path string, f func(http.ResponseWriter, *http.Request)) error + SetDebug(enable bool) + HandleFunc(path string, f func(c *Context)) error + HandleFuncAuthenticated(path string, f func(c *Context)) error } type server struct { @@ -31,10 +32,53 @@ type server struct { cachedTmpl *template.Template } -type ApplicationContext struct { - Username string + +func (s *server) plain(f func(c *Context)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + c := &Context{ + Username: "", + r: r, + w: w, + srv: s, + } + f(c) + } } + +func (s *server) authenticated(handle func(c *Context)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + name, ok := s.auth.DoAuth( w, r ) + + if !ok { + http.Error(w, "Not authorized", 401) + } else { + c:= &Context{ + Username: name, + r: r, + w: w, + srv: s, + } + handle(c) + } + } +} + +func (s *server) wrapper(handle func(c *Context)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + c:= &Context{ + Username: "", + r: r, + w: w, + srv: s, + } + handle(c) + } + +} + + + func (s *server) loadTemplates() *template.Template { tmpl := template.New("") @@ -66,20 +110,29 @@ func (s *server) getTemplates() *template.Template { return s.cachedTmpl } -func (s *server) HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) error { - s.router.HandleFunc(path, f) - return nil +func (s *server) render(w http.ResponseWriter, tmpl string, content interface{} ) { + s.getTemplates().ExecuteTemplate(w, tmpl, content) } -func (s *server) HandleFuncAuthenticated(path string, f func(http.ResponseWriter, *http.Request)) error { +func (s *server) HandleFuncAuthenticated(path string, f func(c *Context)) error { if s.auth == nil { return fmt.Errorf("no auth manager provided") } - s.router.HandleFunc(path, s.auth.DoAuth(f)) + s.router.HandleFunc(path, s.authenticated(f)) return nil } +func (s *server) HandleFunc(path string, f func(c *Context)) error { + s.router.HandleFunc(path, s.wrapper(f)) + return nil +} + + +func (s *server) SetDebug(enable bool) { + s.debug = enable +} + func (s *server) ServeTLS(keyPath string, certPath string) error { srv := &http.Server{ Handler: s.router, @@ -113,6 +166,6 @@ func New(address string, path string, auth auth.AuthManager) Server { path: path, } - s.router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(s.ServerPath+"static/")))) + s.router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(s.path+"static/")))) return &s }