Move start login methods to pair.go

This commit is contained in:
Tulir Asokan 2023-07-19 01:19:04 +03:00
parent b46885ad58
commit 6caf7e89fb
2 changed files with 29 additions and 30 deletions

View file

@ -4,7 +4,6 @@ import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"net/http"
@ -14,7 +13,6 @@ import (
"github.com/google/uuid"
"github.com/rs/zerolog"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/events"
@ -133,34 +131,6 @@ func (c *Client) Connect() error {
return nil
}
func (c *Client) StartLogin() (string, error) {
registered, err := c.RegisterPhoneRelay()
if err != nil {
return "", err
}
c.AuthData.TachyonAuthToken = registered.AuthKeyData.TachyonAuthToken
go c.rpc.ListenReceiveMessages(false)
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) Disconnect() {
c.rpc.CloseConnection()
c.http.CloseIdleConnections()

View file

@ -3,6 +3,7 @@ package libgm
import (
"bytes"
"crypto/x509"
"encoding/base64"
"fmt"
"io"
"net/http"
@ -15,6 +16,34 @@ import (
"go.mau.fi/mautrix-gmessages/libgm/util"
)
func (c *Client) StartLogin() (string, error) {
registered, err := c.RegisterPhoneRelay()
if err != nil {
return "", err
}
c.AuthData.TachyonAuthToken = registered.AuthKeyData.TachyonAuthToken
go c.rpc.ListenReceiveMessages(false)
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: