Mock auth forreal back to query param. go run . is functional, woot
parent
468e5bedd5
commit
3b72f05b4e
|
|
@ -10,6 +10,9 @@ import (
|
|||
|
||||
func Register(g storage.RateLimitedGraph, r *http.Request) error {
|
||||
namespaceRequested := readRequestedNamespace(r)
|
||||
if namespaceRequested == "" {
|
||||
return errors.New("namespace not found")
|
||||
}
|
||||
keyRequested := readRequestedKey(r)
|
||||
_, err := getKeyForNamespace(r.Context(), g, namespaceRequested)
|
||||
if err == nil {
|
||||
|
|
|
|||
|
|
@ -54,3 +54,34 @@ func TestRegister(t *testing.T) {
|
|||
t.Fatal(one)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterEmpty(t *testing.T) {
|
||||
g := storage.NewRateLimitedGraph()
|
||||
key := uuid.New().String()
|
||||
r := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/",
|
||||
strings.NewReader(
|
||||
fmt.Sprintf(
|
||||
"%s=&%s=%s",
|
||||
UserKey,
|
||||
AuthKey,
|
||||
key,
|
||||
),
|
||||
),
|
||||
)
|
||||
r.Header.Set("content-type", "application/x-www-form-urlencoded")
|
||||
if err := r.ParseForm(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := readRequested(r, UserKey); v != "" {
|
||||
t.Fatal(UserKey, v)
|
||||
}
|
||||
if v := readRequested(r, AuthKey); v == "" {
|
||||
t.Fatal(AuthKey, v)
|
||||
}
|
||||
err := Register(g, r)
|
||||
if err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import (
|
|||
"local/dndex/storage"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func Verify(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
|
||||
|
|
@ -24,6 +26,12 @@ func Verify(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request)
|
|||
}
|
||||
|
||||
func getToken(r *http.Request) (Token, bool) {
|
||||
if !config.New().Auth {
|
||||
namespaces, ok := r.URL.Query()["ns"]
|
||||
if ok && len(namespaces) > 0 {
|
||||
return Token{Namespace: namespaces[0], Token: uuid.New().String()}, true
|
||||
}
|
||||
}
|
||||
cookie, err := r.Cookie(AuthKey)
|
||||
if err != nil {
|
||||
return Token{}, false
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"local/dndex/config"
|
||||
"local/dndex/server/auth"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (rest *REST) users(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -10,7 +14,9 @@ func (rest *REST) users(w http.ResponseWriter, r *http.Request) {
|
|||
case http.MethodPost:
|
||||
default:
|
||||
rest.respNotFound(w)
|
||||
return
|
||||
}
|
||||
r.Header.Set("Application-Type", "application/x-www-form-urlencoded")
|
||||
switch r.URL.Path {
|
||||
case "/register":
|
||||
rest.usersRegister(w, r)
|
||||
|
|
@ -22,11 +28,31 @@ func (rest *REST) users(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (rest *REST) usersRegister(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(r.URL.Path, rest.scope(r))
|
||||
http.Error(w, "not impl", http.StatusNotImplemented)
|
||||
err := auth.Register(rest.g, r)
|
||||
if err != nil {
|
||||
rest.respError(w, err)
|
||||
return
|
||||
}
|
||||
rest.respOK(w)
|
||||
}
|
||||
|
||||
func (rest *REST) usersLogin(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(r.URL.Path, rest.scope(r))
|
||||
http.Error(w, "not impl", http.StatusNotImplemented)
|
||||
salt := uuid.New().String()
|
||||
var token string
|
||||
var err error
|
||||
switch config.New().Auth {
|
||||
case true:
|
||||
token, err = auth.Generate(rest.g, r, salt)
|
||||
case false:
|
||||
token, err = auth.GeneratePlain(rest.g, r)
|
||||
}
|
||||
if err != nil {
|
||||
rest.respError(w, err)
|
||||
return
|
||||
}
|
||||
log.Println("TODO cookie or body?")
|
||||
rest.respMap(w, "ok", map[string]string{
|
||||
"token": token,
|
||||
"salt": salt,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package server
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUsers(t *testing.T) {
|
||||
t.Fatal("not impl")
|
||||
}
|
||||
Loading…
Reference in New Issue