Auth implemented ish

This commit is contained in:
Bel LaPointe
2020-07-24 14:45:03 -06:00
parent 11e7d13cca
commit d3ac4f5c22
5 changed files with 366 additions and 59 deletions

View File

@@ -2,6 +2,7 @@ package view
import (
"encoding/json"
"errors"
"fmt"
"local/dndex/config"
"local/dndex/storage"
@@ -22,22 +23,31 @@ func jsonHandler(g storage.Graph) http.Handler {
mux := http.NewServeMux()
routes := []struct {
path string
foo func(g storage.Graph, w http.ResponseWriter, r *http.Request) error
path string
foo func(g storage.Graph, w http.ResponseWriter, r *http.Request) error
noauth bool
}{
{
path: "/who",
foo: who,
},
{
path: config.New().FilePrefix + "/",
foo: files,
path: config.New().FilePrefix + "/",
foo: files,
noauth: true,
},
}
for _, route := range routes {
foo := route.foo
auth := !route.noauth
mux.HandleFunc(route.path, func(w http.ResponseWriter, r *http.Request) {
if auth {
if err := Auth(g, w, r); err != nil {
log.Println(err)
return
}
}
if err := foo(g, w, r); err != nil {
status := http.StatusInternalServerError
if strings.Contains(err.Error(), "collision") {
@@ -50,10 +60,6 @@ func jsonHandler(g storage.Graph) http.Handler {
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := Auth(g, w, r); err != nil {
log.Println(err)
return
}
if gziphttp.Can(r) {
gz := gziphttp.New(w)
defer gz.Close()
@@ -62,3 +68,19 @@ func jsonHandler(g storage.Graph) http.Handler {
mux.ServeHTTP(w, r)
})
}
func getAuthNamespace(r *http.Request) (string, error) {
namespace := r.URL.Query().Get("namespace")
if len(namespace) == 0 {
return "", errors.New("no namespace found")
}
return strings.Join([]string{namespace, AuthKey}, "."), nil
}
func getNamespace(r *http.Request) (string, error) {
namespace := r.URL.Query().Get("namespace")
if len(namespace) == 0 {
return "", errors.New("no namespace found")
}
return namespace, nil
}