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.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

@ -502,7 +502,7 @@ export default class AccountManager extends EventTarget {
password,
registrationId,
encryptedDeviceName,
{ accessKey }
{ accessKey, 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.protocol.hydrateCaches();
// We are finally ready to reconnect
window.textsecure.storage.user.emitCredentialsChanged(
'AccountManager.createAccount'
);
}
async clearSessionsAndPreKeys() {

View file

@ -376,6 +376,8 @@ export class SocketManager extends EventListener {
this.authenticated?.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>([
// MessageController
'messages',
'multiRecipient',
'reportMessage',
// ProfileController
@ -710,9 +711,6 @@ const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
'directoryAuth',
// Storage
'storageManifest',
'storageModify',
'storageRead',
'storageToken',
]);
@ -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,
@ -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,
@ -1499,7 +1497,7 @@ export function initialize({
senderKey: true,
};
const { accessKey } = options;
const { accessKey, uuid } = options;
const jsonData: any = {
capabilities,
fetchesMessages: true,
@ -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;
}
@ -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),
},

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);
}
}