From c89ec94b17eb0ea568faf9349fc74a671a710650 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 27 Sep 2022 10:13:12 -0500 Subject: [PATCH] Remove old auth code --- package.json | 3 +- src/platform/auth-flow.native.ts | 53 -------------- src/platform/auth-flow.ts | 19 ----- src/state/lib/auth.ts | 118 ------------------------------- 4 files changed, 1 insertion(+), 192 deletions(-) delete mode 100644 src/platform/auth-flow.native.ts delete mode 100644 src/platform/auth-flow.ts delete mode 100644 src/state/lib/auth.ts diff --git a/package.json b/package.json index 4ec918d8..a3fc7dc0 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,7 @@ "react-native-screens": "^3.13.1", "react-native-svg": "^12.4.0", "react-native-url-polyfill": "^1.3.0", - "react-native-web": "^0.17.7", - "ucans": "0.9.1" + "react-native-web": "^0.17.7" }, "devDependencies": { "@babel/core": "^7.12.9", diff --git a/src/platform/auth-flow.native.ts b/src/platform/auth-flow.native.ts deleted file mode 100644 index 3c9bd09e..00000000 --- a/src/platform/auth-flow.native.ts +++ /dev/null @@ -1,53 +0,0 @@ -import {Linking} from 'react-native' -import * as auth from '@adxp/auth' -import * as ucan from 'ucans' -import {InAppBrowser} from 'react-native-inappbrowser-reborn' -import {isWeb} from '../platform/detection' -import {extractHashFragment, makeAppUrl} from '../platform/urls' -import {ReactNativeStore, parseUrlForUcan} from '../state/lib/auth' -import * as env from '../env' - -export async function requestAppUcan( - authStore: ReactNativeStore, - scope: ucan.Capability, -) { - const did = await authStore.getDid() - const returnUrl = makeAppUrl() - const fragment = auth.requestAppUcanHashFragment(did, scope, returnUrl) - const url = `${env.AUTH_LOBBY}#${fragment}` - - if (isWeb) { - // @ts-ignore window is defined -prf - window.location.href = url - return false - } - - if (await InAppBrowser.isAvailable()) { - // use in-app browser - const res = await InAppBrowser.openAuth(url, returnUrl, { - // iOS Properties - ephemeralWebSession: false, - // Android Properties - showTitle: false, - enableUrlBarHiding: true, - enableDefaultShare: false, - }) - if (res.type === 'success' && res.url) { - const fragment = extractHashFragment(res.url) - if (fragment) { - const ucan = await parseUrlForUcan(fragment) - if (ucan) { - await authStore.addUcan(ucan) - return true - } - } - } else { - console.log('Not completed', res) - return false - } - } else { - // use system browser - Linking.openURL(url) - } - return true -} diff --git a/src/platform/auth-flow.ts b/src/platform/auth-flow.ts deleted file mode 100644 index fbc85a37..00000000 --- a/src/platform/auth-flow.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as auth from '@adxp/auth' -import * as ucan from 'ucans' -import {makeAppUrl} from '../platform/urls' -import {ReactNativeStore} from '../state/lib/auth' -import * as env from '../env' - -export async function requestAppUcan( - authStore: ReactNativeStore, - scope: ucan.Capability, -) { - const did = await authStore.getDid() - const returnUrl = makeAppUrl() - const fragment = auth.requestAppUcanHashFragment(did, scope, returnUrl) - const url = `${env.AUTH_LOBBY}#${fragment}` - - // @ts-ignore window is defined -prf - window.location.href = url - return false -} diff --git a/src/state/lib/auth.ts b/src/state/lib/auth.ts deleted file mode 100644 index d51ba449..00000000 --- a/src/state/lib/auth.ts +++ /dev/null @@ -1,118 +0,0 @@ -export {} // TODO -/*import * as auth from '@adxp/auth' -import * as ucan from 'ucans' -import { - getInitialURL, - extractHashFragment, - clearHash, -} from '../../platform/urls' -import * as authFlow from '../../platform/auth-flow' -import * as storage from './storage' - -const SCOPE = auth.writeCap( - 'did:key:z6MkfRiFMLzCxxnw6VMrHK8pPFt4QAHS3jX3XM87y9rta6kP', - 'did:example:microblog', -) - -export async function isAuthed(authStore: ReactNativeStore) { - return await authStore.hasUcan(SCOPE) -} - -export async function logout(authStore: ReactNativeStore) { - await authStore.reset() -} - -export async function parseUrlForUcan(fragment: string) { - try { - return await auth.parseLobbyResponseHashFragment(fragment) - } catch (err) { - return undefined - } -} - -export async function initialLoadUcanCheck(authStore: ReactNativeStore) { - let wasAuthed = false - const fragment = extractHashFragment(await getInitialURL()) - if (fragment) { - const ucan = await parseUrlForUcan(fragment) - if (ucan) { - await authStore.addUcan(ucan) - wasAuthed = true - clearHash() - } - } - return wasAuthed -} - -export async function requestAppUcan(authStore: ReactNativeStore) { - return authFlow.requestAppUcan(authStore, SCOPE) -} - -export class ReactNativeStore extends auth.AuthStore { - private keypair: ucan.EdKeypair - private ucanStore: ucan.Store - - constructor(keypair: ucan.EdKeypair, ucanStore: ucan.Store) { - super() - this.keypair = keypair - this.ucanStore = ucanStore - } - - static async load(): Promise { - const keypair = await ReactNativeStore.loadOrCreateKeypair() - - const storedUcans = await ReactNativeStore.getStoredUcanStrs() - const ucanStore = await ucan.Store.fromTokens(storedUcans) - - return new ReactNativeStore(keypair, ucanStore) - } - - static async loadOrCreateKeypair(): Promise { - const storedKey = await storage.loadString('adxKey') - if (storedKey) { - return ucan.EdKeypair.fromSecretKey(storedKey) - } else { - // @TODO: again just stand in since no actual root keys - const keypair = await ucan.EdKeypair.create({exportable: true}) - storage.saveString('adxKey', await keypair.export()) - return keypair - } - } - - static async getStoredUcanStrs(): Promise { - const storedStr = await storage.loadString('adxUcans') - if (!storedStr) { - return [] - } - return storedStr.split(',') - } - - static setStoredUcanStrs(ucans: string[]): void { - storage.saveString('adxUcans', ucans.join(',')) - } - - protected async getKeypair(): Promise { - return this.keypair - } - - async addUcan(token: ucan.Chained): Promise { - this.ucanStore.add(token) - const storedUcans = await ReactNativeStore.getStoredUcanStrs() - ReactNativeStore.setStoredUcanStrs([...storedUcans, token.encoded()]) - } - - async getUcanStore(): Promise { - return this.ucanStore - } - - async clear(): Promise { - storage.clear() - } - - async reset(): Promise { - this.clear() - this.keypair = await ReactNativeStore.loadOrCreateKeypair() - this.ucanStore = await ucan.Store.fromTokens([]) - } -} -*/