Skip to content

Remove unused sessionStore #2455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 0 additions & 3 deletions spec/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import MockHttpBackend from 'matrix-mock-request';

import { LocalStorageCryptoStore } from '../src/crypto/store/localStorage-crypto-store';
import { logger } from '../src/logger';
import { WebStorageSessionStore } from "../src/store/session/webstorage";
import { syncPromise } from "./test-utils/test-utils";
import { createClient } from "../src/matrix";
import { ICreateClientOpts, IDownloadKeyResult, MatrixClient, PendingEventOrdering } from "../src/client";
Expand Down Expand Up @@ -53,7 +52,6 @@ export class TestClient {
if (sessionStoreBackend === undefined) {
sessionStoreBackend = new MockStorageApi();
}
const sessionStore = new WebStorageSessionStore(sessionStoreBackend);

this.httpBackend = new MockHttpBackend();

Expand All @@ -62,7 +60,6 @@ export class TestClient {
userId: userId,
accessToken: accessToken,
deviceId: deviceId,
sessionStore: sessionStore,
request: this.httpBackend.requestFn as IHttpOpts["request"],
...options,
};
Expand Down
7 changes: 3 additions & 4 deletions spec/unit/crypto.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import '../olm-loader';
import { EventEmitter } from "events";

import { Crypto } from "../../src/crypto";
import { WebStorageSessionStore } from "../../src/store/session/webstorage";
import { MemoryCryptoStore } from "../../src/crypto/store/memory-crypto-store";
import { MockStorageApi } from "../MockStorageApi";
import { TestClient } from "../TestClient";
Expand All @@ -14,6 +13,7 @@ import { sleep } from "../../src/utils";
import { CRYPTO_ENABLED } from "../../src/client";
import { DeviceInfo } from "../../src/crypto/deviceinfo";
import { logger } from '../../src/logger';
import { MemoryStore } from "../../src";

const Olm = global.Olm;

Expand Down Expand Up @@ -153,7 +153,7 @@ describe("Crypto", function() {

beforeEach(async function() {
const mockStorage = new MockStorageApi();
const sessionStore = new WebStorageSessionStore(mockStorage);
const clientStore = new MemoryStore({ localStorage: mockStorage });
const cryptoStore = new MemoryCryptoStore(mockStorage);

cryptoStore.storeEndToEndDeviceData({
Expand All @@ -180,10 +180,9 @@ describe("Crypto", function() {

crypto = new Crypto(
mockBaseApis,
sessionStore,
"@alice:home.server",
"FLIBBLE",
sessionStore,
clientStore,
cryptoStore,
mockRoomList,
);
Expand Down
15 changes: 5 additions & 10 deletions spec/unit/crypto/backup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import * as olmlib from "../../../src/crypto/olmlib";
import { MatrixClient } from "../../../src/client";
import { MatrixEvent } from "../../../src/models/event";
import * as algorithms from "../../../src/crypto/algorithms";
import { WebStorageSessionStore } from "../../../src/store/session/webstorage";
import { MemoryCryptoStore } from "../../../src/crypto/store/memory-crypto-store";
import { MockStorageApi } from "../../MockStorageApi";
import * as testUtils from "../../test-utils/test-utils";
Expand Down Expand Up @@ -118,7 +117,7 @@ function saveCrossSigningKeys(k) {
Object.assign(keys, k);
}

function makeTestClient(sessionStore, cryptoStore) {
function makeTestClient(cryptoStore) {
const scheduler = [
"getQueueForEvent", "queueEvent", "removeEventFromQueue",
"setProcessFunction",
Expand All @@ -141,7 +140,6 @@ function makeTestClient(sessionStore, cryptoStore) {
scheduler: scheduler,
userId: "@alice:bar",
deviceId: "device",
sessionStore: sessionStore,
cryptoStore: cryptoStore,
cryptoCallbacks: { getCrossSigningKey, saveCrossSigningKeys },
});
Expand All @@ -161,7 +159,6 @@ describe("MegolmBackup", function() {
let mockOlmLib;
let mockCrypto;
let mockStorage;
let sessionStore;
let cryptoStore;
let megolmDecryption;
beforeEach(async function() {
Expand All @@ -174,7 +171,6 @@ describe("MegolmBackup", function() {
mockCrypto.backupInfo = CURVE25519_BACKUP_INFO;

mockStorage = new MockStorageApi();
sessionStore = new WebStorageSessionStore(mockStorage);
cryptoStore = new MemoryCryptoStore(mockStorage);

olmDevice = new OlmDevice(cryptoStore);
Expand Down Expand Up @@ -261,7 +257,7 @@ describe("MegolmBackup", function() {
const ibGroupSession = new Olm.InboundGroupSession();
ibGroupSession.create(groupSession.session_key());

const client = makeTestClient(sessionStore, cryptoStore);
const client = makeTestClient(cryptoStore);

megolmDecryption = new MegolmDecryption({
userId: '@user:id',
Expand Down Expand Up @@ -340,7 +336,7 @@ describe("MegolmBackup", function() {
const ibGroupSession = new Olm.InboundGroupSession();
ibGroupSession.create(groupSession.session_key());

const client = makeTestClient(sessionStore, cryptoStore);
const client = makeTestClient(cryptoStore);

megolmDecryption = new MegolmDecryption({
userId: '@user:id',
Expand Down Expand Up @@ -423,7 +419,7 @@ describe("MegolmBackup", function() {
const ibGroupSession = new Olm.InboundGroupSession();
ibGroupSession.create(groupSession.session_key());

const client = makeTestClient(sessionStore, cryptoStore);
const client = makeTestClient(cryptoStore);

megolmDecryption = new MegolmDecryption({
userId: '@user:id',
Expand Down Expand Up @@ -520,7 +516,6 @@ describe("MegolmBackup", function() {
scheduler: scheduler,
userId: "@alice:bar",
deviceId: "device",
sessionStore: sessionStore,
cryptoStore: cryptoStore,
});

Expand Down Expand Up @@ -606,7 +601,7 @@ describe("MegolmBackup", function() {
let client;

beforeEach(function() {
client = makeTestClient(sessionStore, cryptoStore);
client = makeTestClient(cryptoStore);

megolmDecryption = new MegolmDecryption({
userId: '@user:id',
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,8 @@ describe("Room", function() {
return Promise.resolve();
},
getSyncToken: () => "sync_token",
getPendingEvents: jest.fn().mockResolvedValue([]),
setPendingEvents: jest.fn().mockResolvedValue(undefined),
},
};
}
Expand Down
20 changes: 2 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ import {
import { IAbortablePromise, IdServerUnbindResult, IImageInfo, Preset, Visibility } from "./@types/partials";
import { EventMapper, eventMapperFor, MapperOpts } from "./event-mapper";
import { randomString } from "./randomstring";
import { WebStorageSessionStore } from "./store/session/webstorage";
import { BackupManager, IKeyBackup, IKeyBackupCheck, IPreparedKeyBackupVersion, TrustInfo } from "./crypto/backup";
import { DEFAULT_TREE_POWER_LEVELS_TEMPLATE, MSC3089TreeSpace } from "./models/MSC3089TreeSpace";
import { ISignatures } from "./@types/signed";
Expand All @@ -195,7 +194,6 @@ import { Thread, THREAD_RELATION_TYPE } from "./models/thread";
import { MBeaconInfoEventContent, M_BEACON_INFO } from "./@types/beacon";

export type Store = IStore;
export type SessionStore = WebStorageSessionStore;

export type Callback<T = any> = (err: Error | any | null, data?: T) => void;
export type ResetTimelineCallback = (roomId: string) => boolean;
Expand Down Expand Up @@ -315,14 +313,6 @@ export interface ICreateClientOpts {
*/
pickleKey?: string;

/**
* A store to be used for end-to-end crypto session data. Most data has been
* migrated out of here to `cryptoStore` instead. If not specified,
* end-to-end crypto will be disabled. The `createClient` helper
* _will not_ create this store at the moment.
*/
sessionStore?: SessionStore;

verificationMethods?: Array<VerificationMethod>;

/**
Expand Down Expand Up @@ -897,7 +887,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
public timelineSupport = false;
public urlPreviewCache: { [key: string]: Promise<IPreviewUrlResponse> } = {};
public identityServer: IIdentityServerProvider;
public sessionStore: SessionStore; // XXX: Intended private, used in code.
public http: MatrixHttpApi; // XXX: Intended private, used in code.
public crypto: Crypto; // XXX: Intended private, used in code.
public cryptoCallbacks: ICryptoCallbacks; // XXX: Intended private, used in code.
Expand Down Expand Up @@ -1029,7 +1018,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
this.timelineSupport = Boolean(opts.timelineSupport);

this.cryptoStore = opts.cryptoStore;
this.sessionStore = opts.sessionStore;
this.verificationMethods = opts.verificationMethods;
this.cryptoCallbacks = opts.cryptoCallbacks || {};

Expand Down Expand Up @@ -1654,10 +1642,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return;
}

if (!this.sessionStore) {
// this is temporary, the sessionstore is supposed to be going away
throw new Error(`Cannot enable encryption: no sessionStore provided`);
}
if (!this.cryptoStore) {
// the cryptostore is provided by sdk.createClient, so this shouldn't happen
throw new Error(`Cannot enable encryption: no cryptoStore provided`);
Expand Down Expand Up @@ -1686,8 +1670,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

const crypto = new Crypto(
this,
this.sessionStore,
userId, this.deviceId,
userId,
this.deviceId,
this.store,
this.cryptoStore,
this.roomList,
Expand Down
12 changes: 0 additions & 12 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import {
ISignedKey,
IUploadKeySignaturesResponse,
MatrixClient,
SessionStore,
} from "../client";
import type { IRoomEncryption, RoomList } from "./RoomList";
import { IKeyBackupInfo } from "./keybackup";
Expand Down Expand Up @@ -323,9 +322,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
*
* @param {MatrixClient} baseApis base matrix api interface
*
* @param {module:store/session/webstorage~WebStorageSessionStore} sessionStore
* Store to be used for end-to-end crypto session data
*
* @param {string} userId The user ID for the local user
*
* @param {string} deviceId The identifier for this device.
Expand All @@ -343,7 +339,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
*/
constructor(
public readonly baseApis: MatrixClient,
public readonly sessionStore: SessionStore,
public readonly userId: string,
private readonly deviceId: string,
private readonly clientStore: IStore,
Expand Down Expand Up @@ -1725,13 +1720,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
logger.info(`Finished device verification upgrade for ${userId}`);
}

public async setTrustedBackupPubKey(trustedPubKey: string): Promise<void> {
// This should be redundant post cross-signing is a thing, so just
// plonk it in localStorage for now.
this.sessionStore.setLocalTrustedBackupPubKey(trustedPubKey);
await this.backupManager.checkKeyBackup();
}

/**
*/
public enableLazyLoading(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/store/indexeddb-crypto-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class IndexedDBCryptoStore implements CryptoStore {
}
}).then(backend => {
this.backend = backend;
return backend as CryptoStore;
return backend;
});

return this.backendPromise;
Expand Down
1 change: 0 additions & 1 deletion src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export * from "./interactive-auth";
export * from "./service-types";
export * from "./store/memory";
export * from "./store/indexeddb";
export * from "./store/session/webstorage";
export * from "./crypto/store/memory-crypto-store";
export * from "./crypto/store/indexeddb-crypto-store";
export * from "./content-repo";
Expand Down
40 changes: 11 additions & 29 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,16 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>

if (this.opts.pendingEventOrdering === PendingEventOrdering.Detached) {
this.pendingEventList = [];
const serializedPendingEventList = client.sessionStore.store.getItem(pendingEventsKey(this.roomId));
if (serializedPendingEventList) {
JSON.parse(serializedPendingEventList)
.forEach(async (serializedEvent: Partial<IEvent>) => {
const event = new MatrixEvent(serializedEvent);
if (event.getType() === EventType.RoomMessageEncrypted) {
await event.attemptDecryption(this.client.crypto);
}
event.setStatus(EventStatus.NOT_SENT);
this.addPendingEvent(event, event.getTxnId());
});
}
this.client.store.getPendingEvents(this.roomId).then(events => {
events.forEach(async (serializedEvent: Partial<IEvent>) => {
const event = new MatrixEvent(serializedEvent);
if (event.getType() === EventType.RoomMessageEncrypted) {
await event.attemptDecryption(this.client.crypto);
}
event.setStatus(EventStatus.NOT_SENT);
this.addPendingEvent(event, event.getTxnId());
});
});
}

// awaited by getEncryptionTargetMembers while room members are loading
Expand Down Expand Up @@ -2075,15 +2073,7 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>
return isEventEncrypted || !isRoomEncrypted;
});

const { store } = this.client.sessionStore;
if (this.pendingEventList.length > 0) {
store.setItem(
pendingEventsKey(this.roomId),
JSON.stringify(pendingEvents),
);
} else {
store.removeItem(pendingEventsKey(this.roomId));
}
this.client.store.setPendingEvents(this.roomId, pendingEvents);
}
}

Expand Down Expand Up @@ -3112,14 +3102,6 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>
}
}

/**
* @param {string} roomId ID of the current room
* @returns {string} Storage key to retrieve pending events
*/
function pendingEventsKey(roomId: string): string {
return `mx_pending_events_${roomId}`;
}

// a map from current event status to a list of allowed next statuses
const ALLOWED_TRANSITIONS: Record<EventStatus, EventStatus[]> = {
[EventStatus.ENCRYPTING]: [
Expand Down
6 changes: 5 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
import { EventType } from "../@types/event";
import { Room } from "../models/room";
import { User } from "../models/user";
import { MatrixEvent } from "../models/event";
import { IEvent, MatrixEvent } from "../models/event";
import { Filter } from "../filter";
import { RoomSummary } from "../models/room-summary";
import { IMinimalEvent, IRooms, ISyncResponse } from "../sync-accumulator";
Expand Down Expand Up @@ -218,4 +218,8 @@ export interface IStore {
getClientOptions(): Promise<IStartClientOpts>;

storeClientOptions(options: IStartClientOpts): Promise<void>;

getPendingEvents(roomId: string): Promise<Partial<IEvent>[]>;

setPendingEvents(roomId: string, events: Partial<IEvent>[]): Promise<void>;
}
Loading