From e849d51761075fdfbe61d8ea9de72850a842679b Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Wed, 11 May 2022 06:20:11 +0200 Subject: [PATCH] Store also username in database. --- commands.go | 16 +++++++------- .../2022-04-02-add-password-username.go | 9 +++++++- database/user.go | 10 ++++----- user.go | 22 +++++++++---------- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/commands.go b/commands.go index 8cb56c1..5c97b6a 100644 --- a/commands.go +++ b/commands.go @@ -284,9 +284,8 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { } else { leavePortals(ce) } - var pass string; - ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) - if ret && pass != "" { + ret := ce.User.bridge.DB.User.GetCredentialsByMXID(ce.User.MXID, &password, &username) + if ret && password != "" { ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.") } } @@ -306,7 +305,7 @@ func (handler *CommandHandler) CommandSavePassword(ce *CommandEvent) { return } - ret = ce.User.bridge.DB.User.SetPassByMXID(ce.User.Conn.LoginInfo.Password, ce.User.MXID) + ret = ce.User.bridge.DB.User.SetCredentialsByMXID(ce.User.Conn.LoginInfo.Password, ce.User.Conn.LoginInfo.Username, ce.User.MXID) if ret == true { ce.Reply("Your password was successfully saved into database.") } else { @@ -324,7 +323,7 @@ func (handler *CommandHandler) CommandRemovePassword(ce *CommandEvent) { return } - ret = ce.User.bridge.DB.User.SetPassByMXID("", ce.User.MXID) + ret = ce.User.bridge.DB.User.SetCredentialsByMXID("", "", ce.User.MXID) if ret == true { ce.Reply("Your password was successfully removed from database.") } else { @@ -389,9 +388,10 @@ func (handler *CommandHandler) CommandPing(ce *CommandEvent) { } ce.Reply("You're logged in as @" + username + ", orgid is " + orgId) } - var pass string; - ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) - if ret && pass != "" { + var password string; + var username string; + ret := ce.User.bridge.DB.User.GetCredentialsByMXID(ce.User.MXID, &password, &username) + if ret && password != "" { ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.") } } diff --git a/database/upgrades/2022-04-02-add-password-username.go b/database/upgrades/2022-04-02-add-password-username.go index 174843b..31c5c15 100644 --- a/database/upgrades/2022-04-02-add-password-username.go +++ b/database/upgrades/2022-04-02-add-password-username.go @@ -5,7 +5,7 @@ import ( ) func init() { - upgrades[20] = upgrade{"Add password column to user table.", func(tx *sql.Tx, c context) error { + upgrades[20] = upgrade{"Add password and username column to user table.", func(tx *sql.Tx, c context) error { if c.dialect == Postgres { _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN password VARCHAR(255)`) if err != nil { @@ -13,6 +13,13 @@ func init() { } } + if c.dialect == Postgres { + _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN username VARCHAR(255)`) + if err != nil { + return err + } + } + return nil }} } diff --git a/database/user.go b/database/user.go index 4d04b5b..5673af3 100644 --- a/database/user.go +++ b/database/user.go @@ -54,17 +54,17 @@ func (uq *UserQuery) GetByJID(userID types.SkypeID) *User { return uq.New().Scan(row) } -func (uq *UserQuery) GetPassByMXID(userID id.UserID, password *string) bool { - row := uq.db.QueryRow(`SELECT password FROM "user" WHERE mxid=$1`, userID) +func (uq *UserQuery) GetCredentialsByMXID(userID id.UserID, password *string, username *string) bool { + row := uq.db.QueryRow(`SELECT password, username FROM "user" WHERE mxid=$1`, userID) if row == nil { return false } - err := row.Scan(password) + err := row.Scan(password, username) return err == nil } -func (uq *UserQuery) SetPassByMXID(password string, userID id.UserID) bool { - row := uq.db.QueryRow(`UPDATE "user" SET password=$1 WHERE mxid=$2`, password, userID) +func (uq *UserQuery) SetCredentialsByMXID(password string, username string, userID id.UserID) bool { + row := uq.db.QueryRow(`UPDATE "user" SET password=$1, username=$2 WHERE mxid=$3`, password, username, userID) return row != nil } diff --git a/user.go b/user.go index ff9a96c..c66cb26 100644 --- a/user.go +++ b/user.go @@ -295,8 +295,9 @@ func (user *User) Connect(evenIfNoSession bool) bool { func (user *User) RestoreSession() bool { if user.Session != nil { var password string - ret := user.bridge.DB.User.GetPassByMXID(user.MXID, &password) - if ret && password != "" { + var username string + ret := user.bridge.DB.User.GetCredentialsByMXID(user.MXID, &password, &username) + if ret && password != "" && username != "" { user.log.Debugln("Found password for user " + user.MXID + " in database, trying to login.") ce := &CommandEvent{ Bot: user.bridge.MatrixHandler.cmd.bridge.Bot, @@ -305,17 +306,13 @@ func (user *User) RestoreSession() bool { RoomID: user.GetManagementRoom(), User: user, } - username := strings.Split(strings.Replace(user.JID, skypeExt.NewUserSuffix, "", 1), ":") - strings.Split(user.JID, ":") - if username[1] != "" { - err := user.Login(ce, username[1], password) - if err == nil { - user.log.Debugln("User " + username[1] + " successfully connected.") - syncAll(user, false) - } - } else { - user.log.Debugln("Unable to get username for user %s , user.JID=%s", user.MXID, user.JID) + err := user.Login(ce, username, password) + if err == nil { + user.log.Debugln("User " + username + " successfully connected.") + syncAll(user, false) } + } else { + user.log.Debugln("An error occured while obtaining username and password for user " + user.MXID + ".") } //sess, err := user.Conn.RestoreWithSession(*user.Session) @@ -432,6 +429,7 @@ func (user *User) monitorSession(ce *CommandEvent) { leavePortals(ce) } } else { + ce.Reply("Session expired\nStore your password into database with command `save-password` to resolve this issue.") close(user.Conn.Refresh) leavePortals(ce) }