Skip to content

Commit

Permalink
Simplify OutgoingIdentityKeyError, use it in getKeysForIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Feb 25, 2022
1 parent e4b4a0c commit fcb96bf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion ts/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ export async function modifyGroupV2({
await conversationJobQueue.add({
type: conversationQueueJobEnum.enum.GroupUpdate,
conversationId: conversation.id,
groupChangeBase64: Bytes.toBase64(groupChangeBuffer),
groupChangeBase64,
recipients: groupV2Info.members,
revision: groupV2Info.revision,
});
Expand Down
7 changes: 1 addition & 6 deletions ts/test-electron/util/sendToGroup_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,7 @@ describe('sendToGroup', () => {
it('returns true for certain types of error subclasses', async () => {
assert.isTrue(
_shouldFailSend(
new OutgoingIdentityKeyError(
'something',
new Uint8Array(),
200,
new Uint8Array()
),
new OutgoingIdentityKeyError('something'),
'testing OutgoingIdentityKeyError'
)
);
Expand Down
10 changes: 1 addition & 9 deletions ts/textsecure/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,8 @@ export class ReplayableError extends Error {
export class OutgoingIdentityKeyError extends ReplayableError {
identifier: string;

identityKey: Uint8Array;

// Note: Data to resend message is no longer captured
constructor(
incomingIdentifier: string,
_m: Uint8Array,
_t: number,
identityKey: Uint8Array
) {
constructor(incomingIdentifier: string) {
const identifier = incomingIdentifier.split('.')[0];

super({
Expand All @@ -90,7 +83,6 @@ export class OutgoingIdentityKeyError extends ReplayableError {
});

this.identifier = identifier;
this.identityKey = identityKey;
}
}

Expand Down
7 changes: 1 addition & 6 deletions ts/textsecure/OutgoingMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,7 @@ export default class OutgoingMessage {
await this.reloadDevicesAndSend(identifier, true)();
} catch (error) {
if (error?.message?.includes('untrusted identity for address')) {
const newError = new OutgoingIdentityKeyError(
identifier,
error.originalMessage,
error.timestamp,
error.identityKey && new Uint8Array(error.identityKey)
);
const newError = new OutgoingIdentityKeyError(identifier);
this.registerError(identifier, 'Untrusted identity', newError);
} else {
this.registerError(
Expand Down
35 changes: 26 additions & 9 deletions ts/textsecure/getKeysForIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import {
PublicKey,
} from '@signalapp/signal-client';

import { UnregisteredUserError, HTTPError } from './Errors';
import {
UnregisteredUserError,
HTTPError,
OutgoingIdentityKeyError,
} from './Errors';
import { Sessions, IdentityKeys } from '../LibSignalStores';
import { Address } from '../types/Address';
import { QualifiedAddress } from '../types/QualifiedAddress';
import { UUID } from '../types/UUID';
import type { ServerKeysType, WebAPIType } from './WebAPI';
import * as log from '../logging/log';
import { isRecord } from '../util/isRecord';

export async function getKeysForIdentifier(
identifier: string,
Expand Down Expand Up @@ -54,20 +59,32 @@ async function getServerKeys(
server: WebAPIType,
accessKey?: string
): Promise<{ accessKeyFailed?: boolean; keys: ServerKeysType }> {
if (!accessKey) {
return {
keys: await server.getKeysForIdentifier(identifier),
};
}

try {
if (!accessKey) {
return {
keys: await server.getKeysForIdentifier(identifier),
};
}

return {
keys: await server.getKeysForIdentifierUnauth(identifier, undefined, {
accessKey,
}),
};
} catch (error) {
if (error.code === 401 || error.code === 403) {
} catch (error: unknown) {
if (
error instanceof Error &&
error.message.includes('untrusted identity')
) {
throw new OutgoingIdentityKeyError(identifier);
}

if (
accessKey &&
isRecord(error) &&
typeof error.code === 'number' &&
(error.code === 401 || error.code === 403)
) {
return {
accessKeyFailed: true,
keys: await server.getKeysForIdentifier(identifier),
Expand Down
4 changes: 2 additions & 2 deletions ts/util/sendToGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1268,8 +1268,8 @@ async function fetchKeysForIdentifier(
});
window.Signal.Data.updateConversation(emptyConversation.attributes);
}
} catch (error) {
if (error.name === 'UnregisteredUserError') {
} catch (error: unknown) {
if (error instanceof UnregisteredUserError) {
await markIdentifierUnregistered(identifier);
return;
}
Expand Down

0 comments on commit fcb96bf

Please sign in to comment.