diff --git a/ts/textsecure/SocketManager.ts b/ts/textsecure/SocketManager.ts index 0ae545977a0e..9a4597234b86 100644 --- a/ts/textsecure/SocketManager.ts +++ b/ts/textsecure/SocketManager.ts @@ -268,10 +268,8 @@ export class SocketManager extends EventListener { public async fetch(url: string, init: RequestInit): Promise { const headers = new Headers(init.headers); - const isAuthenticated = headers.has('Authorization'); - let resource: WebSocketResource; - if (isAuthenticated) { + if (this.isAuthenticated(headers)) { resource = await this.getAuthenticatedResource(); } else { resource = await this.getUnauthenticatedResource(); @@ -618,6 +616,33 @@ export class SocketManager extends EventListener { } } + private isAuthenticated(headers: Headers): boolean { + if (!this.credentials) { + return false; + } + + const authorization = headers.get('Authorization'); + if (!authorization) { + return false; + } + + const [basic, base64] = authorization.split(/\s+/, 2); + + if (basic.toLowerCase() !== 'basic' || !base64) { + return false; + } + + const [username, password] = Bytes.toString(Bytes.fromBase64(base64)).split( + ':', + 2 + ); + + return ( + username === this.credentials.username && + password === this.credentials.password + ); + } + // EventEmitter types public on(type: 'authError', callback: (error: HTTPError) => void): this; diff --git a/ts/textsecure/WebAPI.ts b/ts/textsecure/WebAPI.ts index 7d1c395b86a9..4589a00f5e83 100644 --- a/ts/textsecure/WebAPI.ts +++ b/ts/textsecure/WebAPI.ts @@ -696,6 +696,24 @@ const WEBSOCKET_CALLS = new Set([ // RemoteConfigController 'config', + + // Certificate + 'deliveryCert', + 'getGroupCredentials', + + // Devices + 'devices', + 'registerCapabilities', + 'supportUnauthenticatedDelivery', + + // Directory + 'directoryAuth', + + // Storage + 'storageManifest', + 'storageModify', + 'storageRead', + 'storageToken', ]); type InitializeOptionsType = { @@ -1497,15 +1515,14 @@ 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 }); - 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