-
Notifications
You must be signed in to change notification settings - Fork 31
/
keybase_cache.go
executable file
·57 lines (47 loc) · 1004 Bytes
/
keybase_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package auth
import (
"errors"
"net/http"
"sync"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/dex-demo/embedded/session"
)
var kb *Keybase
var currID string
var mtx sync.RWMutex
func GetKBFromSession(r *http.Request) (*Keybase, error) {
id, err := session.GetStr(r, keybaseIDKey)
if err != nil {
return nil, err
}
kb := GetKB(id)
if kb == nil {
return nil, errors.New("no keybase found")
}
return kb, nil
}
func MustGetKBFromSession(r *http.Request) *Keybase {
kb, err := GetKBFromSession(r)
if err != nil {
panic(err)
}
return kb
}
func MustGetKBPassphraseFromSession(r *http.Request) string {
return session.MustGetStr(r, keybasePassphraseKey)
}
func GetKB(id string) *Keybase {
mtx.RLock()
defer mtx.RUnlock()
if currID != id {
return nil
}
return kb
}
func ReplaceKB(name string, passphrase string, pk crypto.PrivKey) string {
mtx.Lock()
defer mtx.Unlock()
currID = ReadStr32()
kb = NewHotKeybase(name, passphrase, pk)
return currID
}