-
Notifications
You must be signed in to change notification settings - Fork 49
/
memorydb.go
87 lines (71 loc) · 2.34 KB
/
memorydb.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
package keyshareserver
import (
"context"
"sync"
"github.com/privacybydesign/irmago/server/keyshare"
)
// memoryDB provides an easy-to-configure testing implementation of the
// keyshare server database. It does not provide full functionality, instead
// mocking some behaviour, as noted on the specific functions.
type memoryDB struct {
sync.Mutex
users map[string]UserSecrets
}
func NewMemoryDB() DB {
return &memoryDB{users: map[string]UserSecrets{}}
}
func (db *memoryDB) user(_ context.Context, username string) (*User, error) {
// Ensure access to database is single-threaded
db.Lock()
defer db.Unlock()
// Check and fetch user data
secrets, ok := db.users[username]
if !ok {
return nil, keyshare.ErrUserNotFound
}
return &User{Username: username, Secrets: UserSecrets(secrets)}, nil
}
func (db *memoryDB) AddUser(_ context.Context, user *User) error {
// Ensure access to database is single-threaded
db.Lock()
defer db.Unlock()
// Check and insert user
_, exists := db.users[user.Username]
if exists {
return errUserAlreadyExists
}
db.users[user.Username] = user.Secrets
return nil
}
func (db *memoryDB) updateUser(_ context.Context, user *User) error {
// Ensure access to database is single-threaded
db.Lock()
defer db.Unlock()
// Check and update user.
_, exists := db.users[user.Username]
if !exists {
return keyshare.ErrUserNotFound
}
db.users[user.Username] = user.Secrets
return nil
}
func (db *memoryDB) reservePinTry(_ context.Context, _ *User) (bool, int, int64, error) {
// Since this is a testing DB, implementing anything more than always allow creates hastle
return true, 1, 0, nil
}
func (db *memoryDB) resetPinTries(_ context.Context, _ *User) error {
// Since this is a testing DB, implementing anything more than always allow creates hastle
return nil
}
func (db *memoryDB) setSeen(_ context.Context, _ *User) error {
// We don't need to do anything here, as this information cannot be extracted locally
return nil
}
func (db *memoryDB) addLog(_ context.Context, _ *User, _ eventType, _ interface{}) error {
// We don't need to do anything here, as this information cannot be extracted locally
return nil
}
func (db *memoryDB) addEmailVerification(_ context.Context, _ *User, _, _ string, _ int) error {
// We don't need to do anything here, as this information cannot be extracted locally
return nil
}