diff --git a/lib/types/tunnelbroker/qr-code-auth-message-types.js b/lib/types/tunnelbroker/qr-code-auth-message-types.js --- a/lib/types/tunnelbroker/qr-code-auth-message-types.js +++ b/lib/types/tunnelbroker/qr-code-auth-message-types.js @@ -6,21 +6,37 @@ import { tShape, tString, tUserID } from '../../utils/validation-utils.js'; export const qrCodeAuthMessageTypes = Object.freeze({ + // Sent by primary device DEVICE_LIST_UPDATE_SUCCESS: 'DeviceListUpdateSuccess', + // Sent by secondary device SECONDARY_DEVICE_REGISTRATION_SUCCESS: 'SecondaryDeviceRegistrationSuccess', - BACKUP_DATA_KEY_MESSAGE: 'BackupDataKeyMessage', }); +type QRAuthBackupData = { + +backupID: string, + +backupDataKey: string, + +backupLogDataKey: string, +}; +export const qrAuthBackupDataValidator: TInterface = + tShape({ + backupID: t.String, + backupDataKey: t.String, + backupLogDataKey: t.String, + }); + export type DeviceListUpdateSuccess = { +type: 'DeviceListUpdateSuccess', +userID: string, +primaryDeviceID: string, + // We don't need `backupData` for the keyserver + +backupData: ?QRAuthBackupData, }; export const deviceListUpdateSuccessValidator: TInterface = tShape({ type: tString(qrCodeAuthMessageTypes.DEVICE_LIST_UPDATE_SUCCESS), userID: tUserID, primaryDeviceID: t.String, + backupData: t.maybe(qrAuthBackupDataValidator), }); export type SecondaryDeviceRegistrationSuccess = { @@ -33,28 +49,12 @@ requestBackupKeys: t.Boolean, }); -export type BackupDataKeyMessage = { - +type: 'BackupDataKeyMessage', - +backupID: string, - +backupDataKey: string, - +backupLogDataKey: string, -}; -export const backupDataKeyMessageValidator: TInterface = - tShape({ - type: tString(qrCodeAuthMessageTypes.BACKUP_DATA_KEY_MESSAGE), - backupID: t.String, - backupDataKey: t.String, - backupLogDataKey: t.String, - }); - export type QRCodeAuthMessagePayload = | DeviceListUpdateSuccess - | SecondaryDeviceRegistrationSuccess - | BackupDataKeyMessage; + | SecondaryDeviceRegistrationSuccess; export const qrCodeAuthMessagePayloadValidator: TUnion = t.union([ deviceListUpdateSuccessValidator, secondaryDeviceRegistrationSuccessValidator, - backupDataKeyMessageValidator, ]); diff --git a/native/profile/secondary-device-qr-code-scanner.react.js b/native/profile/secondary-device-qr-code-scanner.react.js --- a/native/profile/secondary-device-qr-code-scanner.react.js +++ b/native/profile/secondary-device-qr-code-scanner.react.js @@ -126,36 +126,11 @@ return; } - const [backupSecret, latestBackupInfo] = await Promise.all([ - getBackupSecret(), - retrieveLatestBackupInfo(), - ]); - const backupKeysResponse = await commCoreModule.retrieveBackupKeys( - backupSecret, - latestBackupInfo.backupID, - ); - const backupKeys = assertWithValidator( - JSON.parse(backupKeysResponse), - backupKeysValidator, - ); - - const backupKeyMessage = await composeTunnelbrokerQRAuthMessage( - encryptionKey, - { - type: qrCodeAuthMessageTypes.BACKUP_DATA_KEY_MESSAGE, - ...backupKeys, - }, - ); - await tunnelbrokerContext.sendMessageToDevice({ - deviceID: targetDeviceID, - payload: JSON.stringify(backupKeyMessage), - }); - Alert.alert('Device added', 'Device registered successfully', [ { text: 'OK', onPress: goBack }, ]); }, - [getBackupSecret, goBack, retrieveLatestBackupInfo, tunnelbrokerContext], + [goBack], ); React.useEffect(() => { @@ -199,10 +174,26 @@ const deviceType = secondaryDeviceType.current; const sendDeviceListUpdateSuccessMessage = async () => { + let backupData = null; + if (deviceType !== identityDeviceTypes.KEYSERVER) { + const [backupSecret, latestBackupInfo] = await Promise.all([ + getBackupSecret(), + retrieveLatestBackupInfo(), + ]); + const backupKeysResponse = await commCoreModule.retrieveBackupKeys( + backupSecret, + latestBackupInfo.backupID, + ); + backupData = assertWithValidator( + JSON.parse(backupKeysResponse), + backupKeysValidator, + ); + } const message = await composeTunnelbrokerQRAuthMessage(encryptionKey, { type: qrCodeAuthMessageTypes.DEVICE_LIST_UPDATE_SUCCESS, userID, primaryDeviceID, + backupData, }); await tunnelbrokerContext.sendMessageToDevice({ deviceID: targetDeviceID, @@ -269,9 +260,11 @@ goBack(); } }, [ + getBackupSecret, goBack, identityContext, keyserverDeviceID, + retrieveLatestBackupInfo, runDeviceListUpdate, tunnelbrokerContext, ]);