Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/definitions/ICertificate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ICertificate {
path: string;
password: string;
}
2 changes: 2 additions & 0 deletions app/definitions/IRocketChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ type TRocketChat = typeof rocketchat;

export interface IRocketChat extends TRocketChat {
sdk: any;
activeUsersSubTimeout: any;
roomsSub: any;
}
2 changes: 2 additions & 0 deletions app/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export * from './IUser';
export * from './IServer';
export * from './ILoggedUser';
export * from './IServerHistory';
export * from './IRocketChat';
export * from './ICertificate';

export interface IBaseScreen<T extends Record<string, object | undefined>, S extends string> {
navigation: StackNavigationProp<T, S>;
Expand Down
43 changes: 24 additions & 19 deletions app/lib/methods/logout.js → app/lib/methods/logout.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
import * as FileSystem from 'expo-file-system';
import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk';
import Model from '@nozbe/watermelondb/Model';

import { getDeviceToken } from '../../notifications/push';
import { extractHostname } from '../../utils/server';
import { BASIC_AUTH_KEY } from '../../utils/fetch';
import database, { getDatabase } from '../database';
import RocketChat from '../rocketchat';
import { useSsl } from '../../utils/url';
import log from '../../utils/log';
import { E2E_PRIVATE_KEY, E2E_PUBLIC_KEY, E2E_RANDOM_PASSWORD_KEY } from '../encryption/constants';
import UserPreferences from '../userPreferences';
import { ICertificate, IRocketChat } from '../../definitions';

async function removeServerKeys({ server, userId }) {
async function removeServerKeys({ server, userId }: { server: string; userId: string | null }) {
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${server}`);
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`);
if (userId) {
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`);
}
await UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`);
await UserPreferences.removeItem(`${server}-${E2E_PUBLIC_KEY}`);
await UserPreferences.removeItem(`${server}-${E2E_PRIVATE_KEY}`);
await UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
}

async function removeSharedCredentials({ server }) {
async function removeSharedCredentials({ server }: { server: string }) {
// clear certificate for server - SSL Pinning
try {
const certificate = await UserPreferences.getMapAsync(extractHostname(server));
if (certificate && certificate.path) {
const certificate = (await UserPreferences.getMapAsync(extractHostname(server))) as ICertificate | null;
if (certificate?.path) {
await UserPreferences.removeItem(extractHostname(server));
await FileSystem.deleteAsync(certificate.path);
}
} catch (e) {
console.log('removeSharedCredentials', e);
log(e);
}
}

async function removeServerData({ server }) {
async function removeServerData({ server }: { server: string }) {
try {
const batch = [];
const batch: Model[] = [];
const serversDB = database.servers;
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);

Expand All @@ -47,28 +52,28 @@ async function removeServerData({ server }) {
const serverRecord = await serverCollection.find(server);
batch.push(serverRecord.prepareDestroyPermanently());

await serversDB.action(() => serversDB.batch(...batch));
await serversDB.write(() => serversDB.batch(...batch));
await removeSharedCredentials({ server });
await removeServerKeys({ server });
await removeServerKeys({ server, userId });
} catch (e) {
console.log('removeServerData', e);
log(e);
}
}

async function removeCurrentServer() {
await UserPreferences.removeItem(RocketChat.CURRENT_SERVER);
}

async function removeServerDatabase({ server }) {
async function removeServerDatabase({ server }: { server: string }) {
try {
const db = getDatabase(server);
await db.action(() => db.unsafeResetDatabase());
await db.write(() => db.unsafeResetDatabase());
} catch (e) {
console.log(e);
log(e);
}
}

export async function removeServer({ server }) {
export async function removeServer({ server }: { server: string }): Promise<void> {
try {
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);
if (userId) {
Expand All @@ -88,11 +93,11 @@ export async function removeServer({ server }) {
await removeServerData({ server });
await removeServerDatabase({ server });
} catch (e) {
console.log('removeServer', e);
log(e);
}
}

export default async function logout({ server }) {
export default async function logout(this: IRocketChat, { server }: { server: string }): Promise<void> {
if (this.roomsSub) {
this.roomsSub.stop();
this.roomsSub = null;
Expand All @@ -106,14 +111,14 @@ export default async function logout({ server }) {
try {
await this.removePushToken();
} catch (e) {
console.log('removePushToken', e);
log(e);
}

try {
// RC 0.60.0
await this.sdk.logout();
} catch (e) {
console.log('logout', e);
log(e);
}

if (this.sdk) {
Expand Down
6 changes: 1 addition & 5 deletions app/utils/sslPinning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as FileSystem from 'expo-file-system';
import UserPreferences from '../lib/userPreferences';
import I18n from '../i18n';
import { extractHostname } from './server';
import { ICertificate } from '../definitions';

const { SSLPinning } = NativeModules;
const { documentDirectory } = FileSystem;
Expand All @@ -13,11 +14,6 @@ const extractFileScheme = (path: string) => path.replace('file://', ''); // file

const getPath = (name: string) => `${documentDirectory}/${name}`;

interface ICertificate {
path: string;
password: string;
}

const persistCertificate = async (name: string, password: string) => {
const certificatePath = getPath(name);
const certificate: ICertificate = {
Expand Down