/
cache.go
65 lines (51 loc) · 1.51 KB
/
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
58
59
60
61
62
63
64
65
package oidcceremony
import (
"time"
"github.com/globalsign/mgo/bson"
"go.aporeto.io/manipulate"
"go.aporeto.io/manipulate/manipmongo"
"golang.org/x/oauth2"
)
const oidcCacheCollection = "oidccache"
// CacheItem represents a cache OIDC request info.
type CacheItem struct {
State string `bson:"state"`
ClientID string `bson:"clientid"`
CA string `bson:"ca"`
OAuth2Config oauth2.Config `bson:"oauth2config"`
ProviderEndpoint string `bson:"providerEndpoint"`
Time time.Time `bson:"time"`
}
// Set sets the given OIDCRequestItem in redis.
func Set(m manipulate.Manipulator, item *CacheItem) error {
item.Time = time.Now()
db, disco, err := manipmongo.GetDatabase(m)
if err != nil {
return err
}
defer disco()
return db.C(oidcCacheCollection).Insert(item)
}
// Get gets the items with the given state.
// If none is found, it will return nil.
func Get(m manipulate.Manipulator, state string) (*CacheItem, error) {
db, disco, err := manipmongo.GetDatabase(m)
if err != nil {
return nil, err
}
defer disco()
item := &CacheItem{}
if err := db.C(oidcCacheCollection).Find(bson.M{"state": state}).One(item); err != nil {
return nil, err
}
return item, nil
}
// Delete deletes the items with the given state.
func Delete(m manipulate.Manipulator, state string) error {
db, disco, err := manipmongo.GetDatabase(m)
if err != nil {
return err
}
defer disco()
return db.C(oidcCacheCollection).Remove(bson.M{"state": state})
}