forked from grantzvolsky/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_memory.go
224 lines (195 loc) · 6.38 KB
/
manager_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
/*
* Copyright © 2015-2018 Aeneas Rekkas <[email protected]>
*
* 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 <[email protected]>
* @Copyright 2017-2018 Aeneas Rekkas <[email protected]>
* @license Apache-2.0
*/
package consent
import (
"sync"
"time"
"github.com/ory/fosite"
"github.com/ory/hydra/pkg"
"github.com/pkg/errors"
)
type MemoryManager struct {
consentRequests map[string]ConsentRequest
handledConsentRequests map[string]HandledConsentRequest
authRequests map[string]AuthenticationRequest
handledAuthRequests map[string]HandledAuthenticationRequest
authSessions map[string]AuthenticationSession
m map[string]*sync.RWMutex
}
func NewMemoryManager() *MemoryManager {
return &MemoryManager{
consentRequests: map[string]ConsentRequest{},
handledConsentRequests: map[string]HandledConsentRequest{},
authRequests: map[string]AuthenticationRequest{},
handledAuthRequests: map[string]HandledAuthenticationRequest{},
authSessions: map[string]AuthenticationSession{},
m: map[string]*sync.RWMutex{
"consentRequests": new(sync.RWMutex),
"handledConsentRequests": new(sync.RWMutex),
"authRequests": new(sync.RWMutex),
"handledAuthRequests": new(sync.RWMutex),
"authSessions": new(sync.RWMutex),
},
}
}
func (m *MemoryManager) CreateConsentRequest(c *ConsentRequest) error {
m.m["consentRequests"].Lock()
defer m.m["consentRequests"].Unlock()
if _, ok := m.consentRequests[c.Challenge]; ok {
return errors.New("Key already exists")
}
m.consentRequests[c.Challenge] = *c
return nil
}
func (m *MemoryManager) GetConsentRequest(challenge string) (*ConsentRequest, error) {
m.m["consentRequests"].RLock()
defer m.m["consentRequests"].RUnlock()
if c, ok := m.consentRequests[challenge]; ok {
return &c, nil
}
return nil, errors.WithStack(pkg.ErrNotFound)
}
func (m *MemoryManager) HandleConsentRequest(challenge string, r *HandledConsentRequest) (*ConsentRequest, error) {
m.m["handledConsentRequests"].Lock()
m.handledConsentRequests[r.Challenge] = *r
m.m["handledConsentRequests"].Unlock()
return m.GetConsentRequest(challenge)
}
func (m *MemoryManager) VerifyAndInvalidateConsentRequest(verifier string) (*HandledConsentRequest, error) {
for _, c := range m.consentRequests {
if c.Verifier == verifier {
for _, h := range m.handledConsentRequests {
if h.Challenge == c.Challenge {
if h.WasUsed {
return nil, errors.WithStack(fosite.ErrInvalidRequest.WithDebug("Consent verifier has been used already"))
}
h.WasUsed = true
if _, err := m.HandleConsentRequest(h.Challenge, &h); err != nil {
return nil, err
}
h.ConsentRequest = &c
return &h, nil
}
}
}
}
return nil, errors.WithStack(pkg.ErrNotFound)
}
func (m *MemoryManager) FindPreviouslyGrantedConsentRequests(client string, subject string) ([]HandledConsentRequest, error) {
var rs []HandledConsentRequest
for _, c := range m.handledConsentRequests {
cr, err := m.GetConsentRequest(c.Challenge)
if errors.Cause(err) == pkg.ErrNotFound {
return nil, errors.WithStack(errNoPreviousConsentFound)
} else if err != nil {
return nil, err
}
if client != cr.Client.GetID() {
continue
}
if subject != cr.Subject {
continue
}
if c.Error != nil {
continue
}
if !c.Remember {
continue
}
if cr.Skip {
continue
}
if c.RememberFor > 0 && c.RequestedAt.Add(time.Duration(c.RememberFor)*time.Second).Before(time.Now().UTC()) {
continue
}
c.ConsentRequest = cr
rs = append(rs, c)
}
if len(rs) == 0 {
return []HandledConsentRequest{}, nil
}
return rs, nil
}
func (m *MemoryManager) GetAuthenticationSession(id string) (*AuthenticationSession, error) {
m.m["authSessions"].RLock()
defer m.m["authSessions"].RUnlock()
if c, ok := m.authSessions[id]; ok {
return &c, nil
}
return nil, errors.WithStack(pkg.ErrNotFound)
}
func (m *MemoryManager) CreateAuthenticationSession(a *AuthenticationSession) error {
m.m["authSessions"].Lock()
defer m.m["authSessions"].Unlock()
if _, ok := m.authSessions[a.ID]; ok {
return errors.New("Key already exists")
}
m.authSessions[a.ID] = *a
return nil
}
func (m *MemoryManager) DeleteAuthenticationSession(id string) error {
m.m["authSessions"].Lock()
defer m.m["authSessions"].Unlock()
delete(m.authSessions, id)
return nil
}
func (m *MemoryManager) CreateAuthenticationRequest(a *AuthenticationRequest) error {
m.m["authRequests"].Lock()
defer m.m["authRequests"].Unlock()
if _, ok := m.authRequests[a.Challenge]; ok {
return errors.New("Key already exists")
}
m.authRequests[a.Challenge] = *a
return nil
}
func (m *MemoryManager) GetAuthenticationRequest(challenge string) (*AuthenticationRequest, error) {
m.m["authRequests"].RLock()
defer m.m["authRequests"].RUnlock()
if c, ok := m.authRequests[challenge]; ok {
return &c, nil
}
return nil, errors.WithStack(pkg.ErrNotFound)
}
func (m *MemoryManager) HandleAuthenticationRequest(challenge string, r *HandledAuthenticationRequest) (*AuthenticationRequest, error) {
m.m["handledAuthRequests"].Lock()
m.handledAuthRequests[r.Challenge] = *r
m.m["handledAuthRequests"].Unlock()
return m.GetAuthenticationRequest(challenge)
}
func (m *MemoryManager) VerifyAndInvalidateAuthenticationRequest(verifier string) (*HandledAuthenticationRequest, error) {
for _, c := range m.authRequests {
if c.Verifier == verifier {
for _, h := range m.handledAuthRequests {
if h.Challenge == c.Challenge {
if h.WasUsed {
return nil, errors.WithStack(fosite.ErrInvalidRequest.WithDebug("Authentication verifier has been used already"))
}
h.WasUsed = true
if _, err := m.HandleAuthenticationRequest(h.Challenge, &h); err != nil {
return nil, err
}
h.AuthenticationRequest = &c
return &h, nil
}
}
}
}
return nil, errors.WithStack(pkg.ErrNotFound)
}