Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 85582532dc430fd984fab82ac79d7f0b82ba7d2b Author: Wim <wim@42.be> Date: Wed Aug 23 22:49:42 2017 +0000 Add support for personal access tokens (mattermost) * https://docs.mattermost.com/developer/personal-access-tokens.html diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 54a7eff..3de7e55 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -616 +6112 @@ func (b *Bmattermost) Connect() error {   b.mh = matterhook.New(b.Config.WebhookURL,   matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,   BindAddress: b.Config.WebhookBindAddress}) + } else if b.Config.Token != "" { + flog.Info("Connecting using token (sending)") + err := b.apiLogin() + if err != nil { + return err + }   } else if b.Config.Login != "" {   flog.Info("Connecting using login/password (sending)")   err := b.apiLogin() @@ -817 +8714 @@ func (b *Bmattermost) Connect() error {   b.mh = matterhook.New(b.Config.WebhookURL,   matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,   DisableServer: true}) - if b.Config.Login != "" { + if b.Config.Token != "" { + flog.Info("Connecting using token (receiving)") + err := b.apiLogin() + if err != nil { + return err + } + go b.handleMatter() + } else if b.Config.Login != "" {   flog.Info("Connecting using login/password (receiving)")   err := b.apiLogin()   if err != nil { @@ -906 +10313 @@ func (b *Bmattermost) Connect() error {   go b.handleMatter()   }   return nil + } else if b.Config.Token != "" { + flog.Info("Connecting using token (sending and receiving)") + err := b.apiLogin() + if err != nil { + return err + } + go b.handleMatter()   } else if b.Config.Login != "" {   flog.Info("Connecting using login/password (sending and receiving)")   err := b.apiLogin() @@ -988 +1188 @@ func (b *Bmattermost) Connect() error {   }   go b.handleMatter()   } - if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Login == "" { - return errors.New("No connection method found. See that you have WebhookBindAddress, WebhookURL or Login/Password/Server/Team configured.") + if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Login == "" && b.Config.Token == "" { + return errors.New("No connection method found. See that you have WebhookBindAddress, WebhookURL or Token/Login/Password/Server/Team configured.")   }   return nil  } @@ -1527 +17211 @@ func (b *Bmattermost) handleMatter() {   flog.Debugf("Choosing webhooks based receiving")   go b.handleMatterHook(mchan)   } else { - flog.Debugf("Choosing login/password based receiving") + if b.Config.Token != "" { + flog.Debugf("Choosing token based receiving") + } else { + flog.Debugf("Choosing login/password based receiving") + }   go b.handleMatterClient(mchan)   }   for message := range mchan { @@ -2217 +24512 @@ func (b *Bmattermost) handleMatterHook(mchan chan *MMMessage) {  }    func (b *Bmattermost) apiLogin() error { - b.mc = matterclient.New(b.Config.Login, b.Config.Password, + password := b.Config.Password + if b.Config.Token != "" { + password = "MMAUTHTOKEN=" + b.Config.Token + } + + b.mc = matterclient.New(b.Config.Login, password,   b.Config.Team, b.Config.Server)   b.mc.SkipTLSVerify = b.Config.SkipTLSVerify   b.mc.NoTLS = b.Config.NoTLS diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 9d15efb..b84cda6 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -2136 +21311 @@ Team="yourteam"  Login="yourlogin"  Password="yourpass"   +#personal access token of the bot. +#new feature since mattermost 4.1. See https://docs.mattermost.com/developer/personal-access-tokens.html +#OPTIONAL (you can use token instead of login/password) +#Token="abcdefghijklm" +  #Enable this to make a http connection (instead of https) to your mattermost.  #OPTIONAL (default false)  NoTLS=false diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go index f2ae46b..53c7809 100644 --- a/matterclient/matterclient.go +++ b/matterclient/matterclient.go @@ -1407 +1407 @@ func (m *MMClient) Login() error {   for {   m.log.Debugf("%s %s %s %s", logmsg, m.Credentials.Team, m.Credentials.Login, m.Credentials.Server)   if strings.Contains(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN) { - m.log.Debugf(logmsg+" with %s", model.SESSION_COOKIE_TOKEN) + m.log.Debugf(logmsg + " with token")   token := strings.Split(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN+"=")   if len(token) != 2 {   return errors.New("incorrect MMAUTHTOKEN. valid input is MMAUTHTOKEN=yourtoken") @@ -1507 +1507 @@ func (m *MMClient) Login() error {   m.Client.AuthType = model.HEADER_BEARER   m.User, resp = m.Client.GetMe("")   if resp.Error != nil { - return errors.New(resp.Error.DetailedError) + return resp.Error   }   if m.User == nil {   m.log.Errorf("LOGIN TOKEN: %s is invalid", m.Credentials.Pass)