confirmCode endpoint shouldn't reconnect socket

This commit is contained in:
Fedor Indutny 2021-08-04 12:46:37 -07:00 committed by GitHub
parent b137d65128
commit c506e0de49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 56 deletions

View File

@ -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,
});
@ -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);
});
@ -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.',

View File

@ -521,7 +521,7 @@ export default class AccountManager extends EventTarget {
password,
registrationId,
encryptedDeviceName,
{ accessKey }
{ accessKey, uuid }
);
const uuidChanged = previousUuid && uuid && previousUuid !== uuid;
@ -622,11 +622,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() {

View File

@ -686,6 +686,7 @@ const URL_CALLS = {
const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
// MessageController
'messages',
'multiRecipient',
'reportMessage',
// ProfileController
@ -801,7 +802,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,
@ -1472,7 +1473,7 @@ export function initialize({
newPassword: string,
registrationId: number,
deviceName?: string | null,
options: { accessKey?: ArrayBuffer } = {}
options: { accessKey?: ArrayBuffer; uuid?: string } = {}
) {
const capabilities: CapabilitiesUploadType = {
announcementGroup: true,
@ -1481,7 +1482,7 @@ export function initialize({
senderKey: false,
};
const { accessKey } = options;
const { accessKey, uuid } = options;
const jsonData: any = {
capabilities,
fetchesMessages: true,
@ -1497,8 +1498,17 @@ export function initialize({
const call = deviceName ? 'devices' : 'accounts';
const urlPrefix = deviceName ? '/' : '/code/';
// We update our saved username and password, since we're creating a new account
await authenticate({ username: number, password: newPassword });
// 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,
@ -1508,11 +1518,9 @@ export function initialize({
jsonData,
});
// 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;
}
@ -1775,6 +1783,7 @@ export function initialize({
data,
urlParameters: `?ts=${timestamp}&online=${online ? 'true' : 'false'}`,
responseType: 'json',
unauthenticated: true,
headers: {
'Unidentified-Access-Key': arrayBufferToBase64(accessKeys),
},

View File

@ -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';
@ -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,
@ -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 {
@ -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');
@ -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);
}
}