From 169a1efdabcc4eb5d117254752ec535bd9eb1632 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 2 Feb 2025 18:11:42 +0000 Subject: [PATCH] add knot mgmt page --- appview/db/db.go | 125 ++++++++++++++++++++++++++---------- appview/pages/knots.html | 34 ++++++++++ appview/pages/pages.go | 9 +++ appview/pages/timeline.html | 1 + appview/state/state.go | 42 ++++++------ 5 files changed, 158 insertions(+), 53 deletions(-) create mode 100644 appview/pages/knots.html diff --git a/appview/db/db.go b/appview/db/db.go index dffd447f..b756439c 100644 --- a/appview/db/db.go +++ b/appview/db/db.go @@ -4,9 +4,9 @@ import ( "database/sql" "fmt" "log" + "time" "github.com/google/uuid" - _ "github.com/mattn/go-sqlite3" ) @@ -25,8 +25,8 @@ func Make(dbPath string) (*DB, error) { domain text not null unique, did text not null, secret text not null, - created integer default (strftime('%s', 'now')), - registered integer); + created timestamp default current_timestamp, + registered timestamp); create table if not exists public_keys ( id integer primary key autoincrement, did text not null, @@ -42,54 +42,114 @@ func Make(dbPath string) (*DB, error) { return &DB{db: db}, nil } -type RegStatus uint32 +type Registration struct { + Domain string + ByDid string + Created *time.Time + Registered *time.Time +} + +func (r *Registration) Status() Status { + if r.Registered != nil { + return Registered + } else { + return Pending + } +} + +type Status uint32 const ( - Registered RegStatus = iota - Unregistered + Registered Status = iota Pending ) // returns registered status, did of owner, error -func (d *DB) RegistrationStatus(domain string) (RegStatus, string, error) { - var registeredBy string - var registratedAt *uint64 +func (d *DB) RegistrationsByDid(did string) ([]Registration, error) { + var registrations []Registration + + rows, err := d.db.Query(` + select domain, did, created, registered from registrations + where did = ? + `, did) + if err != nil { + return nil, err + } + + for rows.Next() { + var createdAt *int64 + var registeredAt *int64 + var registration Registration + err = rows.Scan(®istration.Domain, ®istration.ByDid, &createdAt, ®isteredAt) + + if err != nil { + log.Println(err) + } else { + createdAtTime := time.Unix(*createdAt, 0) + + var registeredAtTime *time.Time + if registeredAt != nil { + x := time.Unix(*registeredAt, 0) + registeredAtTime = &x + } + + registration.Created = &createdAtTime + registration.Registered = registeredAtTime + registrations = append(registrations, registration) + } + } + + return registrations, nil +} + +// returns registered status, did of owner, error +func (d *DB) RegistrationByDomain(domain string) (*Registration, error) { + var createdAt *int64 + var registeredAt *int64 + var registration Registration err := d.db.QueryRow(` - select did, registered from registrations + select domain, did, created, registered from registrations where domain = ? - `, domain).Scan(®isteredBy, ®istratedAt) + `, domain).Scan(®istration.Domain, ®istration.ByDid, &createdAt, ®isteredAt) + + createdAtTime := time.Unix(*createdAt, 0) + var registeredAtTime *time.Time + if registeredAt != nil { + x := time.Unix(*registeredAt, 0) + registeredAtTime = &x + } + + registration.Created = &createdAtTime + registration.Registered = registeredAtTime + if err != nil { if err == sql.ErrNoRows { - return Unregistered, "", nil + return nil, nil } else { - return Unregistered, "", err + return nil, err } } - if registratedAt != nil { - return Registered, registeredBy, nil - } else { - return Pending, registeredBy, nil - } + return ®istration, nil } func (d *DB) GenerateRegistrationKey(domain, did string) (string, error) { // sanity check: does this domain already have a registration? - status, owner, err := d.RegistrationStatus(domain) + reg, err := d.RegistrationByDomain(domain) if err != nil { return "", err } - switch status { - case Registered: - // already registered by `owner` - return "", fmt.Errorf("%s already registered by %s", domain, owner) - case Pending: - log.Printf("%s registered by %s, status pending", domain, owner) - // TODO: provide a warning here, and allow the current user to overwrite - // the registration, this prevents users from registering domains that they - // do not own - default: - // ok, we can register this domain + + // registration is open + if reg != nil { + switch reg.Status() { + case Registered: + // already registered by `owner` + return "", fmt.Errorf("%s already registered by %s", domain, reg.ByDid) + case Pending: + // TODO: be loud about this + log.Printf("%s registered by %s, status pending", domain, reg.ByDid) + } } secret := uuid.New().String() @@ -134,8 +194,3 @@ func (d *DB) Register(domain string) error { return nil } - -// type Registration struct { -// status RegStatus -// } -// func (d *DB) RegistrationsForDid(did string) () diff --git a/appview/pages/knots.html b/appview/pages/knots.html new file mode 100644 index 00000000..1a27112c --- /dev/null +++ b/appview/pages/knots.html @@ -0,0 +1,34 @@ +{{define "title"}}knots{{end}} + +{{define "content"}} + back to timeline +

knots

+ +

register

+ put in a domain, and use the key while booting up your knotserver +
+ + + + +
+ +

existing registrations

+ +{{end}} diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 795d09a1..15d039e3 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -57,3 +57,12 @@ type SettingsParams struct { func Settings(w io.Writer, p SettingsParams) error { return parse("settings.html").Execute(w, p) } + +type KnotsParams struct { + User *auth.User + Registrations []db.Registration +} + +func Knots(w io.Writer, p KnotsParams) error { + return parse("knots.html").Execute(w, p) +} diff --git a/appview/pages/timeline.html b/appview/pages/timeline.html index fc6ab79f..d7529520 100644 --- a/appview/pages/timeline.html +++ b/appview/pages/timeline.html @@ -6,5 +6,6 @@ {{ if .User }}

logged in as {{ .User.Handle }}

settings + knots {{ end }} {{end}} diff --git a/appview/state/state.go b/appview/state/state.go index 63239277..d298d94f 100644 --- a/appview/state/state.go +++ b/appview/state/state.go @@ -87,7 +87,6 @@ func (s *State) Login(w http.ResponseWriter, r *http.Request) { func (s *State) Timeline(w http.ResponseWriter, r *http.Request) { user := s.auth.GetUser(r) - fmt.Printf("%+v\n", user) pages.Timeline(w, pages.TimelineParams{ User: user, }) @@ -142,8 +141,6 @@ func (s *State) Settings(w http.ResponseWriter, r *http.Request) { User: user, PubKeys: pubKeys, }) - return - } func (s *State) Keys(w http.ResponseWriter, r *http.Request) { @@ -194,12 +191,10 @@ func (s *State) Keys(w http.ResponseWriter, r *http.Request) { } // create a signed request and check if a node responds to that -// -// we should also rate limit these checks to avoid ddosing knotservers -func (s *State) Check(w http.ResponseWriter, r *http.Request) { - domain := r.FormValue("domain") +func (s *State) InitKnotServer(w http.ResponseWriter, r *http.Request) { + domain := chi.URLParam(r, "domain") if domain == "" { - http.Error(w, "Invalid form", http.StatusBadRequest) + http.Error(w, "malformed url", http.StatusBadRequest) return } @@ -261,7 +256,7 @@ func (s *State) Check(w http.ResponseWriter, r *http.Request) { } // set permissions for this did as owner - _, did, err := s.db.RegistrationStatus(domain) + reg, err := s.db.RegistrationByDomain(domain) if err != nil { log.Println("failed to register domain", err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -277,7 +272,7 @@ func (s *State) Check(w http.ResponseWriter, r *http.Request) { } // add this did as owner of this domain - err = s.enforcer.AddOwner(domain, did) + err = s.enforcer.AddOwner(domain, reg.ByDid) if err != nil { log.Println("failed to setup owner of domain", err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -285,8 +280,21 @@ func (s *State) Check(w http.ResponseWriter, r *http.Request) { } w.Write([]byte("check success")) +} - return +// get knots registered by this user +func (s *State) Knots(w http.ResponseWriter, r *http.Request) { + // for now, this is just pubkeys + user := s.auth.GetUser(r) + registrations, err := s.db.RegistrationsByDid(user.Did) + if err != nil { + log.Println(err) + } + + pages.Knots(w, pages.KnotsParams{ + User: user, + Registrations: registrations, + }) } func buildPingRequest(url, secret string) (*http.Request, error) { @@ -315,13 +323,11 @@ func (s *State) Router() http.Handler { r.Get("/login", s.Login) r.Post("/login", s.Login) - r.Route("/node", func(r chi.Router) { - r.Post("/check", s.Check) - - r.Group(func(r chi.Router) { - r.Use(AuthMiddleware(s)) - r.Post("/key", s.RegistrationKey) - }) + r.Route("/knots", func(r chi.Router) { + r.Use(AuthMiddleware(s)) + r.Get("/", s.Knots) + r.Post("/init/{domain}", s.InitKnotServer) + r.Post("/key", s.RegistrationKey) }) r.Group(func(r chi.Router) { -- knotmirror 0.0.1