cleanup and cache pubkeys

This commit is contained in:
Ted Unangst 2019-10-30 03:31:16 -04:00
parent 75ec971004
commit df1b5af40a
1 changed files with 39 additions and 58 deletions

55
fun.go
View File

@ -27,7 +27,6 @@ import (
"os"
"regexp"
"strings"
"sync"
"golang.org/x/net/html"
"humungus.tedunangst.com/r/webs/cache"
@ -585,25 +584,22 @@ func ziggy(userid int64) *KeyInfo {
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 {
_, key, err := httpsig.DecodeKey(data)
if err != nil {
log.Printf("error decoding %s pubkey: %s", keyname, err)
}
return key, true
}
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
return nil, true
}
keyobj, ok := j.GetMap("publicKey")
if ok {
@ -612,44 +608,29 @@ func zaggy(keyname string) (key *rsa.PublicKey) {
data, ok = j.GetString("publicKeyPem")
if !ok {
log.Printf("error finding %s pubkey", keyname)
return
return nil, true
}
_, ok = j.GetString("owner")
if !ok {
log.Printf("error finding %s pubkey owner", keyname)
return
return nil, true
}
_, key, err = httpsig.DecodeKey(data)
_, key, err := httpsig.DecodeKey(data)
if err != nil {
log.Printf("error decoding %s pubkey: %s", keyname, err)
return
return nil, true
}
_, 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
}
}
zaggylock.Lock()
zaggies[keyname] = key
zaggylock.Unlock()
return
}
return key, true
}})
func makeitworksomehowwithoutregardforkeycontinuity(keyname string, r *http.Request, payload []byte) (string, error) {
_, err := stmtDeleteXonker.Exec(keyname, "pubkey")
if err != nil {
log.Printf("error deleting key: %s", err)
}
zaggylock.Lock()
delete(zaggies, keyname)
zaggylock.Unlock()
return httpsig.VerifyRequest(r, payload, zaggy)
func zaggy(keyname string) *rsa.PublicKey {
var key *rsa.PublicKey
zaggies.Get(keyname, &key)
return key
}
func keymatch(keyname string, actor string) string {