cleanup som stuff snap things
This commit is contained in:
parent
fd5c64ddde
commit
4dc448ad37
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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])
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
81
server.go
81
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue