mirror of
https://github.com/cwinfo/matterbridge.git
synced 2025-06-27 07:39:25 +00:00
Add MxId/Token login option for Matrix (#1438)
* Add possibility for using MxId/Token with Matrix Makes it possible to configure a Matrix bot to use Matrix ID + Access token instead of username/password. This makes it possible to use the bot in environments where password login is disabled (for example SSO environments). Matrix user ID's are commonly referred to as "MXID's". I thought about (ab)using "Login" here but it felt like a bad idea given it's used as "username" for the password login. None of the other configuration items felt fitting. Closes #1429 * MxId -> MxID * Add err != nil to matrix.NewClient
This commit is contained in:
@ -75,22 +75,33 @@ func New(cfg *bridge.Config) bridge.Bridger {
|
||||
func (b *Bmatrix) Connect() error {
|
||||
var err error
|
||||
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
||||
b.mc, err = matrix.NewClient(b.GetString("Server"), "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
if b.GetString("MxID") != "" && b.GetString("Token") != "" {
|
||||
b.mc, err = matrix.NewClient(
|
||||
b.GetString("Server"), b.GetString("MxID"), b.GetString("Token"),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.UserID = b.GetString("MxID")
|
||||
b.Log.Info("Using existing Matrix credentials")
|
||||
} else {
|
||||
b.mc, err = matrix.NewClient(b.GetString("Server"), "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := b.mc.Login(&matrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
User: b.GetString("Login"),
|
||||
Password: b.GetString("Password"),
|
||||
Identifier: matrix.NewUserIdentifier(b.GetString("Login")),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.mc.SetCredentials(resp.UserID, resp.AccessToken)
|
||||
b.UserID = resp.UserID
|
||||
b.Log.Info("Connection succeeded")
|
||||
}
|
||||
resp, err := b.mc.Login(&matrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
User: b.GetString("Login"),
|
||||
Password: b.GetString("Password"),
|
||||
Identifier: matrix.NewUserIdentifier(b.GetString("Login")),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.mc.SetCredentials(resp.UserID, resp.AccessToken)
|
||||
b.UserID = resp.UserID
|
||||
b.Log.Info("Connection succeeded")
|
||||
go b.handlematrix()
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user