From 7fe4f0b047a7f25f70445bf23c2c67acb72c5664 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 16 Jul 2023 14:40:28 +0300 Subject: [PATCH] Unnest device pair info in AuthData --- libgm/client.go | 16 +++++++++------- libgm/media_processor.go | 2 +- libgm/pair.go | 4 ++-- libgm/pairing_handler.go | 3 +-- libgm/rpc.go | 2 +- libgm/session_handler.go | 4 ++-- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/libgm/client.go b/libgm/client.go index 8ff4889..f7a8c49 100644 --- a/libgm/client.go +++ b/libgm/client.go @@ -32,7 +32,8 @@ type AuthData struct { // Key used to sign requests to refresh the tachyon auth token from the server RefreshKey *crypto.JWK `json:"refresh_key,omitempty"` // Identity of the paired phone and browser - DevicePair *pblite.DevicePair `json:"device_pair,omitempty"` + Browser *binary.Device `json:"browser,omitempty"` + Mobile *binary.Device `json:"mobile,omitempty"` // Key used to authenticate with the server TachyonAuthToken []byte `json:"tachyon_token,omitempty"` TachyonExpiry time.Time `json:"tachyon_expiry,omitempty"` @@ -153,7 +154,7 @@ func (c *Client) IsConnected() bool { } func (c *Client) IsLoggedIn() bool { - return c.authData != nil && c.authData.DevicePair != nil + return c.authData != nil && c.authData.Browser != nil } func (c *Client) Reconnect() error { @@ -270,9 +271,10 @@ func (c *Client) updateTachyonAuthToken(t []byte, validFor int64) { c.Logger.Debug().Time("tachyon_expiry", c.authData.TachyonExpiry).Int64("valid_for", validFor).Msg("Updated tachyon token") } -func (c *Client) updateDevicePair(devicePair *pblite.DevicePair) { - c.authData.DevicePair = devicePair - c.Logger.Debug().Any("devicePair", devicePair).Msg("Updated DevicePair") +func (c *Client) updateDevicePair(mobile, browser *binary.Device) { + c.authData.Mobile = mobile + c.authData.Browser = browser + c.Logger.Debug().Any("mobile", mobile).Any("browser", browser).Msg("Updated device pair") } func (c *Client) SaveAuthSession(path string) error { @@ -300,7 +302,7 @@ func LoadAuthSession(path string) (*AuthData, error) { } func (c *Client) refreshAuthToken() error { - if c.authData.DevicePair == nil || time.Until(c.authData.TachyonExpiry) > RefreshTachyonBuffer { + if c.authData.Browser == nil || time.Until(c.authData.TachyonExpiry) > RefreshTachyonBuffer { return nil } c.Logger.Debug().Time("tachyon_expiry", c.authData.TachyonExpiry).Msg("Refreshing auth token") @@ -320,7 +322,7 @@ func (c *Client) refreshAuthToken() error { TachyonAuthToken: c.authData.TachyonAuthToken, ConfigVersion: payload.ConfigMessage, }, - CurrBrowserDevice: c.authData.DevicePair.Browser, + CurrBrowserDevice: c.authData.Browser, UnixTimestamp: timestamp, Signature: sig, EmptyRefreshArr: &binary.EmptyRefreshArr{EmptyArr: &binary.EmptyArr{}}, diff --git a/libgm/media_processor.go b/libgm/media_processor.go index 7eb0f0a..6c1c492 100644 --- a/libgm/media_processor.go +++ b/libgm/media_processor.go @@ -134,7 +134,7 @@ func (c *Client) buildStartUploadPayload() (string, error) { TachyonAuthToken: c.authData.TachyonAuthToken, ConfigVersion: payload.ConfigMessage, }, - Mobile: c.authData.DevicePair.Mobile, + Mobile: c.authData.Mobile, } protoDataBytes, err := proto.Marshal(protoData) diff --git a/libgm/pair.go b/libgm/pair.go index 0f1e657..8deef46 100644 --- a/libgm/pair.go +++ b/libgm/pair.go @@ -169,7 +169,7 @@ func (c *Client) GetWebEncryptionKey() (*binary.WebEncryptionKeyResponse, error) } func (c *Client) Unpair() (*binary.RevokeRelayPairingResponse, error) { - if c.authData.TachyonAuthToken == nil || c.authData.DevicePair == nil || c.authData.DevicePair.Browser == nil { + if c.authData.TachyonAuthToken == nil || c.authData.Browser == nil { return nil, nil } payload, err := proto.Marshal(&binary.RevokeRelayPairing{ @@ -178,7 +178,7 @@ func (c *Client) Unpair() (*binary.RevokeRelayPairingResponse, error) { TachyonAuthToken: c.authData.TachyonAuthToken, ConfigVersion: payload.ConfigMessage, }, - Browser: c.authData.DevicePair.Browser, + Browser: c.authData.Browser, }) if err != nil { return nil, err diff --git a/libgm/pairing_handler.go b/libgm/pairing_handler.go index 8e90a34..54a5c7e 100644 --- a/libgm/pairing_handler.go +++ b/libgm/pairing_handler.go @@ -41,8 +41,7 @@ func (c *Client) pairCallback(data *binary.PairedData) error { tokenData := data.GetTokenData() c.updateTachyonAuthToken(tokenData.GetTachyonAuthToken(), tokenData.GetTTL()) - devicePair := c.NewDevicePair(data.Mobile, data.Browser) - c.updateDevicePair(devicePair) + c.updateDevicePair(data.Mobile, data.Browser) webEncryptionKeyResponse, webErr := c.GetWebEncryptionKey() if webErr != nil { diff --git a/libgm/rpc.go b/libgm/rpc.go index dcd896d..ff6d1db 100644 --- a/libgm/rpc.go +++ b/libgm/rpc.go @@ -91,7 +91,7 @@ func (r *RPC) ListenReceiveMessages() { } r.client.Logger.Debug().Int("statusCode", resp.StatusCode).Msg("Long polling opened") r.conn = resp.Body - if r.client.authData.DevicePair != nil { + if r.client.authData.Browser != nil { go func() { err := r.client.NotifyDittoActivity() if err != nil { diff --git a/libgm/session_handler.go b/libgm/session_handler.go index 064c72a..429a601 100644 --- a/libgm/session_handler.go +++ b/libgm/session_handler.go @@ -73,7 +73,7 @@ func (s *SessionHandler) sendMessage(actionType binary.ActionType, encryptedData func (s *SessionHandler) buildMessage(actionType binary.ActionType, encryptedData proto.Message) (string, []byte, binary.ActionType, error) { var requestID string - pairedDevice := s.client.authData.DevicePair.Mobile + pairedDevice := s.client.authData.Mobile sessionId := s.client.sessionHandler.sessionID token := s.client.authData.TachyonAuthToken @@ -142,7 +142,7 @@ func (s *SessionHandler) sendAckRequest() { for i, reqID := range dataToAck { ackMessages[i] = &binary.AckMessageData{ RequestID: reqID, - Device: s.client.authData.DevicePair.Browser, + Device: s.client.authData.Browser, } } ackMessagePayload := &binary.AckMessagePayload{