Skip to content
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

fix: graceful upgrade from localstorage to idb without breaking sessions #606

Merged
merged 1 commit into from Aug 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generated/changelog.html
Expand Up @@ -19,6 +19,7 @@ <h2>Version 0.12.3</h2>
<ul>
<li>Also offers a generic Indexed Db keyval store, IdbKeyVal</li>
</ul>
<li>AuthClient migrates gracefully from localstorage to IDB when upgrading</li>
</ul>
<h2>Version 0.12.2</h2>
<ul>
Expand Down
63 changes: 62 additions & 1 deletion packages/auth-client/src/index.test.ts
Expand Up @@ -5,7 +5,12 @@ import { IDL } from '@dfinity/candid';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { Principal } from '@dfinity/principal';
import { AuthClient, ERROR_USER_INTERRUPT, IdbStorage } from './index';
import { AuthClientStorage } from './storage';
import {
AuthClientStorage,
KEY_STORAGE_DELEGATION,
KEY_STORAGE_KEY,
LocalStorage,
} from './storage';

/**
* A class for mocking the IDP service.
Expand Down Expand Up @@ -578,3 +583,59 @@ describe('Auth Client login', () => {
expect(idpWindow.close).toBeCalled();
});
});

describe('Migration from localstorage', () => {
it('should proceed normally if no values are stored in localstorage', async () => {
const storage: AuthClientStorage = {
remove: jest.fn(),
get: jest.fn(),
set: jest.fn(),
};

await AuthClient.create({ storage });

expect(storage.set as jest.Mock).toBeCalledTimes(0);
});
it('should not attempt to migrate if a delegation is already stored', async () => {
const storage: AuthClientStorage = {
remove: jest.fn(),
get: jest.fn(async x => {
if (x === KEY_STORAGE_DELEGATION) return 'test';
if (x === KEY_STORAGE_KEY) return 'key';
return null;
}),
set: jest.fn(),
};

await AuthClient.create({ storage });

expect(storage.set as jest.Mock).toBeCalledTimes(0);
});
it('should migrate storage from localstorage', async () => {
const localStorage = new LocalStorage('ic');
const storage: AuthClientStorage = {
remove: jest.fn(),
get: jest.fn(),
set: jest.fn(),
};

await localStorage.set(KEY_STORAGE_DELEGATION, 'test');
await localStorage.set(KEY_STORAGE_KEY, 'key');

await AuthClient.create({ storage });

expect(storage.set as jest.Mock).toBeCalledTimes(2);
expect((storage.set as jest.Mock).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"delegation",
"test",
],
Array [
"identity",
"key",
],
]
`);
});
});
22 changes: 21 additions & 1 deletion packages/auth-client/src/index.ts
Expand Up @@ -21,6 +21,7 @@ import {
KEY_STORAGE_DELEGATION,
KEY_STORAGE_KEY,
KEY_VECTOR,
LocalStorage,
} from './storage';

export { IdbStorage, LocalStorage } from './storage';
Expand Down Expand Up @@ -193,7 +194,26 @@ export class AuthClient {
if (options.identity) {
key = options.identity;
} else {
const maybeIdentityStorage = await storage.get(KEY_STORAGE_KEY);
let maybeIdentityStorage = await storage.get(KEY_STORAGE_KEY);
if (!maybeIdentityStorage) {
// Attempt to migrate from localstorage
try {
const fallbackLocalStorage = new LocalStorage('ic');
const localChain = await fallbackLocalStorage.get(KEY_STORAGE_DELEGATION);
const localKey = await fallbackLocalStorage.get(KEY_STORAGE_KEY);
if (localChain && localKey) {
console.log('Discovered an identity stored in localstorage. Migrating to IndexedDB');
await storage.set(KEY_STORAGE_DELEGATION, localChain);
await storage.set(KEY_STORAGE_KEY, localKey);
maybeIdentityStorage = localChain;
// clean up
await fallbackLocalStorage.remove(KEY_STORAGE_DELEGATION);
await fallbackLocalStorage.remove(KEY_STORAGE_KEY);
}
} catch (error) {
console.error('error while attempting to recover localstorage: ' + error);
}
}
if (maybeIdentityStorage) {
try {
key = Ed25519KeyIdentity.fromJSON(maybeIdentityStorage);
Expand Down