Commit 7853883e authored by Sietse Ringers's avatar Sietse Ringers
Browse files

refactor: export fewer functions of irma keyshare task

parent 1fce14b5
......@@ -11,16 +11,9 @@ var keyshareTaskCmd = &cobra.Command{
Short: "Perform IRMA keyshare background tasks",
Run: func(command *cobra.Command, args []string) {
conf := configureKeyshareTask(command)
task, err := taskserver.New(conf)
if err != nil {
if err := taskserver.Do(conf); err != nil {
die("", err)
}
task.CleanupEmails()
task.CleanupTokens()
task.CleanupAccounts()
task.ExpireAccounts()
},
}
......
......@@ -31,10 +31,10 @@ func TestCleanupEmails(t *testing.T) {
_, err = db.Exec("INSERT INTO irma.emails (user_id, email, delete_on) VALUES (15, 'test@test.com', NULL), (15, 'test2@test.com', $1), (15, 'test3@test.com', 0)", time.Now().Add(time.Hour).Unix())
require.NoError(t, err)
th, err := New(&Configuration{DBConnstring: test.PostgresTestUrl, Logger: irma.Logger})
th, err := newHandler(&Configuration{DBConnstring: test.PostgresTestUrl, Logger: irma.Logger})
require.NoError(t, err)
th.CleanupEmails()
th.cleanupEmails()
res, err := db.Query("SELECT COUNT(*) FROM irma.emails")
require.NoError(t, err)
......@@ -58,10 +58,10 @@ func TestCleanupTokens(t *testing.T) {
_, err = db.Exec("INSERT INTO irma.email_login_tokens (token, email, expiry) VALUES ('t1', 't1@test.com', 0), ('t2', 't2@test.com', $1)", time.Now().Add(time.Hour).Unix())
require.NoError(t, err)
th, err := New(&Configuration{DBConnstring: test.PostgresTestUrl, Logger: irma.Logger})
th, err := newHandler(&Configuration{DBConnstring: test.PostgresTestUrl, Logger: irma.Logger})
require.NoError(t, err)
th.CleanupTokens()
th.cleanupTokens()
res, err := db.Query("SELECT COUNT(*) FROM irma.email_verification_tokens")
require.NoError(t, err)
......@@ -88,10 +88,10 @@ func TestCleanupAccounts(t *testing.T) {
_, err = db.Exec("INSERT INTO irma.users (id, username, language, coredata, pin_counter, pin_block_date, last_seen, delete_on) VALUES (15, 'testuser', '', '', 0,0, 0, NULL), (16, 't2', '', '', 0, 0, 0, $1-3600), (17, 't3', '', '', 0, 0, $1, $1-3600), (18, 't4', '', NULL, 0, 0, $1, $1-3600)", time.Now().Unix())
require.NoError(t, err)
th, err := New(&Configuration{DBConnstring: test.PostgresTestUrl, Logger: irma.Logger})
th, err := newHandler(&Configuration{DBConnstring: test.PostgresTestUrl, Logger: irma.Logger})
require.NoError(t, err)
th.CleanupAccounts()
th.cleanupAccounts()
res, err := db.Query("SELECT COUNT(*) FROM irma.users")
require.NoError(t, err)
......@@ -114,7 +114,7 @@ func TestExpireAccounts(t *testing.T) {
_, err = db.Exec("INSERT INTO irma.emails (user_id, email, delete_on) VALUES (15, 'test@test.com', NULL), (16, 'test@test.com', NULL)")
require.NoError(t, err)
th, err := New(&Configuration{
th, err := newHandler(&Configuration{
DBConnstring: test.PostgresTestUrl,
DeleteDelay: 30,
ExpiryDelay: 1,
......@@ -133,7 +133,7 @@ func TestExpireAccounts(t *testing.T) {
})
require.NoError(t, err)
th.ExpireAccounts()
th.expireAccounts()
res, err := db.Query("SELECT COUNT(*) FROM irma.users WHERE delete_on IS NOT NULL")
require.NoError(t, err)
......
......@@ -9,12 +9,12 @@ import (
"github.com/privacybydesign/irmago/internal/common"
)
type TaskHandler struct {
type taskHandler struct {
conf *Configuration
db *sql.DB
}
func New(conf *Configuration) (*TaskHandler, error) {
func newHandler(conf *Configuration) (*taskHandler, error) {
err := processConfiguration(conf)
if err != nil {
return nil, err
......@@ -26,14 +26,27 @@ func New(conf *Configuration) (*TaskHandler, error) {
if err = db.Ping(); err != nil {
return nil, errors.Errorf("failed to connect to database: %v", err)
}
return &TaskHandler{
db: db,
conf: conf,
}, nil
task := &taskHandler{db: db, conf: conf}
return task, nil
}
func Do(conf *Configuration) error {
task, err := newHandler(conf)
if err != nil {
return err
}
task.cleanupEmails()
task.cleanupTokens()
task.cleanupAccounts()
task.expireAccounts()
return nil
}
// Remove email addresses marked for deletion long enough ago
func (t *TaskHandler) CleanupEmails() {
func (t *taskHandler) cleanupEmails() {
_, err := t.db.Exec("DELETE FROM irma.emails WHERE delete_on < $1", time.Now().Unix())
if err != nil {
t.conf.Logger.WithField("error", err).Error("Could not remove email addresses marked for deletion")
......@@ -41,7 +54,7 @@ func (t *TaskHandler) CleanupEmails() {
}
// Remove old login and email verifciation tokens
func (t *TaskHandler) CleanupTokens() {
func (t *taskHandler) cleanupTokens() {
_, err := t.db.Exec("DELETE FROM irma.email_login_tokens WHERE expiry < $1", time.Now().Unix())
if err != nil {
t.conf.Logger.WithField("error", err).Error("Could not remove email login tokens that have expired")
......@@ -54,7 +67,7 @@ func (t *TaskHandler) CleanupTokens() {
}
// Cleanup accounts disabled long enough ago.
func (t *TaskHandler) CleanupAccounts() {
func (t *taskHandler) cleanupAccounts() {
_, err := t.db.Exec("DELETE FROM irma.users WHERE delete_on < $1 AND (coredata IS NULL OR last_seen < delete_on - $2)",
time.Now().Unix(),
t.conf.DeleteDelay*24*60*60)
......@@ -63,7 +76,7 @@ func (t *TaskHandler) CleanupAccounts() {
}
}
func (t *TaskHandler) sendExpiryEmails(id int64, username, lang string) error {
func (t *taskHandler) sendExpiryEmails(id int64, username, lang string) error {
// Fetch user's email addresses
emailRes, err := t.db.Query("SELECT email FROM irma.emails WHERE user_id = $1", id)
if err != nil {
......@@ -97,7 +110,7 @@ func (t *TaskHandler) sendExpiryEmails(id int64, username, lang string) error {
}
// Mark old unused accounts for deletion, and inform their owners.
func (t *TaskHandler) ExpireAccounts() {
func (t *taskHandler) expireAccounts() {
// Disable this task when email server is not given
if t.conf.EmailServer == "" {
t.conf.Logger.Warning("Expiring accounts is disabled, as no email server is configured")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment