all repos — honk @ 4468d00cd7d00a2d43b61cd34e946b8bfc280192

my fork of honk

cleanup and cache pubkeys
Ted Unangst tedu@tedunangst.com
Wed, 30 Oct 2019 03:31:16 -0400
commit

4468d00cd7d00a2d43b61cd34e946b8bfc280192

parent

a8ba88aaf9a3f729638ed8bb35538139a0e01aa6

1 files changed, 38 insertions(+), 57 deletions(-)

jump to
M fun.gofun.go

@@ -27,7 +27,6 @@ "net/http"

"os" "regexp" "strings" - "sync" "golang.org/x/net/html" "humungus.tedunangst.com/r/webs/cache"

@@ -585,71 +584,53 @@ ziggies.Get(userid, &ki)

return ki } -var zaggies = make(map[string]*rsa.PublicKey) -var zaggylock sync.Mutex - -func zaggy(keyname string) (key *rsa.PublicKey) { - zaggylock.Lock() - key = zaggies[keyname] - zaggylock.Unlock() - if key != nil { - return - } +var zaggies = cache.New(cache.Options{Filler: func(keyname string) (*rsa.PublicKey, bool) { row := stmtGetXonker.QueryRow(keyname, "pubkey") var data string err := row.Scan(&data) - if err != nil { - log.Printf("hitting the webs for missing pubkey: %s", keyname) - j, err := GetJunk(keyname) - if err != nil { - log.Printf("error getting %s pubkey: %s", keyname, err) - return - } - keyobj, ok := j.GetMap("publicKey") - if ok { - j = keyobj - } - data, ok = j.GetString("publicKeyPem") - if !ok { - log.Printf("error finding %s pubkey", keyname) - return - } - _, ok = j.GetString("owner") - if !ok { - log.Printf("error finding %s pubkey owner", keyname) - return - } - _, key, err = httpsig.DecodeKey(data) + if err == nil { + _, key, err := httpsig.DecodeKey(data) if err != nil { log.Printf("error decoding %s pubkey: %s", keyname, err) - return } - _, err = stmtSaveXonker.Exec(keyname, data, "pubkey") - if err != nil { - log.Printf("error saving key: %s", err) - } - } else { - _, key, err = httpsig.DecodeKey(data) - if err != nil { - log.Printf("error decoding %s pubkey: %s", keyname, err) - return - } + return key, true } - zaggylock.Lock() - zaggies[keyname] = key - zaggylock.Unlock() - return -} - -func makeitworksomehowwithoutregardforkeycontinuity(keyname string, r *http.Request, payload []byte) (string, error) { - _, err := stmtDeleteXonker.Exec(keyname, "pubkey") + log.Printf("hitting the webs for missing pubkey: %s", keyname) + j, err := GetJunk(keyname) + if err != nil { + log.Printf("error getting %s pubkey: %s", keyname, err) + return nil, true + } + keyobj, ok := j.GetMap("publicKey") + if ok { + j = keyobj + } + data, ok = j.GetString("publicKeyPem") + if !ok { + log.Printf("error finding %s pubkey", keyname) + return nil, true + } + _, ok = j.GetString("owner") + if !ok { + log.Printf("error finding %s pubkey owner", keyname) + return nil, true + } + _, key, err := httpsig.DecodeKey(data) + if err != nil { + log.Printf("error decoding %s pubkey: %s", keyname, err) + return nil, true + } + _, err = stmtSaveXonker.Exec(keyname, data, "pubkey") if err != nil { - log.Printf("error deleting key: %s", err) + log.Printf("error saving key: %s", err) } - zaggylock.Lock() - delete(zaggies, keyname) - zaggylock.Unlock() - return httpsig.VerifyRequest(r, payload, zaggy) + return key, true +}}) + +func zaggy(keyname string) *rsa.PublicKey { + var key *rsa.PublicKey + zaggies.Get(keyname, &key) + return key } func keymatch(keyname string, actor string) string {