From 5c4323a168831eb8c2c3dabee0e78aa7eda78a42 Mon Sep 17 00:00:00 2001 From: Chapuis Bertil Date: Tue, 25 Aug 2015 11:35:49 +0200 Subject: [PATCH] basic auth --- auth.go | 23 +++++++++++++++++++++-- handlers.go | 11 ++++++----- repository.go | 3 ++- router.go | 6 +++--- variables.go | 21 ++++++++++++++++++++- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/auth.go b/auth.go index 4171697..ec5f7fc 100644 --- a/auth.go +++ b/auth.go @@ -1,9 +1,28 @@ package main import ( + "errors" "net/http" ) -func Authorize(r *http.Request) bool { - return true +func Authorize(r *http.Request) error { + username, password, ok := r.BasicAuth() + if !ok { + return errors.New("malformed basic auth credentials") + } + + repo, err := RepositoryName(r.RequestURI) + if err != nil { + return err + } + + if username != "user" || password != "pass" { + return errors.New("unknown user") + } + + if username != repo { + return errors.New("unauthorized") + } + + return nil } diff --git a/handlers.go b/handlers.go index 875ec0a..6ec1909 100644 --- a/handlers.go +++ b/handlers.go @@ -84,7 +84,7 @@ func ListBlob(w http.ResponseWriter, r *http.Request, c *Context) { return } bt := BackendType(uri) - if bt.IsNull() { + if string(bt) == "" { http.NotFound(w, r) return } @@ -114,7 +114,7 @@ func HeadBlob(w http.ResponseWriter, r *http.Request, c *Context) { return } bt := BackendType(uri) - if bt.IsNull() { + if string(bt) == "" { http.NotFound(w, r) return } @@ -142,7 +142,7 @@ func GetBlob(w http.ResponseWriter, r *http.Request, c *Context) { return } bt := BackendType(uri) - if bt.IsNull() { + if string(bt) == "" { http.NotFound(w, r) return } @@ -172,7 +172,7 @@ func PostBlob(w http.ResponseWriter, r *http.Request, c *Context) { return } bt := BackendType(uri) - if bt.IsNull() { + if string(bt) == "" { http.NotFound(w, r) return } @@ -191,6 +191,7 @@ func PostBlob(w http.ResponseWriter, r *http.Request, c *Context) { http.NotFound(w, r) return } + w.WriteHeader(201) } func DeleteBlob(w http.ResponseWriter, r *http.Request, c *Context) { @@ -206,7 +207,7 @@ func DeleteBlob(w http.ResponseWriter, r *http.Request, c *Context) { return } bt := BackendType(uri) - if bt.IsNull() { + if string(bt) == "" { http.NotFound(w, r) return } diff --git a/repository.go b/repository.go index 04247f8..f050e64 100644 --- a/repository.go +++ b/repository.go @@ -1,6 +1,7 @@ package main import ( + "io" "io/ioutil" "os" "path/filepath" @@ -74,7 +75,7 @@ func (r *Repository) HasBlob(bt backend.Type, id backend.ID) bool { return true } -func (r *Repository) ReadBlob(bt backend.Type, id backend.ID) (*os.File, error) { +func (r *Repository) ReadBlob(bt backend.Type, id backend.ID) (io.ReadSeeker, error) { file := filepath.Join(r.path, string(bt), id.String()) f, err := os.Open(file) if err != nil { diff --git a/router.go b/router.go index 4c2d3be..367f22a 100644 --- a/router.go +++ b/router.go @@ -18,14 +18,14 @@ func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Printf("%s %s", m, u) - if Authorize(r) { + if err := Authorize(r); err == nil { if handler := RestAPI(m, u); handler != nil { handler(w, r, &router.Context) } else { http.Error(w, "not found", 404) } } else { - http.Error(w, "unauthorized", 403) + http.Error(w, err.Error(), 403) } } @@ -54,7 +54,7 @@ func RestAPI(m string, u string) Handler { // Route blob requests id := BlobID(u) - if len(s) == 4 && !bt.IsNull() && bt != backend.Config { + if len(s) == 4 && string(bt) != "" && bt != backend.Config { if s[3] == "" && m == "GET" { return ListBlob } else if !id.IsNull() { diff --git a/variables.go b/variables.go index 548fb88..ff14252 100644 --- a/variables.go +++ b/variables.go @@ -33,11 +33,30 @@ func BackendType(u string) backend.Type { s := strings.Split(u, "/") var bt backend.Type if len(s) > 2 { - bt, _ = backend.ParseType(s[2]) + bt = parseBackendType(s[2]) } return bt } +func parseBackendType(u string) backend.Type { + switch u { + case string(backend.Config): + return backend.Config + case string(backend.Data): + return backend.Data + case string(backend.Snapshot): + return backend.Snapshot + case string(backend.Key): + return backend.Key + case string(backend.Index): + return backend.Index + case string(backend.Lock): + return backend.Lock + default: + return "" + } +} + // Returns the blob ID for a given path func BlobID(u string) backend.ID { s := strings.Split(u, "/")