-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_in-memory.go
67 lines (58 loc) · 1.47 KB
/
provider_in-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
package auth
import (
"errors"
"github.com/sauerbraten/maitred/v2/pkg/protocol"
"github.com/sauerbraten/waiter/pkg/protocol/role"
)
// request holds the data we need to remember between
// generating a challenge and checking the response.
type request struct {
id uint32
user *User
solution string
}
type InMemoryProvider struct {
ids *protocol.IDCycle
usersByName map[string]*User
pendingRequests map[uint32]*request
}
func NewInMemoryProvider(users []*User) *InMemoryProvider {
p := &InMemoryProvider{
ids: new(protocol.IDCycle),
usersByName: map[string]*User{},
pendingRequests: map[uint32]*request{},
}
for _, u := range users {
p.usersByName[u.Name] = u
}
return p
}
func (p *InMemoryProvider) GenerateChallenge(name string, callback func(uint32, string, error)) {
u, ok := p.usersByName[name]
if !ok {
callback(0, "", errors.New("auth: user not found"))
return
}
req := &request{
id: p.ids.Next(),
user: u,
}
chal, sol, err := GenerateChallenge(u.PublicKey)
if err == nil {
req.solution = sol
p.pendingRequests[req.id] = req
}
callback(req.id, chal, err)
}
func (p *InMemoryProvider) ConfirmAnswer(reqID uint32, answ string, callback func(role.ID, error)) {
req, ok := p.pendingRequests[reqID]
if !ok {
callback(role.None, errors.New("auth: request not found"))
return
}
if answ != req.solution {
callback(role.None, errors.New("auth: wrong answer"))
return
}
callback(req.user.Role, nil)
}