diff --git a/appview/db/db.go b/appview/db/db.go index 86dfc33b..da24dc4a 100644 --- a/appview/db/db.go +++ b/appview/db/db.go @@ -25,14 +25,14 @@ func Make(dbPath string) (*DB, error) { domain text not null unique, did text not null, secret text not null, - created timestamp default current_timestamp, - registered timestamp); + created integer default (strftime('%s', 'now')), + registered integer); create table if not exists public_keys ( id integer primary key autoincrement, did text not null, name text not null, key text not null, - created timestamp default current_timestamp, + created integer default (strftime('%s', 'now')), unique(did, name, key) ); `) diff --git a/appview/pages/knot.html b/appview/pages/knot.html index c58c2981..32d6916b 100644 --- a/appview/pages/knot.html +++ b/appview/pages/knot.html @@ -1,4 +1,4 @@ -{{define "title"}}knot{{end}} +{{define "title"}}{{ .Registration.Domain }}{{end}} {{define "content"}} back to timeline diff --git a/appview/state/signer.go b/appview/state/signer.go new file mode 100644 index 00000000..8a24f175 --- /dev/null +++ b/appview/state/signer.go @@ -0,0 +1,33 @@ +package state + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "net/http" + "time" +) + +type SignerTransport struct { + Secret string +} + +func SignedClient(secret string) *http.Client { + return &http.Client{ + Timeout: 5 * time.Second, + Transport: SignerTransport{ + Secret: secret, + }, + } +} + +func (s SignerTransport) RoundTrip(req *http.Request) (*http.Response, error) { + timestamp := time.Now().Format(time.RFC3339) + mac := hmac.New(sha256.New, []byte(s.Secret)) + message := req.Method + req.URL.Path + timestamp + mac.Write([]byte(message)) + signature := hex.EncodeToString(mac.Sum(nil)) + req.Header.Set("X-Signature", signature) + req.Header.Set("X-Timestamp", timestamp) + return http.DefaultTransport.RoundTrip(req) +} diff --git a/appview/state/state.go b/appview/state/state.go index cf2df2f3..4d84789c 100644 --- a/appview/state/state.go +++ b/appview/state/state.go @@ -212,15 +212,14 @@ func (s *State) InitKnotServer(w http.ResponseWriter, r *http.Request) { // make a request do the knotserver with an empty body and above signature url := fmt.Sprintf("http://%s/health", domain) - pingRequest, err := buildPingRequest(url, secret) + pingRequest, err := http.NewRequest("GET", url, nil) if err != nil { log.Println("failed to build ping request", err) return } - client := &http.Client{ - Timeout: 5 * time.Second, - } + client := SignedClient(secret) + resp, err := client.Do(pingRequest) if err != nil { w.Write([]byte("no dice")) @@ -389,23 +388,23 @@ func (s *State) AddMember(w http.ResponseWriter, r *http.Request) { func (s *State) RemoveMember(w http.ResponseWriter, r *http.Request) { } -func buildPingRequest(url, secret string) (*http.Request, error) { - pingRequest, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - - timestamp := time.Now().Format(time.RFC3339) - mac := hmac.New(sha256.New, []byte(secret)) - message := pingRequest.Method + pingRequest.URL.Path + timestamp - mac.Write([]byte(message)) - signature := hex.EncodeToString(mac.Sum(nil)) - - pingRequest.Header.Set("X-Signature", signature) - pingRequest.Header.Set("X-Timestamp", timestamp) - - return pingRequest, nil -} +// func buildPingRequest(url, secret string) (*http.Request, error) { +// pingRequest, err := http.NewRequest("GET", url, nil) +// if err != nil { +// return nil, err +// } +// +// timestamp := time.Now().Format(time.RFC3339) +// mac := hmac.New(sha256.New, []byte(secret)) +// message := pingRequest.Method + pingRequest.URL.Path + timestamp +// mac.Write([]byte(message)) +// signature := hex.EncodeToString(mac.Sum(nil)) +// +// pingRequest.Header.Set("X-Signature", signature) +// pingRequest.Header.Set("X-Timestamp", timestamp) +// +// return pingRequest, nil +// } func (s *State) Router() http.Handler { r := chi.NewRouter()