From 56cbd40bfe90d423e9ba46023002511b5f97ef8d Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Thu, 29 Sep 2022 10:41:01 +0100
Subject: [PATCH] Add authenticator ID field to the userlog

---
 protocol.go            | 15 ++++++++-------
 server/sql.go          |  3 +++
 server/userlog.go      | 12 +++++++-----
 server/userlog_test.go |  1 +
 4 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/protocol.go b/protocol.go
index d3beb10..37bf082 100644
--- a/protocol.go
+++ b/protocol.go
@@ -66,13 +66,14 @@ func DecodeDeviceInfoFromMap(m map[string]string, prefix string) *DeviceInfo {
 
 // LogEntry represents an authentication event in the user-specific log.
 type LogEntry struct {
-	Timestamp   time.Time   `json:"timestamp"`
-	Username    string      `json:"username"`
-	Type        string      `json:"log_type"`
-	Message     string      `json:"message,omitempty"`
-	Service     string      `json:"service,omitempty"`
-	LoginMethod string      `json:"login_method,omitempty"`
-	DeviceInfo  *DeviceInfo `json:"device_info,omitempty"`
+	Timestamp            time.Time   `json:"timestamp"`
+	Username             string      `json:"username"`
+	Type                 string      `json:"log_type"`
+	Message              string      `json:"message,omitempty"`
+	Service              string      `json:"service,omitempty"`
+	LoginMethod          string      `json:"login_method,omitempty"`
+	LoginAuthenticatorID string      `json:"login_authenticator_id,omitempty"`
+	DeviceInfo           *DeviceInfo `json:"device_info,omitempty"`
 }
 
 func (e *LogEntry) Validate() error {
diff --git a/server/sql.go b/server/sql.go
index 271f991..1c7b3da 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -85,6 +85,9 @@ INSERT INTO userlog (username, service, log_type, login_method, message, device_
 DROP TABLE _userlog_old;
 `, `
 PRAGMA foreign_keys=on;
+`),
+	sqlutil.Statement(`
+ALTER TABLE userlog ADD COLUMN login_authenticator_id TEXT;
 `),
 }
 
diff --git a/server/userlog.go b/server/userlog.go
index 868797b..508b940 100644
--- a/server/userlog.go
+++ b/server/userlog.go
@@ -42,17 +42,17 @@ var userlogDBStatements = map[string]string{
 	// optional device information).
 	"insert_userlog": `
 	    INSERT INTO userlog (
-	        username, service, log_type, login_method, message, timestamp
+	        username, service, log_type, login_method, login_authenticator_id, message, timestamp
             ) VALUES (
-                ?, ?, ?, ?, ?, ?
+                ?, ?, ?, ?, ?, ?, ?
             )`,
 	"insert_userlog_with_device_info": `
 	    INSERT INTO userlog (
-	        username, service, log_type, login_method, message, timestamp,
+	        username, service, log_type, login_method, login_authenticator_id, message, timestamp,
 		device_id, device_remote_zone, device_user_agent,
 		device_browser, device_os, device_mobile
             ) VALUES (
-                ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
+                ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
             )`,
 
 	// Database maintenance (prune old entries etc).
@@ -87,7 +87,7 @@ var userlogDBStatements = map[string]string{
 	// Retrieve logs for a specific user.
 	"get_user_logs": `
 	    SELECT
-		service, log_type, login_method, message, timestamp,
+		service, log_type, login_method, login_authenticator_id, message, timestamp,
 	        device_id, device_remote_zone, device_user_agent,
 		device_browser, device_os, device_mobile
 	    FROM
@@ -229,6 +229,7 @@ func (u *userlogDB) AddLog(ctx context.Context, entry *usermetadb.LogEntry) erro
 		entry.Service,
 		entry.Type,
 		entry.LoginMethod,
+		entry.LoginAuthenticatorID,
 		entry.Message,
 		entry.Timestamp,
 	}
@@ -285,6 +286,7 @@ func scanEntryRow(rows *sql.Rows) (*usermetadb.LogEntry, error) {
 		&e.Service,
 		&e.Type,
 		&e.LoginMethod,
+		&e.LoginAuthenticatorID,
 		&e.Message,
 		&e.Timestamp,
 		&deviceID,
diff --git a/server/userlog_test.go b/server/userlog_test.go
index 67f42c8..b2ae75a 100644
--- a/server/userlog_test.go
+++ b/server/userlog_test.go
@@ -127,6 +127,7 @@ func bulkLoadTestLogs(t testing.TB, db *sql.DB) *usermetadb.LogEntry {
 			entry.Service,
 			entry.Type,
 			entry.LoginMethod,
+			entry.LoginAuthenticatorID,
 			entry.Message,
 			entry.Timestamp,
 			entry.DeviceInfo.ID,
-- 
GitLab