-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
98 lines (83 loc) · 2.57 KB
/
storage.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
package teststorage
import (
"errors"
"github.com/RangelReale/osin"
)
type Test struct {
Clients map[string]osin.Client
AuthorizeData *osin.AuthorizeData
Authorize map[string]*osin.AuthorizeData
AccessData *osin.AccessData
Access map[string]*osin.AccessData
Err error
}
func New() *Test {
return &Test{
Clients: make(map[string]osin.Client),
Authorize: make(map[string]*osin.AuthorizeData),
Access: make(map[string]*osin.AccessData),
}
}
func (t *Test) Clone() osin.Storage {
return t
}
func (t *Test) Close() {
}
// GetClient loads the client by id (client_id)
func (t *Test) GetClient(id string) (osin.Client, error) {
return t.Clients[id], t.Err
}
// SaveAuthorize saves authorize data.
func (t *Test) SaveAuthorize(data *osin.AuthorizeData) error {
t.AuthorizeData = data
t.Authorize[data.Code] = data
return t.Err
}
// LoadAuthorize looks up AuthorizeData by a code.
// Client information MUST be loaded together.
// Optionally can return error if expired.
func (t *Test) LoadAuthorize(code string) (*osin.AuthorizeData, error) {
return t.Authorize[code], t.Err
}
// RemoveAuthorize revokes or deletes the authorization code.
func (t *Test) RemoveAuthorize(code string) error {
delete(t.Authorize, code)
return t.Err
}
// SaveAccess writes AccessData.
// If RefreshToken is not blank, it must save in a way that can be loaded using LoadRefresh.
func (t *Test) SaveAccess(data *osin.AccessData) error {
t.AccessData = data
t.Access[data.AccessToken] = data
return t.Err
}
// LoadAccess retrieves access data by token. Client information MUST be loaded together.
// AuthorizeData and AccessData DON'T NEED to be loaded if not easily available.
// Optionally can return error if expired.
func (t *Test) LoadAccess(token string) (*osin.AccessData, error) {
return t.Access[token], t.Err
}
// RemoveAccess revokes or deletes an AccessData.
func (t *Test) RemoveAccess(token string) error {
delete(t.Access, token)
return t.Err
}
// LoadRefresh retrieves refresh AccessData. Client information MUST be loaded together.
// AuthorizeData and AccessData DON'T NEED to be loaded if not easily available.
// Optionally can return error if expired.
func (t *Test) LoadRefresh(token string) (*osin.AccessData, error) {
for _, v := range t.Access {
if v.RefreshToken == token {
return v, t.Err
}
}
return nil, errors.New("not found")
}
// RemoveRefresh revokes or deletes refresh AccessData.
func (t *Test) RemoveRefresh(token string) error {
data, _ := t.LoadRefresh(token)
if data != nil {
delete(t.Access, data.AccessToken)
}
return t.Err
}