Add option to automatically enable custom puppeting with shared secret login

This commit is contained in:
Tulir Asokan
2019-12-30 20:21:04 +02:00
parent 0b51d84646
commit ec0e60c71c
6 changed files with 61 additions and 3 deletions

View File

@ -17,6 +17,9 @@
package main
import (
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
@ -58,6 +61,22 @@ func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid string) error {
return nil
}
func (puppet *Puppet) loginWithSharedSecret(mxid string) (string, error) {
mac := hmac.New(sha512.New, []byte(puppet.bridge.Config.Bridge.LoginSharedSecret))
mac.Write([]byte(mxid))
resp, err := puppet.bridge.AS.BotClient().Login(&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: "m.id.user", User: mxid},
Password: hex.EncodeToString(mac.Sum(nil)),
DeviceID: "WhatsApp Bridge",
InitialDeviceDisplayName: "WhatsApp Bridge",
})
if err != nil {
return "", err
}
return resp.AccessToken, nil
}
func (puppet *Puppet) newCustomIntent() (*appservice.IntentAPI, error) {
if len(puppet.CustomMXID) == 0 {
return nil, ErrNoCustomMXID