Skip to content

Commit

Permalink
[DX-3013] feat: passport indexed db storage for unity sdk (#1978)
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 authored Jul 21, 2024
1 parent e2bf08e commit f4f6eee
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"scripts": {
"build": "yarn workspace @imtbl/sdk updateDependencies && NODE_OPTIONS=--max-old-space-size=14366 nx run-many --target=build --projects=@imtbl/sdk,@imtbl/checkout-widgets && yarn syncpack:format && yarn wsrun -p @imtbl/sdk -a -m copyBrowserBundles",
"build:onlysdk": "NODE_OPTIONS=--max-old-space-size=14366 nx run-many --target=build --projects=@imtbl/sdk && yarn syncpack:format",
"dev": "./dev.sh",
"docs:build": "typedoc",
"docs:serve": "http-server ./docs --cors -p 8080 -c-1",
"lint": "wsrun --exclude-missing -e lint --no-error-on-unmatched-pattern",
Expand All @@ -64,8 +65,7 @@
"test:checkout:sdk:coverage": "wsrun -p @imtbl/checkout-sdk --exclude-missing -e test:coverage",
"test:examples": "cd examples/ts-immutable-sample && yarn test && yarn test:e2e",
"test:vpn": "RUN_VPN_TESTS=1 wsrun --exclude-missing -e test",
"typecheck": "wsrun --exclude-missing typecheck",
"dev": "./dev.sh"
"typecheck": "wsrun --exclude-missing typecheck"
},
"workspaces": {
"packages": [
Expand Down
1 change: 1 addition & 0 deletions packages/passport/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ethers": "^5.7.2",
"events": "^3.3.0",
"jwt-decode": "^3.1.2",
"localforage": "^1.10.0",
"magic-sdk": "^21.2.0",
"oidc-client-ts": "2.4.0",
"uuid": "^8.3.2"
Expand Down
11 changes: 10 additions & 1 deletion packages/passport/sdk/src/authManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import axios from 'axios';
import * as crypto from 'crypto';
import jwt_decode from 'jwt-decode';
import { getDetail, Detail } from '@imtbl/metrics';
import localForage from 'localforage';
import DeviceCredentialsManager from './storage/device_credentials_manager';
import logger from './utils/logger';
import { isTokenExpired } from './utils/token';
Expand All @@ -31,6 +32,7 @@ import {
} from './types';
import { PassportConfiguration } from './config';
import Overlay from './overlay';
import { LocalForageAsyncStorage } from './storage/LocalForageAsyncStorage';

const formUrlEncodedHeader = {
headers: {
Expand All @@ -44,7 +46,14 @@ const authorizeEndpoint = '/authorize';
const getAuthConfiguration = (config: PassportConfiguration): UserManagerSettings => {
const { authenticationDomain, oidcConfiguration } = config;

const store = typeof window !== 'undefined' ? window.localStorage : new InMemoryWebStorage();
let store;
if (config.crossSdkBridgeEnabled) {
store = new LocalForageAsyncStorage('ImmutableSDKPassport', localForage.INDEXEDDB);
} else if (typeof window !== 'undefined') {
store = window.localStorage;
} else {
store = new InMemoryWebStorage();
}
const userStore = new WebStorageStateStore({ store });

const endSessionEndpoint = new URL(logoutEndpoint, authenticationDomain.replace(/^(?:https?:\/\/)?(.*)/, 'https://$1'));
Expand Down
34 changes: 34 additions & 0 deletions packages/passport/sdk/src/storage/LocalForageAsyncStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { AsyncStorage } from 'oidc-client-ts';
import localForage from 'localforage';

export class LocalForageAsyncStorage implements AsyncStorage {
private storage: LocalForage;

constructor(name: string, driver: string | string[]) {
this.storage = localForage.createInstance({ name, driver });
}

get length(): Promise<number> {
return this.storage.length();
}

clear(): Promise<void> {
return this.storage.clear();
}

getItem(key: string): Promise<string | null> {
return this.storage.getItem<string>(key);
}

key(index: number): Promise<string | null> {
return this.storage.key(index);
}

async removeItem(key: string): Promise<void> {
await this.storage.removeItem(key);
}

async setItem(key: string, value: string): Promise<void> {
await this.storage.setItem(key, value);
}
}
1 change: 1 addition & 0 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"i18next": "^23.7.6",
"i18next-browser-languagedetector": "^7.2.0",
"jwt-decode": "^3.1.2",
"localforage": "^1.10.0",
"lru-memorise": "0.3.0",
"magic-sdk": "^21.2.0",
"merkletreejs": "^0.3.11",
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2250,7 +2250,6 @@ __metadata:
"@biom3/design-tokens": ~0.4.0
buffer: ^6.0.3
csstype: ^3.1.2
localforage: ^1.10.0
lodash.debounce: ^4.0.8
lodash.get: ^4.4.2
lodash.isequal: ^4.5.0
Expand Down Expand Up @@ -3833,6 +3832,7 @@ __metadata:
jest-environment-jsdom: ^29.4.3
jwt-decode: ^3.1.2
jwt-encode: ^1.0.1
localforage: ^1.10.0
magic-sdk: ^21.2.0
msw: ^1.2.2
oidc-client-ts: 2.4.0
Expand Down Expand Up @@ -3923,6 +3923,7 @@ __metadata:
i18next: ^23.7.6
i18next-browser-languagedetector: ^7.2.0
jwt-decode: ^3.1.2
localforage: ^1.10.0
lru-memorise: 0.3.0
magic-sdk: ^21.2.0
merkletreejs: ^0.3.11
Expand Down

0 comments on commit f4f6eee

Please sign in to comment.