52 lines
1005 B
Go
52 lines
1005 B
Go
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")
|
|
}
|