-
Notifications
You must be signed in to change notification settings - Fork 399
/
storage_test.go
158 lines (130 loc) · 3.4 KB
/
storage_test.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package osin
import (
"strconv"
"time"
)
type TestingStorage struct {
clients map[string]Client
authorize map[string]*AuthorizeData
access map[string]*AccessData
refresh map[string]string
}
func NewTestingStorage() *TestingStorage {
r := &TestingStorage{
clients: make(map[string]Client),
authorize: make(map[string]*AuthorizeData),
access: make(map[string]*AccessData),
refresh: make(map[string]string),
}
r.clients["1234"] = &DefaultClient{
Id: "1234",
Secret: "aabbccdd",
RedirectUri: "http://localhost:14000/appauth",
}
r.clients["public-client"] = &DefaultClient{
Id: "public-client",
RedirectUri: "http://localhost:14000/appauth",
}
r.authorize["9999"] = &AuthorizeData{
Client: r.clients["1234"],
Code: "9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
RedirectUri: "http://localhost:14000/appauth",
}
r.access["9999"] = &AccessData{
Client: r.clients["1234"],
AuthorizeData: r.authorize["9999"],
AccessToken: "9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
}
r.access["r9999"] = &AccessData{
Client: r.clients["1234"],
AuthorizeData: r.authorize["9999"],
AccessData: r.access["9999"],
AccessToken: "9999",
RefreshToken: "r9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
}
r.refresh["r9999"] = "9999"
return r
}
func (s *TestingStorage) Clone() Storage {
return s
}
func (s *TestingStorage) Close() {
}
func (s *TestingStorage) GetClient(id string) (Client, error) {
if c, ok := s.clients[id]; ok {
return c, nil
}
return nil, ErrNotFound
}
func (s *TestingStorage) SetClient(id string, client Client) error {
s.clients[id] = client
return nil
}
func (s *TestingStorage) SaveAuthorize(data *AuthorizeData) error {
s.authorize[data.Code] = data
return nil
}
func (s *TestingStorage) LoadAuthorize(code string) (*AuthorizeData, error) {
if d, ok := s.authorize[code]; ok {
return d, nil
}
return nil, ErrNotFound
}
func (s *TestingStorage) RemoveAuthorize(code string) error {
delete(s.authorize, code)
return nil
}
func (s *TestingStorage) SaveAccess(data *AccessData) error {
s.access[data.AccessToken] = data
if data.RefreshToken != "" {
s.refresh[data.RefreshToken] = data.AccessToken
}
return nil
}
func (s *TestingStorage) LoadAccess(code string) (*AccessData, error) {
if d, ok := s.access[code]; ok {
return d, nil
}
return nil, ErrNotFound
}
func (s *TestingStorage) RemoveAccess(code string) error {
delete(s.access, code)
return nil
}
func (s *TestingStorage) LoadRefresh(code string) (*AccessData, error) {
if d, ok := s.refresh[code]; ok {
return s.LoadAccess(d)
}
return nil, ErrNotFound
}
func (s *TestingStorage) RemoveRefresh(code string) error {
delete(s.refresh, code)
return nil
}
// Predictable testing token generation
type TestingAuthorizeTokenGen struct {
counter int64
}
func (a *TestingAuthorizeTokenGen) GenerateAuthorizeToken(data *AuthorizeData) (ret string, err error) {
a.counter++
return strconv.FormatInt(a.counter, 10), nil
}
type TestingAccessTokenGen struct {
acounter int64
rcounter int64
}
func (a *TestingAccessTokenGen) GenerateAccessToken(data *AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) {
a.acounter++
accesstoken = strconv.FormatInt(a.acounter, 10)
if generaterefresh {
a.rcounter++
refreshtoken = "r" + strconv.FormatInt(a.rcounter, 10)
}
return
}