commit bc89555c9a6325dfafc171736b75ed1432ed20ec
parent 19f817d2a2f3daebc2b1c4770c23645b293e9b65
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Sun, 24 Mar 2024 20:05:11 -0700
Add maximum number of logins
Limit maximum number of logins to 8. This is really just for resource
conservation.
Diffstat:
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/config.h b/config.h
@@ -1,2 +1,3 @@
#define DataDir "/var/timekeeper"
#define TokenTTL (60 * 60 * 8) /* 8 hours */
+#define MaxLogins 8 /* Maximum number of logins for each user */
diff --git a/stmt.c b/stmt.c
@@ -4,6 +4,11 @@
#include "stmt.h"
+#include "config.h"
+
+#define StringHelper(X) #X
+#define String(X) StringHelper(X)
+
struct sqlbox_pstmt pstmts[] = {
[StmtInit0] = { .stmt = "PRAGMA foreign_keys = ON" },
[StmtInit1] = { .stmt =
@@ -33,13 +38,18 @@ struct sqlbox_pstmt pstmts[] = {
"CREATE TABLE IF NOT EXISTS authtokens (\n"
" userid TEXT REFERENCES auth (hash),\n"
" token TEXT NOT NULL PRIMARY KEY,\n"
- " expires INTEGER NOT NULL\n"
+ " expires INTEGER NOT NULL,\n"
+ " created INTEGER NOT NULL\n"
")"
},
[StmtInit5] = { .stmt =
"CREATE TRIGGER IF NOT EXISTS prune_authtokens\n"
" AFTER INSERT ON authtokens FOR EACH ROW BEGIN\n"
" DELETE FROM authtokens WHERE expires <= unixepoch();\n"
+ " DELETE FROM authtokens WHERE token IN (\n"
+ " SELECT token FROM authtokens WHERE userid IS NEW.userid\n"
+ " ORDER BY created DESC LIMIT -1 OFFSET " String(MaxLogins) "\n"
+ " );\n"
"END"
},
[StmtInitLast] = { .stmt =
@@ -60,7 +70,7 @@ struct sqlbox_pstmt pstmts[] = {
},
[StmtDeleteUser] = { .stmt = "DELETE FROM auth WHERE hash IS ?" },
[StmtAddAuthToken] = { .stmt =
- "INSERT into authtokens VALUES (?, ?, ?)"
+ "INSERT into authtokens VALUES (?, ?, ?, unixepoch())"
},
/*