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

97
fun.go
View File

@ -27,7 +27,6 @@ import (
"os" "os"
"regexp" "regexp"
"strings" "strings"
"sync"
"golang.org/x/net/html" "golang.org/x/net/html"
"humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/cache"
@ -585,71 +584,53 @@ func ziggy(userid int64) *KeyInfo {
return ki return ki
} }
var zaggies = make(map[string]*rsa.PublicKey) var zaggies = cache.New(cache.Options{Filler: func(keyname string) (*rsa.PublicKey, bool) {
var zaggylock sync.Mutex
func zaggy(keyname string) (key *rsa.PublicKey) {
zaggylock.Lock()
key = zaggies[keyname]
zaggylock.Unlock()
if key != nil {
return
}
row := stmtGetXonker.QueryRow(keyname, "pubkey") row := stmtGetXonker.QueryRow(keyname, "pubkey")
var data string var data string
err := row.Scan(&data) err := row.Scan(&data)
if err != nil { if err == nil {
log.Printf("hitting the webs for missing pubkey: %s", keyname) _, key, err := httpsig.DecodeKey(data)
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 { if err != nil {
log.Printf("error decoding %s pubkey: %s", keyname, err) 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() log.Printf("hitting the webs for missing pubkey: %s", keyname)
zaggies[keyname] = key j, err := GetJunk(keyname)
zaggylock.Unlock() if err != nil {
return 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 saving key: %s", err)
}
return key, true
}})
func makeitworksomehowwithoutregardforkeycontinuity(keyname string, r *http.Request, payload []byte) (string, error) { func zaggy(keyname string) *rsa.PublicKey {
_, err := stmtDeleteXonker.Exec(keyname, "pubkey") var key *rsa.PublicKey
if err != nil { zaggies.Get(keyname, &key)
log.Printf("error deleting key: %s", err) return key
}
zaggylock.Lock()
delete(zaggies, keyname)
zaggylock.Unlock()
return httpsig.VerifyRequest(r, payload, zaggy)
} }
func keymatch(keyname string, actor string) string { func keymatch(keyname string, actor string) string {