2021-06-15 00:09:37 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
import { WebAPICredentials } from '../Types.d';
|
|
|
|
|
2021-08-05 23:34:49 +00:00
|
|
|
import { strictAssert } from '../../util/assert';
|
2021-06-15 00:09:37 +00:00
|
|
|
import { StorageInterface } from '../../types/Storage.d';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { UUID } from '../../types/UUID';
|
2021-06-15 00:09:37 +00:00
|
|
|
|
|
|
|
import Helpers from '../Helpers';
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
export type SetCredentialsOptions = {
|
|
|
|
uuid?: string;
|
|
|
|
number: string;
|
|
|
|
deviceId: number;
|
|
|
|
deviceName?: string;
|
|
|
|
password: string;
|
|
|
|
};
|
2021-06-15 00:09:37 +00:00
|
|
|
|
2021-08-04 20:12:35 +00:00
|
|
|
export class User {
|
|
|
|
constructor(private readonly storage: StorageInterface) {}
|
2021-06-15 00:09:37 +00:00
|
|
|
|
|
|
|
public async setUuidAndDeviceId(
|
|
|
|
uuid: string,
|
|
|
|
deviceId: number
|
|
|
|
): Promise<void> {
|
2021-07-23 17:23:50 +00:00
|
|
|
await this.storage.put('uuid_id', `${uuid}.${deviceId}`);
|
|
|
|
|
|
|
|
window.log.info('storage.user: uuid and device id changed');
|
2021-06-15 00:09:37 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 23:34:49 +00:00
|
|
|
public async setNumber(number: string): Promise<void> {
|
|
|
|
if (this.getNumber() === number) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const deviceId = this.getDeviceId();
|
|
|
|
strictAssert(
|
|
|
|
deviceId !== undefined,
|
|
|
|
'Cannot update device number without knowing device id'
|
|
|
|
);
|
|
|
|
|
|
|
|
window.log.info('storage.user: number changed');
|
|
|
|
|
2021-09-02 17:12:11 +00:00
|
|
|
await Promise.all([
|
|
|
|
this.storage.put('number_id', `${number}.${deviceId}`),
|
|
|
|
this.storage.remove('senderCertificate'),
|
|
|
|
]);
|
2021-08-05 23:34:49 +00:00
|
|
|
|
|
|
|
// Notify redux about phone number change
|
2021-09-02 21:47:42 +00:00
|
|
|
window.Whisper.events.trigger('userChanged', true);
|
2021-08-05 23:34:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
public getNumber(): string | undefined {
|
|
|
|
const numberId = this.storage.get('number_id');
|
|
|
|
if (numberId === undefined) return undefined;
|
|
|
|
return Helpers.unencodeNumber(numberId)[0];
|
|
|
|
}
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
public getUuid(): UUID | undefined {
|
2021-06-15 00:09:37 +00:00
|
|
|
const uuid = this.storage.get('uuid_id');
|
|
|
|
if (uuid === undefined) return undefined;
|
2021-09-10 02:38:11 +00:00
|
|
|
return new UUID(Helpers.unencodeNumber(uuid.toLowerCase())[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getCheckedUuid(): UUID {
|
|
|
|
const uuid = this.getUuid();
|
|
|
|
strictAssert(uuid !== undefined, 'Must have our own uuid');
|
|
|
|
return uuid;
|
2021-06-15 00:09:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getDeviceId(): number | undefined {
|
|
|
|
const value = this._getDeviceIdFromUuid() || this._getDeviceIdFromNumber();
|
|
|
|
if (value === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return parseInt(value, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getDeviceName(): string | undefined {
|
|
|
|
return this.storage.get('device_name');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async setDeviceNameEncrypted(): Promise<void> {
|
|
|
|
return this.storage.put('deviceNameEncrypted', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getDeviceNameEncrypted(): boolean | undefined {
|
|
|
|
return this.storage.get('deviceNameEncrypted');
|
|
|
|
}
|
|
|
|
|
2021-07-20 01:11:07 +00:00
|
|
|
public async removeSignalingKey(): Promise<void> {
|
|
|
|
return this.storage.remove('signaling_key');
|
2021-06-15 00:09:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
public async setCredentials(
|
|
|
|
credentials: SetCredentialsOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const { uuid, number, deviceId, deviceName, password } = credentials;
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
this.storage.put('number_id', `${number}.${deviceId}`),
|
|
|
|
this.storage.put('uuid_id', `${uuid}.${deviceId}`),
|
|
|
|
this.storage.put('password', password),
|
|
|
|
deviceName
|
|
|
|
? this.storage.put('device_name', deviceName)
|
|
|
|
: Promise.resolve(),
|
|
|
|
]);
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
2021-07-23 17:23:50 +00:00
|
|
|
|
|
|
|
public async removeCredentials(): Promise<void> {
|
2021-07-28 21:37:09 +00:00
|
|
|
window.log.info('storage.user: removeCredentials');
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
await Promise.all([
|
|
|
|
this.storage.remove('number_id'),
|
|
|
|
this.storage.remove('uuid_id'),
|
|
|
|
this.storage.remove('password'),
|
|
|
|
this.storage.remove('device_name'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getWebAPICredentials(): WebAPICredentials {
|
|
|
|
return {
|
|
|
|
username:
|
|
|
|
this.storage.get('uuid_id') || this.storage.get('number_id') || '',
|
|
|
|
password: this.storage.get('password', ''),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
private _getDeviceIdFromUuid(): string | undefined {
|
|
|
|
const uuid = this.storage.get('uuid_id');
|
|
|
|
if (uuid === undefined) return undefined;
|
|
|
|
return Helpers.unencodeNumber(uuid)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getDeviceIdFromNumber(): string | undefined {
|
|
|
|
const numberId = this.storage.get('number_id');
|
|
|
|
if (numberId === undefined) return undefined;
|
|
|
|
return Helpers.unencodeNumber(numberId)[1];
|
|
|
|
}
|
|
|
|
}
|