confirmCode endpoint shouldn't reconnect socket

This commit is contained in:
Fedor Indutny 2021-08-04 13:12:35 -07:00 committed by GitHub
parent f048066693
commit c68fd3d727
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 59 deletions

View file

@ -142,13 +142,6 @@ export async function startApp(): Promise<void> {
); );
window.textsecure.server = server; 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({ initializeAllJobQueues({
server, server,
}); });
@ -1906,6 +1899,11 @@ export async function startApp(): Promise<void> {
window.log.info('listening for registration events'); window.log.info('listening for registration events');
window.Whisper.events.on('registration_done', () => { window.Whisper.events.on('registration_done', () => {
window.log.info('handling registration event'); window.log.info('handling registration event');
strictAssert(server !== undefined, 'WebAPI not ready');
server.authenticate(
window.textsecure.storage.user.getWebAPICredentials()
);
connect(true); connect(true);
}); });
@ -2296,6 +2294,10 @@ export async function startApp(): Promise<void> {
'private' 'private'
); );
me.updateUuid(uuid); me.updateUuid(uuid);
await server.authenticate(
window.textsecure.storage.user.getWebAPICredentials()
);
} catch (error) { } catch (error) {
window.log.error( window.log.error(
'Error: Unable to retrieve UUID from service.', 'Error: Unable to retrieve UUID from service.',

View file

@ -502,7 +502,7 @@ export default class AccountManager extends EventTarget {
password, password,
registrationId, registrationId,
encryptedDeviceName, encryptedDeviceName,
{ accessKey } { accessKey, uuid }
); );
const uuidChanged = previousUuid && uuid && previousUuid !== uuid; const uuidChanged = previousUuid && uuid && previousUuid !== uuid;
@ -603,11 +603,6 @@ export default class AccountManager extends EventTarget {
); );
await window.textsecure.storage.put('regionCode', regionCode); await window.textsecure.storage.put('regionCode', regionCode);
await window.textsecure.storage.protocol.hydrateCaches(); await window.textsecure.storage.protocol.hydrateCaches();
// We are finally ready to reconnect
window.textsecure.storage.user.emitCredentialsChanged(
'AccountManager.createAccount'
);
} }
async clearSessionsAndPreKeys() { async clearSessionsAndPreKeys() {

View file

@ -376,6 +376,8 @@ export class SocketManager extends EventListener {
this.authenticated?.abort(); this.authenticated?.abort();
this.unauthenticated?.abort(); this.unauthenticated?.abort();
this.authenticated = undefined;
this.unauthenticated = undefined;
} }
// //

View file

@ -686,6 +686,7 @@ const URL_CALLS = {
const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([ const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
// MessageController // MessageController
'messages', 'messages',
'multiRecipient',
'reportMessage', 'reportMessage',
// ProfileController // ProfileController
@ -710,9 +711,6 @@ const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
'directoryAuth', 'directoryAuth',
// Storage // Storage
'storageManifest',
'storageModify',
'storageRead',
'storageToken', 'storageToken',
]); ]);
@ -819,7 +817,7 @@ export type WebAPIType = {
newPassword: string, newPassword: string,
registrationId: number, registrationId: number,
deviceName?: string | null, deviceName?: string | null,
options?: { accessKey?: ArrayBuffer } options?: { accessKey?: ArrayBuffer; uuid?: string }
) => Promise<any>; ) => Promise<any>;
createGroup: ( createGroup: (
group: Proto.IGroup, group: Proto.IGroup,
@ -1490,7 +1488,7 @@ export function initialize({
newPassword: string, newPassword: string,
registrationId: number, registrationId: number,
deviceName?: string | null, deviceName?: string | null,
options: { accessKey?: ArrayBuffer } = {} options: { accessKey?: ArrayBuffer; uuid?: string } = {}
) { ) {
const capabilities: CapabilitiesUploadType = { const capabilities: CapabilitiesUploadType = {
announcementGroup: true, announcementGroup: true,
@ -1499,7 +1497,7 @@ export function initialize({
senderKey: true, senderKey: true,
}; };
const { accessKey } = options; const { accessKey, uuid } = options;
const jsonData: any = { const jsonData: any = {
capabilities, capabilities,
fetchesMessages: true, fetchesMessages: true,
@ -1515,21 +1513,29 @@ export function initialize({
const call = deviceName ? 'devices' : 'accounts'; const call = deviceName ? 'devices' : 'accounts';
const urlPrefix = deviceName ? '/' : '/code/'; 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({ const response = await _ajax({
call, call,
httpType: 'PUT', httpType: 'PUT',
responseType: 'json', responseType: 'json',
urlParameters: urlPrefix + code, urlParameters: urlPrefix + code,
jsonData, jsonData,
username: number,
password: newPassword,
}); });
// From here on out, our username will be our UUID or E164 combined with device // Set final REST credentials to let `registerKeys` succeed.
await authenticate({ username = `${uuid || response.uuid || number}.${response.deviceId || 1}`;
username: `${response.uuid || number}.${response.deviceId || 1}`, password = newPassword;
password,
});
return response; return response;
} }
@ -1792,6 +1798,7 @@ export function initialize({
data, data,
urlParameters: `?ts=${timestamp}&online=${online ? 'true' : 'false'}`, urlParameters: `?ts=${timestamp}&online=${online ? 'true' : 'false'}`,
responseType: 'json', responseType: 'json',
unauthenticated: true,
headers: { headers: {
'Unidentified-Access-Key': arrayBufferToBase64(accessKeys), 'Unidentified-Access-Key': arrayBufferToBase64(accessKeys),
}, },

View file

@ -1,8 +1,6 @@
// Copyright 2021 Signal Messenger, LLC // Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import { EventEmitter } from 'events';
import { WebAPICredentials } from '../Types.d'; import { WebAPICredentials } from '../Types.d';
import { StorageInterface } from '../../types/Storage.d'; import { StorageInterface } from '../../types/Storage.d';
@ -17,10 +15,8 @@ export type SetCredentialsOptions = {
password: string; password: string;
}; };
export class User extends EventEmitter { export class User {
constructor(private readonly storage: StorageInterface) { constructor(private readonly storage: StorageInterface) {}
super();
}
public async setUuidAndDeviceId( public async setUuidAndDeviceId(
uuid: string, uuid: string,
@ -29,7 +25,6 @@ export class User extends EventEmitter {
await this.storage.put('uuid_id', `${uuid}.${deviceId}`); await this.storage.put('uuid_id', `${uuid}.${deviceId}`);
window.log.info('storage.user: uuid and device id changed'); window.log.info('storage.user: uuid and device id changed');
this.emit('credentialsChange');
} }
public getNumber(): string | undefined { public getNumber(): string | undefined {
@ -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> { public async removeCredentials(): Promise<void> {
window.log.info('storage.user: removeCredentials'); window.log.info('storage.user: removeCredentials');
@ -118,25 +108,4 @@ export class User extends EventEmitter {
if (numberId === undefined) return undefined; if (numberId === undefined) return undefined;
return Helpers.unencodeNumber(numberId)[1]; 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);
}
} }