forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
253 lines (222 loc) · 7.96 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*
*/
package storage
import (
"context"
"github.com/pkg/errors"
"github.com/ory/fosite"
)
type MemoryUserRelation struct {
Username string
Password string
}
type MemoryStore struct {
Clients map[string]fosite.Client
AuthorizeCodes map[string]StoreAuthorizeCode
IDSessions map[string]fosite.Requester
AccessTokens map[string]fosite.Requester
Implicit map[string]fosite.Requester
RefreshTokens map[string]fosite.Requester
PKCES 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.Client),
AuthorizeCodes: make(map[string]StoreAuthorizeCode),
IDSessions: make(map[string]fosite.Requester),
AccessTokens: make(map[string]fosite.Requester),
Implicit: make(map[string]fosite.Requester),
RefreshTokens: make(map[string]fosite.Requester),
PKCES: make(map[string]fosite.Requester),
Users: make(map[string]MemoryUserRelation),
AccessTokenRequestIDs: make(map[string]string),
RefreshTokenRequestIDs: make(map[string]string),
}
}
type StoreAuthorizeCode struct {
active bool
fosite.Requester
}
func NewExampleStore() *MemoryStore {
return &MemoryStore{
IDSessions: make(map[string]fosite.Requester),
Clients: map[string]fosite.Client{
"my-client": &fosite.DefaultClient{
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"},
},
"encoded:client": &fosite.DefaultClient{
ID: "encoded:client",
Secret: []byte(`$2a$10$A7M8b65dSSKGHF0H2sNkn.9Z0hT8U1Nv6OWPV3teUUaczXkVkxuDS`), // = "encoded&password"
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]StoreAuthorizeCode{},
Implicit: map[string]fosite.Requester{},
AccessTokens: map[string]fosite.Requester{},
RefreshTokens: map[string]fosite.Requester{},
PKCES: 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] = StoreAuthorizeCode{active: true, Requester: 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
}
if !rel.active {
return rel, fosite.ErrInvalidatedAuthorizeCode
}
return rel.Requester, nil
}
func (s *MemoryStore) InvalidateAuthorizeCodeSession(ctx context.Context, code string) error {
rel, ok := s.AuthorizeCodes[code]
if !ok {
return fosite.ErrNotFound
}
rel.active = false
s.AuthorizeCodes[code] = rel
return nil
}
func (s *MemoryStore) DeleteAuthorizeCodeSession(_ context.Context, code string) error {
delete(s.AuthorizeCodes, code)
return nil
}
func (s *MemoryStore) CreatePKCERequestSession(_ context.Context, code string, req fosite.Requester) error {
s.PKCES[code] = req
return nil
}
func (s *MemoryStore) GetPKCERequestSession(_ context.Context, code string, _ fosite.Session) (fosite.Requester, error) {
rel, ok := s.PKCES[code]
if !ok {
return nil, fosite.ErrNotFound
}
return rel, nil
}
func (s *MemoryStore) DeletePKCERequestSession(_ context.Context, code string) error {
delete(s.PKCES, 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) RevokeRefreshToken(ctx context.Context, requestID string) error {
if signature, exists := s.RefreshTokenRequestIDs[requestID]; exists {
s.DeleteRefreshTokenSession(ctx, signature)
s.DeleteAccessTokenSession(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
}