Compute provisioning URL in separate function

This commit is contained in:
Evan Hahn 2021-06-07 11:27:02 -05:00 committed by GitHub
parent 6e2d0ff2ae
commit eaf4036fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 7 deletions

View File

@ -0,0 +1,23 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { size } from '../../util/iterables';
import { getProvisioningUrl } from '../../util/getProvisioningUrl';
describe('getProvisioningUrl', () => {
it('returns a URL with a UUID and public key', () => {
const uuid = 'a08bf1fd-1799-427f-a551-70af747e3956';
const publicKey = new Uint8Array([9, 8, 7, 6, 5, 4, 3]);
const result = getProvisioningUrl(uuid, publicKey);
const resultUrl = new URL(result);
assert(result.startsWith('tsdevice:/?'));
assert.strictEqual(resultUrl.protocol, 'tsdevice:');
assert.strictEqual(size(resultUrl.searchParams.entries()), 2);
assert.strictEqual(resultUrl.searchParams.get('uuid'), uuid);
assert.strictEqual(resultUrl.searchParams.get('pub_key'), 'CQgHBgUEAw==');
});
});

View File

@ -1,4 +1,4 @@
// Copyright 2020 Signal Messenger, LLC
// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable @typescript-eslint/no-explicit-any */
@ -29,6 +29,7 @@ import {
} from '../Curve';
import { isMoreRecentThan, isOlderThan } from '../util/timestamp';
import { ourProfileKeyService } from '../services/ourProfileKey';
import { getProvisioningUrl } from '../util/getProvisioningUrl';
const ARCHIVE_AGE = 30 * 24 * 60 * 60 * 1000;
const PREKEY_ROTATION_AGE = 24 * 60 * 60 * 1000;
@ -222,12 +223,11 @@ export default class AccountManager extends EventTarget {
const proto = window.textsecure.protobuf.ProvisioningUuid.decode(
request.body
);
const url = [
'tsdevice:/?uuid=',
proto.uuid,
'&pub_key=',
encodeURIComponent(btoa(utils.getString(pubKey))),
].join('');
const { uuid } = proto;
if (!uuid) {
throw new Error('registerSecondDevice: expected a UUID');
}
const url = getProvisioningUrl(uuid, pubKey);
if (window.CI) {
window.CI.setProvisioningURL(url);

View File

@ -0,0 +1,15 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { arrayBufferToBase64 } from '../Crypto';
export function getProvisioningUrl(
uuid: string,
publicKey: ArrayBuffer
): string {
const params = new URLSearchParams({
uuid,
pub_key: arrayBufferToBase64(publicKey),
});
return `tsdevice:/?${params.toString()}`;
}