dndex/server/auth/register.go

40 lines
954 B
Go

package auth
import (
"context"
"errors"
"fmt"
"local/dndex/storage"
"local/dndex/storage/entity"
"net/http"
)
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 {
return errors.New("namespace already exists")
}
return makeNamespace(r.Context(), g, namespaceRequested, keyRequested)
}
func readRequestedKey(r *http.Request) string {
return readRequested(r, AuthKey)
}
func toAuthNamespace(namespace string) string {
return fmt.Sprintf("%s.%s", namespace, AuthKey)
}
func makeNamespace(ctx context.Context, g storage.RateLimitedGraph, namespace, key string) error {
one := entity.One{
ID: UserKey,
Title: key,
}
return g.Insert(ctx, toAuthNamespace(namespace), one)
}