/
db.go
105 lines (84 loc) · 3.9 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package db
// Copyright 2021 Matthew R. Wilson <mwilson@mattwilson.org>
//
// This file is part of virtual1403
// <https://github.com/racingmars/virtual1403>.
//
// virtual1403 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// virtual1403 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with virtual1403. If not, see <https://www.gnu.org/licenses/>.
import (
"errors"
"time"
"github.com/racingmars/virtual1403/webserver/model"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/crypto/nacl/auth"
)
var ErrNotFound = errors.New("not found")
const SessionSecretKeyLength = 32
const ShareSecretKeyLength = auth.KeySize
type DB interface {
// Close will close the database.
Close() error
// SaveUser saves a new user, or updates an existing user, identified by
// user.Email.
SaveUser(user model.User) error
// GetUser retrieves a user from the database by email.
GetUser(email string) (model.User, error)
// GetUserForAccessKey returns the user with the provided access key.
GetUserForAccessKey(key string) (model.User, error)
// GetUsers returns all users in the database.
GetUsers() ([]model.User, error)
// DeleteUser deletes the user with the provided email address. who is
// recorded in the deletion log as the person/system that performed the
// deletion.
DeleteUser(email, who string) error
// DeleteInactiveUsers deletes users based on inactivity. The function
// takes two times: the oldest allowed inactive time (time since last
// print job) for verified accounts, and oldest allowed creation time for
// new accounts that are still unverified. For verified accounts with no
// print jobs, the inactive time is based on sign-up time. Returns the
// number of users deleted (which may still be >0 when err != nil).
DeleteInactiveUsers(inactive, unverified time.Time) (int, error)
// LogJob will record that a job was just processed for the user with the
// provided email address. This will add to the job log and update the
// user's record with the last job time and increase the job count for the
// user.
LogJob(email, jobinfo string, pages int, pdf []byte) error
// GetUserJobLog returns up to size rows from the job log for the user
// with the provided email address. Jobs are returned in descending order
// of time.
GetUserJobLog(email string, size int) ([]model.JobLogEntry, error)
// GetJobLog returns up to size rows from the job log in descending order
// of time.
GetJobLog(size int) ([]model.JobLogEntry, error)
// GetJob returns the details of one job.
GetJob(id uint64) (model.JobLogEntry, error)
// GetPDF will get the PDF file for the give job ID.
GetPDF(job uint64) ([]byte, error)
// CleanPDFs will delete the stored PDFs from before the provided date.
CleanPDFs(cutoff time.Time)
// GetSessionSecret will return a 32-byte random value to use as the
// session secret key. If none exists in the database, this function will
// generate one and save it. Essentially, on first startup, each new
// database generates a random value which will be used for the life of
// the database file.
GetSessionSecret() ([]byte, error)
// GetShareSecret will return a 32-byte random value to use as the PDF
// link authentication key. If none exists in the database, this function
// will generate one and save it. Essentially, on first startup, each new
// database generates a random value which will be used for the life of
// the database file.
GetShareSecret() ([]byte, error)
// We also use our database as an autocert cache
autocert.Cache
}