forked from tangled.org/core
this repo has no description

use epoch times everywhere

use custom roundtripper for hmac signing

Changed files
+57 -25
appview
+3 -3
appview/db/db.go
···
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)
);
`)
+1 -1
appview/pages/knot.html
···
-
{{define "title"}}knot{{end}}
+
{{define "title"}}{{ .Registration.Domain }}{{end}}
{{define "content"}}
<a href="/">back to timeline</a>
+33
appview/state/signer.go
···
+
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)
+
}
+20 -21
appview/state/state.go
···
// 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"))
···
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()