-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauthorization.go
244 lines (178 loc) · 4.52 KB
/
authorization.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
package portal
import (
"bytes"
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"sort"
"sync"
"time"
"github.com/olebedev/emitter"
"github.com/pkg/errors"
)
type authStore struct {
}
// AuthorizationState is the state of an Authorization
type AuthorizationState string
// s AuthorizationState json.Marshaler
// var foo json.Marshaler
// MarshalJSON() ([]byte, error)
const (
AuthorizationPending AuthorizationState = "pending"
// transition => {accepted, denied}
AuthorizationAccepted AuthorizationState = "accepted"
// transition => { consumed }
AuthorizationDenied AuthorizationState = "denied"
// transition => {}
AuthorizationConsumed AuthorizationState = "consumed"
// transition => {}
// AuthorizationTimeout
)
type Authorization struct {
ID string `json:"id"`
State AuthorizationState `json:"state"`
Request *jsonRPCRequest `json:"request"`
CreatedAt time.Time `json:"createdAt"`
}
type authorizationStore struct {
authorizaitons map[string]*Authorization
events *emitter.Emitter
mu sync.Mutex
}
func authChangeTopic(id string) string {
return fmt.Sprintf("change:%s", id)
}
func newAuthorizationStore() *authorizationStore {
return &authorizationStore{
events: &emitter.Emitter{},
authorizaitons: make(map[string]*Authorization),
}
}
// waitChange blocks until an authorization changes its state
func (s *authorizationStore) waitChange(ctx context.Context, id string) error {
s.mu.Lock()
auth, found := s.authorizaitons[id]
if !found {
s.mu.Unlock()
return errors.New("not found")
}
if auth.State != AuthorizationPending {
s.mu.Unlock()
return errors.New("not pending")
}
// timeout
topic := authChangeTopic(id)
resolved := s.events.Once(topic)
s.mu.Unlock()
select {
case <-resolved:
return nil
case <-ctx.Done():
s.events.Off(topic, resolved)
}
return nil
}
func (s *authorizationStore) get(id string) (*Authorization, bool) {
s.mu.Lock()
defer s.mu.Unlock()
auth, found := s.authorizaitons[id]
return auth, found
}
func (s *authorizationStore) allAuthorizations() []*Authorization {
s.mu.Lock()
defer s.mu.Unlock()
var auths []*Authorization
for _, auth := range s.authorizaitons {
auths = append(auths, auth)
}
sort.Slice(auths, func(i, j int) bool {
return auths[i].CreatedAt.After(auths[j].CreatedAt)
})
return auths
}
func (s *authorizationStore) pendingAuthorizations() []*Authorization {
var auths []*Authorization
for _, auth := range s.allAuthorizations() {
if auth.State == AuthorizationPending {
auths = append(auths, auth)
}
}
return auths
}
func (s *authorizationStore) create(req *jsonRPCRequest) (*Authorization, error) {
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
return nil, err
}
id := base64.RawURLEncoding.EncodeToString(buf[:])
auth := &Authorization{
ID: id,
State: AuthorizationPending,
Request: req,
CreatedAt: time.Now(),
}
s.mu.Lock()
defer s.mu.Unlock()
// TODO automatic removal after 5 minutes
s.authorizaitons[id] = auth
return auth, nil
}
// consume confirms that an RPC request had been accepted by user.
// It RPC request had been accepted, it returns true exactly once.
func (s *authorizationStore) verify(id string, req *jsonRPCRequest) bool {
s.mu.Lock()
defer s.mu.Unlock()
auth, found := s.authorizaitons[id]
if !found {
return false
}
if auth.State != AuthorizationAccepted {
return false
}
same := auth.Request.Method == req.Method && bytes.Equal(auth.Request.Params, req.Params)
if !same {
return false
}
auth.State = AuthorizationConsumed
s.emitAuthChange(id)
return true
}
func (s *authorizationStore) exists(id string) bool {
s.mu.Lock()
defer s.mu.Unlock()
_, found := s.authorizaitons[id]
return found
}
func (s *authorizationStore) accept(id string) error {
s.mu.Lock()
defer s.mu.Unlock()
auth, found := s.authorizaitons[id]
if !found {
return errors.New("Authorization not found")
}
if auth.State != AuthorizationPending {
return errors.New("Authorization not pending")
}
auth.State = AuthorizationAccepted
s.emitAuthChange(id)
return nil
}
func (s *authorizationStore) deny(id string) error {
s.mu.Lock()
defer s.mu.Unlock()
auth, found := s.authorizaitons[id]
if !found {
return errors.New("Authorization not found")
}
if auth.State != AuthorizationPending {
return errors.New("Authorization not pending")
}
auth.State = AuthorizationDenied
s.emitAuthChange(id)
return nil
}
func (s *authorizationStore) emitAuthChange(id string) {
s.events.Emit(authChangeTopic(id))
}