router and auth kk
parent
1a06d9634b
commit
c145bd9daf
2
main.go
2
main.go
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"local/dndex/config"
|
"local/dndex/config"
|
||||||
|
"local/dndex/server"
|
||||||
"local/dndex/storage"
|
"local/dndex/storage"
|
||||||
"local/dndex/view"
|
"local/dndex/view"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -14,6 +15,7 @@ func main() {
|
||||||
log.Println(c)
|
log.Println(c)
|
||||||
g := storage.NewRateLimitedGraph()
|
g := storage.NewRateLimitedGraph()
|
||||||
view.GitCommit = GitCommit
|
view.GitCommit = GitCommit
|
||||||
|
server.GitCommit = GitCommit
|
||||||
if err := view.JSON(g); err != nil {
|
if err := view.JSON(g); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"local/dndex/storage"
|
"local/dndex/storage"
|
||||||
"local/dndex/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -27,6 +28,9 @@ func Generate(g storage.RateLimitedGraph, r *http.Request, salt string) (string,
|
||||||
|
|
||||||
func generateToken(g storage.RateLimitedGraph, r *http.Request) (Token, string, error) {
|
func generateToken(g storage.RateLimitedGraph, r *http.Request) (Token, string, error) {
|
||||||
namespaceRequested := readRequestedNamespace(r)
|
namespaceRequested := readRequestedNamespace(r)
|
||||||
|
if namespaceRequested == "" {
|
||||||
|
return Token{}, "", errors.New("no namespace found")
|
||||||
|
}
|
||||||
key, err := getKeyForNamespace(r.Context(), g, namespaceRequested)
|
key, err := getKeyForNamespace(r.Context(), g, namespaceRequested)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Token{}, "", err
|
return Token{}, "", err
|
||||||
|
|
|
||||||
|
|
@ -47,3 +47,14 @@ func (rest *REST) auth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
foo(w, r)
|
foo(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rest *REST) shift(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
i := 1
|
||||||
|
for i < len(r.URL.Path) && r.URL.Path[i] != '/' {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
r.URL.Path = r.URL.Path[i:]
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"local/dndex/server/auth"
|
"local/dndex/server/auth"
|
||||||
"local/dndex/storage"
|
"local/dndex/storage"
|
||||||
"local/router"
|
"local/router"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -36,13 +37,14 @@ func NewREST(g storage.RateLimitedGraph) (*REST, error) {
|
||||||
|
|
||||||
paths := map[string]http.HandlerFunc{
|
paths := map[string]http.HandlerFunc{
|
||||||
fmt.Sprintf("version"): rest.version,
|
fmt.Sprintf("version"): rest.version,
|
||||||
fmt.Sprintf("files/%s/%s", config.New().FilePrefix, params): rest.files,
|
fmt.Sprintf("%s/%s", config.New().FilePrefix, params): rest.files,
|
||||||
fmt.Sprintf("users"): rest.users,
|
fmt.Sprintf("users"): rest.users,
|
||||||
fmt.Sprintf("entities/%s", params): rest.entities,
|
fmt.Sprintf("entities/%s", params): rest.entities,
|
||||||
}
|
}
|
||||||
|
|
||||||
for path, foo := range paths {
|
for path, foo := range paths {
|
||||||
bar := foo
|
bar := foo
|
||||||
|
bar = rest.shift(bar)
|
||||||
bar = rest.auth(bar)
|
bar = rest.auth(bar)
|
||||||
bar = rest.defend(bar)
|
bar = rest.defend(bar)
|
||||||
bar = rest.delay(bar)
|
bar = rest.delay(bar)
|
||||||
|
|
@ -58,14 +60,17 @@ func (rest *REST) scope(r *http.Request) auth.Scope {
|
||||||
return auth.GetScope(r)
|
return auth.GetScope(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rest *REST) files(w http.ResponseWriter, _ *http.Request) {
|
func (rest *REST) files(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("files: SCOPE:", rest.scope(r), r.URL.Path)
|
||||||
http.Error(w, "not impl", http.StatusNotImplemented)
|
http.Error(w, "not impl", http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rest *REST) users(w http.ResponseWriter, _ *http.Request) {
|
func (rest *REST) users(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("users: SCOPE:", rest.scope(r), r.URL.Path)
|
||||||
http.Error(w, "not impl", http.StatusNotImplemented)
|
http.Error(w, "not impl", http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rest *REST) entities(w http.ResponseWriter, _ *http.Request) {
|
func (rest *REST) entities(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("entities: SCOPE:", rest.scope(r), r.URL.Path)
|
||||||
http.Error(w, "not impl", http.StatusNotImplemented)
|
http.Error(w, "not impl", http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"local/dndex/config"
|
"local/dndex/config"
|
||||||
|
"local/dndex/server/auth"
|
||||||
"local/dndex/storage"
|
"local/dndex/storage"
|
||||||
"local/dndex/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -19,8 +20,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testNamespaceName = "col-name-" + uuid.New().String()[:10]
|
testNamespace = "col-" + uuid.New().String()[:10]
|
||||||
testNamespaceID = "col-id-" + uuid.New().String()[:10]
|
|
||||||
testEntityName = "ent-name-" + uuid.New().String()[:10]
|
testEntityName = "ent-name-" + uuid.New().String()[:10]
|
||||||
testEntityID = "ent-id-" + uuid.New().String()[:10]
|
testEntityID = "ent-id-" + uuid.New().String()[:10]
|
||||||
testFilename = "filename-" + uuid.New().String()[:10]
|
testFilename = "filename-" + uuid.New().String()[:10]
|
||||||
|
|
@ -28,7 +28,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRESTRouter(t *testing.T) {
|
func TestRESTRouter(t *testing.T) {
|
||||||
rest, clean := testREST(t)
|
rest, setAuth, clean := testREST(t)
|
||||||
defer clean()
|
defer clean()
|
||||||
cases := map[string]struct {
|
cases := map[string]struct {
|
||||||
method string
|
method string
|
||||||
|
|
@ -52,11 +52,11 @@ func TestRESTRouter(t *testing.T) {
|
||||||
method: http.MethodGet,
|
method: http.MethodGet,
|
||||||
is404: true,
|
is404: true,
|
||||||
},
|
},
|
||||||
fmt.Sprintf("/users/%s", testNamespaceID): {
|
fmt.Sprintf("/users/%s", testNamespace): {
|
||||||
method: http.MethodGet,
|
method: http.MethodGet,
|
||||||
is404: true,
|
is404: true,
|
||||||
},
|
},
|
||||||
fmt.Sprintf("/users/%s", testNamespaceID): {
|
fmt.Sprintf("/users/%s", testNamespace): {
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
is404: true,
|
is404: true,
|
||||||
},
|
},
|
||||||
|
|
@ -148,6 +148,7 @@ func TestRESTRouter(t *testing.T) {
|
||||||
path := name
|
path := name
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
r := httptest.NewRequest(c.method, path, strings.NewReader(``))
|
r := httptest.NewRequest(c.method, path, strings.NewReader(``))
|
||||||
|
setAuth(r)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
rest.router.ServeHTTP(w, r)
|
rest.router.ServeHTTP(w, r)
|
||||||
if (w.Code == http.StatusNotFound) != c.is404 {
|
if (w.Code == http.StatusNotFound) != c.is404 {
|
||||||
|
|
@ -157,7 +158,7 @@ func TestRESTRouter(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testREST(t *testing.T) (*REST, func()) {
|
func testREST(t *testing.T) (*REST, func(r *http.Request), func()) {
|
||||||
d, err := ioutil.TempDir(os.TempDir(), "tempdir.*")
|
d, err := ioutil.TempDir(os.TempDir(), "tempdir.*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -179,24 +180,23 @@ func testREST(t *testing.T) (*REST, func()) {
|
||||||
one := randomOne()
|
one := randomOne()
|
||||||
one.Name = testEntityName
|
one.Name = testEntityName
|
||||||
one.ID = testEntityID
|
one.ID = testEntityID
|
||||||
if err := rest.g.Insert(ctx, testNamespaceID, one); err != nil {
|
if err := rest.g.Insert(ctx, testNamespace, one); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := rest.g.Insert(ctx, testNamespaceID+"."+AuthKey, entity.One{
|
register := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(fmt.Sprintf("%s=%s&%s=%s", auth.UserKey, testNamespace, auth.AuthKey, "password")))
|
||||||
Name: testNamespaceName,
|
register.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
ID: testNamespaceID,
|
if err := auth.Register(rest.g, register); err != nil {
|
||||||
Title: "title",
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := rest.g.Insert(ctx, testNamespaceID+"."+AuthKey, entity.One{
|
login := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(auth.UserKey+"="+testNamespace))
|
||||||
Name: UserKey,
|
login.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
ID: UserKey,
|
token, err := auth.GeneratePlain(rest.g, login)
|
||||||
Title: "",
|
if err != nil {
|
||||||
}); err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return rest, func() {
|
return rest, func(r *http.Request) {
|
||||||
|
r.AddCookie(&http.Cookie{Name: auth.AuthKey, Value: token})
|
||||||
|
}, func() {
|
||||||
os.RemoveAll(d)
|
os.RemoveAll(d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue