-
Notifications
You must be signed in to change notification settings - Fork 5
/
store.go
102 lines (86 loc) · 2.58 KB
/
store.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package fstore
import (
"cloud.google.com/go/firestore"
"errors"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/models"
"reflect"
"time"
)
const (
keyCode = "Code"
keyAccess = "Access"
keyRefresh = "Refresh"
timeout = 30 * time.Second
)
// New returns a new Firestore token store.
// The provided firestore client will never be closed.
func New(c *firestore.Client, collection string) oauth2.TokenStore {
return NewWithTimeout(c, collection, timeout)
}
// NewWithTimeout returns a new Firestore token store.
// The provided firestore client will never be closed and all Firestore operations will be cancelled
// if they surpass the provided timeout.
func NewWithTimeout(c *firestore.Client, collection string, timeout time.Duration) oauth2.TokenStore {
fs := &store{c: c, n: collection, t: timeout}
return &client{c: fs}
}
type client struct {
c *store
}
func (f *client) Create(info oauth2.TokenInfo) error {
t, err := token(info)
if err != nil {
return err
}
return f.c.Put(t)
}
func (f *client) RemoveByCode(code string) error {
return f.c.Del(keyCode, code)
}
func (f *client) RemoveByAccess(access string) error {
return f.c.Del(keyAccess, access)
}
func (f *client) RemoveByRefresh(refresh string) error {
return f.c.Del(keyRefresh, refresh)
}
func (f *client) GetByCode(code string) (oauth2.TokenInfo, error) {
return f.c.Get(keyCode, code)
}
func (f *client) GetByAccess(access string) (oauth2.TokenInfo, error) {
return f.c.Get(keyAccess, access)
}
func (f *client) GetByRefresh(refresh string) (oauth2.TokenInfo, error) {
return f.c.Get(keyRefresh, refresh)
}
// ErrInvalidTokenInfo is returned whenever TokenInfo is either nil or zero/empty.
var ErrInvalidTokenInfo = errors.New("invalid TokenInfo")
func token(info oauth2.TokenInfo) (*models.Token, error) {
if isNilOrZero(info) {
return nil, ErrInvalidTokenInfo
}
return &models.Token{
ClientID: info.GetClientID(),
UserID: info.GetUserID(),
RedirectURI: info.GetRedirectURI(),
Scope: info.GetScope(),
Code: info.GetCode(),
CodeCreateAt: info.GetCodeCreateAt(),
CodeExpiresIn: info.GetCodeExpiresIn(),
Access: info.GetAccess(),
AccessCreateAt: info.GetAccessCreateAt(),
AccessExpiresIn: info.GetAccessExpiresIn(),
Refresh: info.GetRefresh(),
RefreshCreateAt: info.GetRefreshCreateAt(),
RefreshExpiresIn: info.GetRefreshExpiresIn(),
}, nil
}
func isNilOrZero(info oauth2.TokenInfo) bool {
if info == nil {
return true
}
if v := reflect.ValueOf(info); v.IsNil() {
return true
}
return reflect.DeepEqual(info, info.New())
}