-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage_test.go
More file actions
133 lines (109 loc) · 3.17 KB
/
Copy pathstorage_test.go
File metadata and controls
133 lines (109 loc) · 3.17 KB
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
package niso
import (
"context"
"errors"
"strconv"
"time"
)
type TestingStorage struct {
clients map[string]*ClientData
authorize map[string]*AuthorizationData
access map[string]*AccessData
refresh map[string]*RefreshTokenData
}
func NewTestingStorage() *TestingStorage {
r := &TestingStorage{
clients: make(map[string]*ClientData),
authorize: make(map[string]*AuthorizationData),
access: make(map[string]*AccessData),
refresh: make(map[string]*RefreshTokenData),
}
r.clients["1234"] = &ClientData{
ClientID: "1234",
ClientSecret: "aabbccdd",
RedirectURI: "http://localhost:14000/appauth",
}
r.clients["public-client"] = &ClientData{
ClientID: "public-client",
RedirectURI: "http://localhost:14000/appauth",
}
r.authorize["9999"] = &AuthorizationData{
ClientID: "1234",
Code: "9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
RedirectURI: "http://localhost:14000/appauth",
}
r.access["9999"] = &AccessData{
ClientID: "1234",
AccessToken: "9999",
ExpiresIn: 3600,
CreatedAt: time.Now(),
}
r.refresh["r9999"] = &RefreshTokenData{
ClientID: "1234",
RefreshToken: "9999",
}
return r
}
func (s *TestingStorage) Close() error {
return nil
}
func (s *TestingStorage) GetClientData(_ context.Context, id string) (*ClientData, error) {
if c, ok := s.clients[id]; ok {
return c, nil
}
return nil, &NotFoundError{Err: errors.New("client not found")}
}
func (s *TestingStorage) SaveAuthorizeData(_ context.Context, data *AuthorizationData) error {
s.authorize[data.Code] = data
return nil
}
func (s *TestingStorage) GetAuthorizeData(_ context.Context, code string) (*AuthorizationData, error) {
if d, ok := s.authorize[code]; ok {
return d, nil
}
return nil, errors.New("authorize not found")
}
func (s *TestingStorage) DeleteAuthorizeData(ctx context.Context, code string) error {
delete(s.authorize, code)
return nil
}
func (s *TestingStorage) SaveAccessData(ctx context.Context, data *AccessData) error {
s.access[data.AccessToken] = data
return nil
}
func (s *TestingStorage) GetRefreshTokenData(ctx context.Context, token string) (*RefreshTokenData, error) {
if d, ok := s.refresh[token]; ok {
return d, nil
}
return nil, errors.New("refresh token data not found")
}
func (s *TestingStorage) SaveRefreshTokenData(ctx context.Context, data *RefreshTokenData) error {
s.refresh[data.RefreshToken] = data
return nil
}
func (s *TestingStorage) DeleteRefreshTokenData(ctx context.Context, token string) error {
delete(s.refresh, token)
return nil
}
// Predictable testing token generation
type TestingAuthorizeTokenGen struct {
counter int64
}
func (a *TestingAuthorizeTokenGen) GenerateAuthorizeToken(data *AuthorizationData) (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) (string, error) {
a.acounter++
return strconv.FormatInt(a.acounter, 10), nil
}
func (a *TestingAccessTokenGen) GenerateRefreshToken(data *RefreshTokenData) (string, error) {
a.rcounter++
return "r" + strconv.FormatInt(a.rcounter, 10), nil
}