-
Notifications
You must be signed in to change notification settings - Fork 47
/
db.go
81 lines (66 loc) · 2.84 KB
/
db.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
package keyshareserver
import (
"context"
"database/sql/driver"
"github.com/privacybydesign/irmago/internal/keysharecore"
"github.com/go-errors/errors"
)
var (
errUserAlreadyExists = errors.New("Cannot create user, username already taken")
)
type eventType string
const (
eventTypePinCheckRefused eventType = "PIN_CHECK_REFUSED"
eventTypePinCheckSuccess eventType = "PIN_CHECK_SUCCESS"
eventTypePinCheckFailed eventType = "PIN_CHECK_FAILED"
eventTypePinCheckBlocked eventType = "PIN_CHECK_BLOCKED"
eventTypeIRMASession eventType = "IRMA_SESSION"
)
// DB is an interface used by server to manage data storage.
// There are multiple implementations of this, currently:
// - memorydb (memorydb.go) storing all data in memory (forgets everything after reboot)
// - postgresdb (postgresdb.go) storing all data in a postgres database
type DB interface {
AddUser(ctx context.Context, user *User) error
user(ctx context.Context, username string) (*User, error)
updateUser(ctx context.Context, user *User) error
// reservePinTry reserves a pin check attempt, and additionally it returns:
// - allowed is whether the user is allowed to do the pin check (false if user is blocked)
// - tries is how many tries are remaining, after this pin check
// - wait is how long the user must wait before the next attempt is allowed if tries is 0
// resetPinTries increases the user's try count and (if applicable) the date when the user
// is unblocked again in the database, regardless of if the pin check succeeds after this
// invocation.
reservePinTry(ctx context.Context, user *User) (allowed bool, tries int, wait int64, err error)
// resetPinTries resets the user's pin count and unblock date fields in the database to their
// default values (0 past attempts, no unblock date).
resetPinTries(ctx context.Context, user *User) error
// User activity registration.
// setSeen calls are used to track when a users account was last active, for deleting old accounts.
setSeen(ctx context.Context, user *User) error
addLog(ctx context.Context, user *User, eventType eventType, param interface{}) error
// Store email verification tokens on registration
addEmailVerification(ctx context.Context, user *User, emailAddress, token string, validity int) error
}
// UserSecrets is a keysharecore.UserSecrets with DB (un)marshaling methods.
type UserSecrets keysharecore.UserSecrets
// User represents a user of this server.
type User struct {
Username string
Language string
Secrets UserSecrets
id int64
}
// Scan implements sql/driver Scanner interface.
func (us *UserSecrets) Scan(src interface{}) (err error) {
bts, ok := src.([]byte)
if !ok {
return errors.New("cannot convert source: not a byte slice")
}
*us = bts
return nil
}
// Value implements sql/driver Scanner interface.
func (us UserSecrets) Value() (driver.Value, error) {
return []byte(us), nil
}