Skip to content

Commit

Permalink
confirmCode endpoint shouldn't reconnect socket
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Aug 4, 2021
1 parent f048066 commit c68fd3d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 59 deletions.
16 changes: 9 additions & 7 deletions ts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,6 @@ export async function startApp(): Promise<void> {
);
window.textsecure.server = server;

window.textsecure.storage.user.on('credentialsChange', async () => {
strictAssert(server !== undefined, 'WebAPI not ready');
await server.authenticate(
window.textsecure.storage.user.getWebAPICredentials()
);
});

initializeAllJobQueues({
server,
});
Expand Down Expand Up @@ -1906,6 +1899,11 @@ export async function startApp(): Promise<void> {
window.log.info('listening for registration events');
window.Whisper.events.on('registration_done', () => {
window.log.info('handling registration event');

strictAssert(server !== undefined, 'WebAPI not ready');
server.authenticate(
window.textsecure.storage.user.getWebAPICredentials()
);
connect(true);
});

Expand Down Expand Up @@ -2296,6 +2294,10 @@ export async function startApp(): Promise<void> {
'private'
);
me.updateUuid(uuid);

await server.authenticate(
window.textsecure.storage.user.getWebAPICredentials()
);
} catch (error) {
window.log.error(
'Error: Unable to retrieve UUID from service.',
Expand Down
7 changes: 1 addition & 6 deletions ts/textsecure/AccountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export default class AccountManager extends EventTarget {
password,
registrationId,
encryptedDeviceName,
{ accessKey }
{ accessKey, uuid }
);

const uuidChanged = previousUuid && uuid && previousUuid !== uuid;
Expand Down Expand Up @@ -603,11 +603,6 @@ export default class AccountManager extends EventTarget {
);
await window.textsecure.storage.put('regionCode', regionCode);
await window.textsecure.storage.protocol.hydrateCaches();

// We are finally ready to reconnect
window.textsecure.storage.user.emitCredentialsChanged(
'AccountManager.createAccount'
);
}

async clearSessionsAndPreKeys() {
Expand Down
2 changes: 2 additions & 0 deletions ts/textsecure/SocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ export class SocketManager extends EventListener {

this.authenticated?.abort();
this.unauthenticated?.abort();
this.authenticated = undefined;
this.unauthenticated = undefined;
}

//
Expand Down
33 changes: 20 additions & 13 deletions ts/textsecure/WebAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ const URL_CALLS = {
const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
// MessageController
'messages',
'multiRecipient',
'reportMessage',

// ProfileController
Expand All @@ -710,9 +711,6 @@ const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
'directoryAuth',

// Storage
'storageManifest',
'storageModify',
'storageRead',
'storageToken',
]);

Expand Down Expand Up @@ -819,7 +817,7 @@ export type WebAPIType = {
newPassword: string,
registrationId: number,
deviceName?: string | null,
options?: { accessKey?: ArrayBuffer }
options?: { accessKey?: ArrayBuffer; uuid?: string }
) => Promise<any>;
createGroup: (
group: Proto.IGroup,
Expand Down Expand Up @@ -1490,7 +1488,7 @@ export function initialize({
newPassword: string,
registrationId: number,
deviceName?: string | null,
options: { accessKey?: ArrayBuffer } = {}
options: { accessKey?: ArrayBuffer; uuid?: string } = {}
) {
const capabilities: CapabilitiesUploadType = {
announcementGroup: true,
Expand All @@ -1499,7 +1497,7 @@ export function initialize({
senderKey: true,
};

const { accessKey } = options;
const { accessKey, uuid } = options;
const jsonData: any = {
capabilities,
fetchesMessages: true,
Expand All @@ -1515,21 +1513,29 @@ export function initialize({
const call = deviceName ? 'devices' : 'accounts';
const urlPrefix = deviceName ? '/' : '/code/';

// Reset old websocket credentials and disconnect.
// AccountManager is our only caller and it will trigger
// `registration_done` which will update credentials.
await socketManager.authenticate({
username: '',
password: '',
});

// Update REST credentials, though. We need them for the call below
username = number;
password = newPassword;

const response = await _ajax({
call,
httpType: 'PUT',
responseType: 'json',
urlParameters: urlPrefix + code,
jsonData,
username: number,
password: newPassword,
});

// From here on out, our username will be our UUID or E164 combined with device
await authenticate({
username: `${response.uuid || number}.${response.deviceId || 1}`,
password,
});
// Set final REST credentials to let `registerKeys` succeed.
username = `${uuid || response.uuid || number}.${response.deviceId || 1}`;
password = newPassword;

return response;
}
Expand Down Expand Up @@ -1792,6 +1798,7 @@ export function initialize({
data,
urlParameters: `?ts=${timestamp}&online=${online ? 'true' : 'false'}`,
responseType: 'json',
unauthenticated: true,
headers: {
'Unidentified-Access-Key': arrayBufferToBase64(accessKeys),
},
Expand Down
35 changes: 2 additions & 33 deletions ts/textsecure/storage/User.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { EventEmitter } from 'events';

import { WebAPICredentials } from '../Types.d';

import { StorageInterface } from '../../types/Storage.d';
Expand All @@ -17,10 +15,8 @@ export type SetCredentialsOptions = {
password: string;
};

export class User extends EventEmitter {
constructor(private readonly storage: StorageInterface) {
super();
}
export class User {
constructor(private readonly storage: StorageInterface) {}

public async setUuidAndDeviceId(
uuid: string,
Expand All @@ -29,7 +25,6 @@ export class User extends EventEmitter {
await this.storage.put('uuid_id', `${uuid}.${deviceId}`);

window.log.info('storage.user: uuid and device id changed');
this.emit('credentialsChange');
}

public getNumber(): string | undefined {
Expand Down Expand Up @@ -83,11 +78,6 @@ export class User extends EventEmitter {
]);
}

public emitCredentialsChanged(reason: string): void {
window.log.info(`storage.user: credentials changed, ${reason}`);
this.emit('credentialsChange');
}

public async removeCredentials(): Promise<void> {
window.log.info('storage.user: removeCredentials');

Expand Down Expand Up @@ -118,25 +108,4 @@ export class User extends EventEmitter {
if (numberId === undefined) return undefined;
return Helpers.unencodeNumber(numberId)[1];
}

//
// EventEmitter typing
//

public on(type: 'credentialsChange', callback: () => void): this;

public on(
type: string | symbol,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listener: (...args: Array<any>) => void
): this {
return super.on(type, listener);
}

public emit(type: 'credentialsChange'): boolean;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public emit(type: string | symbol, ...args: Array<any>): boolean {
return super.emit(type, ...args);
}
}

0 comments on commit c68fd3d

Please sign in to comment.