Namespace is a query param

This commit is contained in:
Bel LaPointe
2020-07-24 12:42:42 -06:00
parent 0e980b1128
commit 11e7d13cca
5 changed files with 76 additions and 31 deletions

51
view/auth.go Normal file
View File

@@ -0,0 +1,51 @@
package view
import (
"encoding/json"
"errors"
"local/dndex/config"
"local/dndex/storage"
"net/http"
)
const (
AuthKey = "DnDex-Auth"
)
func Auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
if !config.New().Auth {
return nil
}
if err := auth(g, w, r); err != nil {
json.NewEncoder(w).Encode(map[string]interface{}{"error": "error when authorizing: " + err.Error()})
return err
}
return nil
}
func auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
if !hasAuth(r) {
if err := requestAuth(g, w, r); err != nil {
return err
}
return errors.New("auth requested")
}
return checkAuth(g, r)
}
func hasAuth(r *http.Request) bool {
_, ok := r.Cookie(AuthKey)
return ok == nil
}
func checkAuth(g storage.Graph, r *http.Request) error {
panic(nil)
/*
token, _ := r.Cookie(AuthKey)
return errors.New("not impl")
*/
}
func requestAuth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl")
}