-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
81 lines (63 loc) · 1.94 KB
/
session.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"time"
"github.com/patrickmn/go-cache"
)
type sessionstore interface {
GetSecret(session string) ([]byte, error)
SetSecret(session string, secret []byte) error
GetUpstream(session string) (upstream *upstreamConfig, err error)
SetUpstream(session string, upstream *upstreamConfig) error
SetSshError(session string, err string) error
GetSshError(session string) (err *string)
DeleteSession(session string, keeperr bool) error
}
type sessionstoreMemory struct {
store *cache.Cache
}
func newSessionstoreMemory() (*sessionstoreMemory, error) {
return &sessionstoreMemory{
store: cache.New(1*time.Minute, 10*time.Minute),
}, nil
}
func (s *sessionstoreMemory) GetSecret(session string) ([]byte, error) {
secret, found := s.store.Get(session + "-secret")
if !found {
return nil, nil
}
return secret.([]byte), nil
}
func (s *sessionstoreMemory) SetSecret(session string, secret []byte) error {
s.store.Set(session+"-secret", secret, cache.DefaultExpiration)
return nil
}
func (s *sessionstoreMemory) GetUpstream(session string) (*upstreamConfig, error) {
upstream, found := s.store.Get(session + "-upstream")
if !found {
return nil, nil
}
return upstream.(*upstreamConfig), nil
}
func (s *sessionstoreMemory) SetUpstream(session string, upstream *upstreamConfig) error {
s.store.Set(session+"-upstream", upstream, cache.DefaultExpiration)
return nil
}
func (s *sessionstoreMemory) SetSshError(session string, err string) error {
s.store.Set(session+"-ssherror", &err, cache.DefaultExpiration)
return nil
}
func (s *sessionstoreMemory) GetSshError(session string) (err *string) {
ssherror, found := s.store.Get(session + "-ssherror")
if !found {
return nil
}
return ssherror.(*string)
}
func (s *sessionstoreMemory) DeleteSession(session string, keeperr bool) error {
s.store.Delete(session + "-secret")
s.store.Delete(session + "-upstream")
if !keeperr {
s.store.Delete(session + "-ssherror")
}
return nil
}