forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.go
206 lines (181 loc) · 6.57 KB
/
memory.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package storage
import (
"context"
"github.com/ory/fosite"
"github.com/pkg/errors"
)
type MemoryUserRelation struct {
Username string
Password string
}
type MemoryStore struct {
Clients map[string]*fosite.DefaultClient
AuthorizeCodes map[string]fosite.Requester
IDSessions map[string]fosite.Requester
AccessTokens map[string]fosite.Requester
Implicit map[string]fosite.Requester
RefreshTokens map[string]fosite.Requester
Users map[string]MemoryUserRelation
// In-memory request ID to token signatures
AccessTokenRequestIDs map[string]string
RefreshTokenRequestIDs map[string]string
}
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
Clients: make(map[string]*fosite.DefaultClient),
AuthorizeCodes: make(map[string]fosite.Requester),
IDSessions: make(map[string]fosite.Requester),
AccessTokens: make(map[string]fosite.Requester),
Implicit: make(map[string]fosite.Requester),
RefreshTokens: make(map[string]fosite.Requester),
Users: make(map[string]MemoryUserRelation),
AccessTokenRequestIDs: make(map[string]string),
RefreshTokenRequestIDs: make(map[string]string),
}
}
func NewExampleStore() *MemoryStore {
return &MemoryStore{
IDSessions: make(map[string]fosite.Requester),
Clients: map[string]*fosite.DefaultClient{
"my-client": {
ID: "my-client",
Secret: []byte(`$2a$10$IxMdI6d.LIRZPpSfEwNoeu4rY3FhDREsxFJXikcgdRRAStxUlsuEO`), // = "foobar"
RedirectURIs: []string{"http://localhost:3846/callback"},
ResponseTypes: []string{"id_token", "code", "token"},
GrantTypes: []string{"implicit", "refresh_token", "authorization_code", "password", "client_credentials"},
Scopes: []string{"fosite", "openid", "photos", "offline"},
},
},
Users: map[string]MemoryUserRelation{
"peter": {
// This store simply checks for equality, a real storage implementation would obviously use
// a hashing algorithm for encrypting the user password.
Username: "peter",
Password: "secret",
},
},
AuthorizeCodes: map[string]fosite.Requester{},
Implicit: map[string]fosite.Requester{},
AccessTokens: map[string]fosite.Requester{},
RefreshTokens: map[string]fosite.Requester{},
AccessTokenRequestIDs: map[string]string{},
RefreshTokenRequestIDs: map[string]string{},
}
}
func (s *MemoryStore) CreateOpenIDConnectSession(_ context.Context, authorizeCode string, requester fosite.Requester) error {
s.IDSessions[authorizeCode] = requester
return nil
}
func (s *MemoryStore) GetOpenIDConnectSession(_ context.Context, authorizeCode string, requester fosite.Requester) (fosite.Requester, error) {
cl, ok := s.IDSessions[authorizeCode]
if !ok {
return nil, fosite.ErrNotFound
}
return cl, nil
}
func (s *MemoryStore) DeleteOpenIDConnectSession(_ context.Context, authorizeCode string) error {
delete(s.IDSessions, authorizeCode)
return nil
}
func (s *MemoryStore) GetClient(_ context.Context, id string) (fosite.Client, error) {
cl, ok := s.Clients[id]
if !ok {
return nil, fosite.ErrNotFound
}
return cl, nil
}
func (s *MemoryStore) CreateAuthorizeCodeSession(_ context.Context, code string, req fosite.Requester) error {
s.AuthorizeCodes[code] = req
return nil
}
func (s *MemoryStore) GetAuthorizeCodeSession(_ context.Context, code string, _ fosite.Session) (fosite.Requester, error) {
rel, ok := s.AuthorizeCodes[code]
if !ok {
return nil, fosite.ErrNotFound
}
return rel, nil
}
func (s *MemoryStore) DeleteAuthorizeCodeSession(_ context.Context, code string) error {
delete(s.AuthorizeCodes, code)
return nil
}
func (s *MemoryStore) CreateAccessTokenSession(_ context.Context, signature string, req fosite.Requester) error {
s.AccessTokens[signature] = req
s.AccessTokenRequestIDs[req.GetID()] = signature
return nil
}
func (s *MemoryStore) GetAccessTokenSession(_ context.Context, signature string, _ fosite.Session) (fosite.Requester, error) {
rel, ok := s.AccessTokens[signature]
if !ok {
return nil, fosite.ErrNotFound
}
return rel, nil
}
func (s *MemoryStore) DeleteAccessTokenSession(_ context.Context, signature string) error {
delete(s.AccessTokens, signature)
return nil
}
func (s *MemoryStore) CreateRefreshTokenSession(_ context.Context, signature string, req fosite.Requester) error {
s.RefreshTokens[signature] = req
s.RefreshTokenRequestIDs[req.GetID()] = signature
return nil
}
func (s *MemoryStore) GetRefreshTokenSession(_ context.Context, signature string, _ fosite.Session) (fosite.Requester, error) {
rel, ok := s.RefreshTokens[signature]
if !ok {
return nil, fosite.ErrNotFound
}
return rel, nil
}
func (s *MemoryStore) DeleteRefreshTokenSession(_ context.Context, signature string) error {
delete(s.RefreshTokens, signature)
return nil
}
func (s *MemoryStore) CreateImplicitAccessTokenSession(_ context.Context, code string, req fosite.Requester) error {
s.Implicit[code] = req
return nil
}
func (s *MemoryStore) Authenticate(_ context.Context, name string, secret string) error {
rel, ok := s.Users[name]
if !ok {
return fosite.ErrNotFound
}
if rel.Password != secret {
return errors.New("Invalid credentials")
}
return nil
}
func (s *MemoryStore) PersistAuthorizeCodeGrantSession(ctx context.Context, authorizeCode, accessSignature, refreshSignature string, request fosite.Requester) error {
if err := s.DeleteAuthorizeCodeSession(ctx, authorizeCode); err != nil {
return err
} else if err := s.CreateAccessTokenSession(ctx, accessSignature, request); err != nil {
return err
} else if refreshSignature == "" {
return nil
} else if err := s.CreateRefreshTokenSession(ctx, refreshSignature, request); err != nil {
return err
}
return nil
}
func (s *MemoryStore) PersistRefreshTokenGrantSession(ctx context.Context, originalRefreshSignature, accessSignature, refreshSignature string, request fosite.Requester) error {
if err := s.DeleteRefreshTokenSession(ctx, originalRefreshSignature); err != nil {
return err
} else if err := s.CreateAccessTokenSession(ctx, accessSignature, request); err != nil {
return err
} else if err := s.CreateRefreshTokenSession(ctx, refreshSignature, request); err != nil {
return err
}
return nil
}
func (s *MemoryStore) RevokeRefreshToken(ctx context.Context, requestID string) error {
if signature, exists := s.RefreshTokenRequestIDs[requestID]; exists {
s.DeleteRefreshTokenSession(ctx, signature)
}
return nil
}
func (s *MemoryStore) RevokeAccessToken(ctx context.Context, requestID string) error {
if signature, exists := s.AccessTokenRequestIDs[requestID]; exists {
s.DeleteAccessTokenSession(ctx, signature)
}
return nil
}