gmessages/libgm/pair.go

148 lines
4.2 KiB
Go
Raw Normal View History

2023-06-30 11:05:33 +00:00
package libgm
2023-06-30 09:54:08 +00:00
import (
2023-07-16 11:36:13 +00:00
"crypto/x509"
2023-07-18 22:19:04 +00:00
"encoding/base64"
"fmt"
2023-06-30 09:54:08 +00:00
2023-07-15 23:11:25 +00:00
"github.com/google/uuid"
2023-07-15 17:43:28 +00:00
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/events"
2023-07-17 13:51:31 +00:00
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
2023-06-30 09:54:08 +00:00
"go.mau.fi/mautrix-gmessages/libgm/util"
)
2023-07-18 22:19:04 +00:00
func (c *Client) StartLogin() (string, error) {
registered, err := c.RegisterPhoneRelay()
if err != nil {
return "", err
}
c.AuthData.TachyonAuthToken = registered.AuthKeyData.TachyonAuthToken
go c.doLongPoll(false)
2023-07-18 22:19:04 +00:00
qr, err := c.GenerateQRCodeData(registered.GetPairingKey())
if err != nil {
return "", fmt.Errorf("failed to generate QR code: %w", err)
}
return qr, nil
}
func (c *Client) GenerateQRCodeData(pairingKey []byte) (string, error) {
urlData := &gmproto.URLData{
PairingKey: pairingKey,
AESKey: c.AuthData.RequestCrypto.AESKey,
HMACKey: c.AuthData.RequestCrypto.HMACKey,
}
encodedURLData, err := proto.Marshal(urlData)
if err != nil {
return "", err
}
cData := base64.StdEncoding.EncodeToString(encodedURLData)
return util.QRCodeURLBase + cData, nil
}
func (c *Client) handlePairingEvent(msg *IncomingRPCMessage) {
switch evt := msg.Pair.Event.(type) {
case *gmproto.RPCPairData_Paired:
c.completePairing(evt.Paired)
case *gmproto.RPCPairData_Revoked:
c.triggerEvent(evt.Revoked)
default:
c.Logger.Debug().Any("evt", evt).Msg("Unknown pair event type")
}
}
func (c *Client) completePairing(data *gmproto.PairedData) {
c.updateTachyonAuthToken(data.GetTokenData().GetTachyonAuthToken(), data.GetTokenData().GetTTL())
c.AuthData.Mobile = data.Mobile
c.AuthData.Browser = data.Browser
c.triggerEvent(&events.PairSuccessful{PairedData: data})
2023-08-10 12:40:43 +00:00
go func() {
err := c.Reconnect()
if err != nil {
c.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to reconnect after pair success: %w", err)})
}
}()
}
2023-07-17 13:51:31 +00:00
func (c *Client) RegisterPhoneRelay() (*gmproto.RegisterPhoneRelayResponse, error) {
2023-07-16 12:55:30 +00:00
key, err := x509.MarshalPKIXPublicKey(c.AuthData.RefreshKey.GetPublicKey())
2023-07-16 11:36:13 +00:00
if err != nil {
return nil, err
}
payload := &gmproto.AuthenticationContainer{
2023-07-17 13:51:31 +00:00
AuthMessage: &gmproto.AuthMessage{
2023-07-16 11:36:13 +00:00
RequestID: uuid.NewString(),
2023-07-16 12:55:30 +00:00
Network: &util.Network,
ConfigVersion: util.ConfigMessage,
2023-07-16 11:36:13 +00:00
},
2023-07-16 12:55:30 +00:00
BrowserDetails: util.BrowserDetailsMessage,
2023-07-17 13:51:31 +00:00
Data: &gmproto.AuthenticationContainer_KeyData{
KeyData: &gmproto.KeyData{
EcdsaKeys: &gmproto.ECDSAKeys{
2023-07-16 11:36:13 +00:00
Field1: 2,
EncryptedKeys: key,
},
},
},
2023-06-30 09:54:08 +00:00
}
return typedHTTPResponse[*gmproto.RegisterPhoneRelayResponse](
c.makeProtobufHTTPRequest(util.RegisterPhoneRelayURL, payload, ContentTypeProtobuf),
)
2023-06-30 09:54:08 +00:00
}
2023-07-16 12:55:30 +00:00
func (c *Client) RefreshPhoneRelay() (string, error) {
payload := &gmproto.AuthenticationContainer{
2023-07-17 13:51:31 +00:00
AuthMessage: &gmproto.AuthMessage{
2023-07-16 10:23:44 +00:00
RequestID: uuid.NewString(),
2023-07-16 12:55:30 +00:00
Network: &util.Network,
TachyonAuthToken: c.AuthData.TachyonAuthToken,
ConfigVersion: util.ConfigMessage,
},
2023-06-30 09:54:08 +00:00
}
res, err := typedHTTPResponse[*gmproto.RefreshPhoneRelayResponse](
c.makeProtobufHTTPRequest(util.RefreshPhoneRelayURL, payload, ContentTypeProtobuf),
)
2023-07-16 12:55:30 +00:00
if err != nil {
return "", err
2023-06-30 09:54:08 +00:00
}
2023-07-16 12:55:30 +00:00
qr, err := c.GenerateQRCodeData(res.GetPairKey())
if err != nil {
return "", err
2023-06-30 09:54:08 +00:00
}
2023-07-16 12:55:30 +00:00
return qr, nil
2023-06-30 09:54:08 +00:00
}
2023-07-17 13:51:31 +00:00
func (c *Client) GetWebEncryptionKey() (*gmproto.WebEncryptionKeyResponse, error) {
payload := &gmproto.AuthenticationContainer{
2023-07-17 13:51:31 +00:00
AuthMessage: &gmproto.AuthMessage{
RequestID: uuid.NewString(),
2023-07-16 12:55:30 +00:00
TachyonAuthToken: c.AuthData.TachyonAuthToken,
ConfigVersion: util.ConfigMessage,
},
2023-06-30 09:54:08 +00:00
}
return typedHTTPResponse[*gmproto.WebEncryptionKeyResponse](
c.makeProtobufHTTPRequest(util.GetWebEncryptionKeyURL, payload, ContentTypeProtobuf),
)
2023-06-30 09:54:08 +00:00
}
2023-07-15 23:11:25 +00:00
2023-07-17 13:51:31 +00:00
func (c *Client) Unpair() (*gmproto.RevokeRelayPairingResponse, error) {
2023-07-16 12:55:30 +00:00
if c.AuthData.TachyonAuthToken == nil || c.AuthData.Browser == nil {
2023-07-15 23:11:25 +00:00
return nil, nil
}
payload := &gmproto.RevokeRelayPairingRequest{
2023-07-17 13:51:31 +00:00
AuthMessage: &gmproto.AuthMessage{
2023-07-15 23:11:25 +00:00
RequestID: uuid.NewString(),
2023-07-16 12:55:30 +00:00
TachyonAuthToken: c.AuthData.TachyonAuthToken,
ConfigVersion: util.ConfigMessage,
2023-07-15 23:11:25 +00:00
},
2023-07-16 12:55:30 +00:00
Browser: c.AuthData.Browser,
2023-07-15 23:11:25 +00:00
}
return typedHTTPResponse[*gmproto.RevokeRelayPairingResponse](
c.makeProtobufHTTPRequest(util.RevokeRelayPairingURL, payload, ContentTypeProtobuf),
)
2023-07-15 23:11:25 +00:00
}