Store also username in database.

This commit is contained in:
Otto Hollmann 2022-05-11 06:20:11 +02:00
parent cf358a572c
commit e849d51761
No known key found for this signature in database
GPG Key ID: 101D022F467ECE22
4 changed files with 31 additions and 26 deletions

View File

@ -284,9 +284,8 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
} else { } else {
leavePortals(ce) leavePortals(ce)
} }
var pass string; ret := ce.User.bridge.DB.User.GetCredentialsByMXID(ce.User.MXID, &password, &username)
ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) if ret && password != "" {
if ret && pass != "" {
ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.") 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 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 { if ret == true {
ce.Reply("Your password was successfully saved into database.") ce.Reply("Your password was successfully saved into database.")
} else { } else {
@ -324,7 +323,7 @@ func (handler *CommandHandler) CommandRemovePassword(ce *CommandEvent) {
return return
} }
ret = ce.User.bridge.DB.User.SetPassByMXID("", ce.User.MXID) ret = ce.User.bridge.DB.User.SetCredentialsByMXID("", "", ce.User.MXID)
if ret == true { if ret == true {
ce.Reply("Your password was successfully removed from database.") ce.Reply("Your password was successfully removed from database.")
} else { } else {
@ -389,9 +388,10 @@ func (handler *CommandHandler) CommandPing(ce *CommandEvent) {
} }
ce.Reply("You're logged in as @" + username + ", orgid is " + orgId) ce.Reply("You're logged in as @" + username + ", orgid is " + orgId)
} }
var pass string; var password string;
ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) var username string;
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.") ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.")
} }
} }

View File

@ -5,7 +5,7 @@ import (
) )
func init() { 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 { if c.dialect == Postgres {
_, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN password VARCHAR(255)`) _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN password VARCHAR(255)`)
if err != nil { 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 return nil
}} }}
} }

View File

@ -54,17 +54,17 @@ func (uq *UserQuery) GetByJID(userID types.SkypeID) *User {
return uq.New().Scan(row) return uq.New().Scan(row)
} }
func (uq *UserQuery) GetPassByMXID(userID id.UserID, password *string) bool { func (uq *UserQuery) GetCredentialsByMXID(userID id.UserID, password *string, username *string) bool {
row := uq.db.QueryRow(`SELECT password FROM "user" WHERE mxid=$1`, userID) row := uq.db.QueryRow(`SELECT password, username FROM "user" WHERE mxid=$1`, userID)
if row == nil { if row == nil {
return false return false
} }
err := row.Scan(password) err := row.Scan(password, username)
return err == nil return err == nil
} }
func (uq *UserQuery) SetPassByMXID(password string, userID id.UserID) bool { func (uq *UserQuery) SetCredentialsByMXID(password string, username string, userID id.UserID) bool {
row := uq.db.QueryRow(`UPDATE "user" SET password=$1 WHERE mxid=$2`, password, userID) row := uq.db.QueryRow(`UPDATE "user" SET password=$1, username=$2 WHERE mxid=$3`, password, username, userID)
return row != nil return row != nil
} }

22
user.go
View File

@ -295,8 +295,9 @@ func (user *User) Connect(evenIfNoSession bool) bool {
func (user *User) RestoreSession() bool { func (user *User) RestoreSession() bool {
if user.Session != nil { if user.Session != nil {
var password string var password string
ret := user.bridge.DB.User.GetPassByMXID(user.MXID, &password) var username string
if ret && password != "" { 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.") user.log.Debugln("Found password for user " + user.MXID + " in database, trying to login.")
ce := &CommandEvent{ ce := &CommandEvent{
Bot: user.bridge.MatrixHandler.cmd.bridge.Bot, Bot: user.bridge.MatrixHandler.cmd.bridge.Bot,
@ -305,17 +306,13 @@ func (user *User) RestoreSession() bool {
RoomID: user.GetManagementRoom(), RoomID: user.GetManagementRoom(),
User: user, User: user,
} }
username := strings.Split(strings.Replace(user.JID, skypeExt.NewUserSuffix, "", 1), ":") err := user.Login(ce, username, password)
strings.Split(user.JID, ":") if err == nil {
if username[1] != "" { user.log.Debugln("User " + username + " successfully connected.")
err := user.Login(ce, username[1], password) syncAll(user, false)
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)
} }
} else {
user.log.Debugln("An error occured while obtaining username and password for user " + user.MXID + ".")
} }
//sess, err := user.Conn.RestoreWithSession(*user.Session) //sess, err := user.Conn.RestoreWithSession(*user.Session)
@ -432,6 +429,7 @@ func (user *User) monitorSession(ce *CommandEvent) {
leavePortals(ce) leavePortals(ce)
} }
} else { } else {
ce.Reply("Session expired\nStore your password into database with command `save-password` to resolve this issue.")
close(user.Conn.Refresh) close(user.Conn.Refresh)
leavePortals(ce) leavePortals(ce)
} }