Add base auth & ucan request flow (web only)

This commit is contained in:
Paul Frazee 2022-06-14 14:29:47 -05:00
parent 09b78a4634
commit cef133031e
14 changed files with 1555 additions and 290 deletions

48
src/api/auth.ts Normal file
View file

@ -0,0 +1,48 @@
import * as auth from '@adxp/auth'
import {isWeb} from '../platform/detection'
import * as env from '../env'
const SCOPE = auth.writeCap(
'did:key:z6MkfRiFMLzCxxnw6VMrHK8pPFt4QAHS3jX3XM87y9rta6kP',
'did:example:microblog',
)
export async function isAuthed(authStore: auth.BrowserStore) {
return await authStore.hasUcan(SCOPE)
}
export async function logout(authStore: auth.BrowserStore) {
await authStore.reset()
}
export async function parseUrlForUcan() {
// @ts-ignore window is defined -prf
const fragment = window.location.hash
if (fragment.length < 1) {
return undefined
}
try {
const ucan = await auth.parseLobbyResponseHashFragment(fragment)
// @ts-ignore window is defined -prf
window.location.hash = ''
return ucan
} catch (err) {
return undefined
}
}
export async function requestAppUcan(authStore: auth.BrowserStore) {
const did = await authStore.getDid()
if (isWeb) {
// @ts-ignore window is defined -prf
const redirectTo = window.location.origin
const fragment = auth.requestAppUcanHashFragment(did, SCOPE, redirectTo)
// @ts-ignore window is defined -prf
window.location.href = `${env.AUTH_LOBBY}#${fragment}`
return false
} else {
// TODO
console.log('TODO')
}
return false
}

View file

@ -1,5 +1,26 @@
import {MicroblogDelegator, MicroblogReader, auth} from '@adx/common'
import * as ucan from 'ucans'
// import {MicroblogDelegator, MicroblogReader, auth} from '@adx/common'
// import * as ucan from 'ucans'
class MicroblogReader {
constructor(public url: string, public did: any) {}
}
class MicroblogDelegator {
constructor(
public url: string,
public did: any,
public keypair: any,
public ucanStore: any,
) {}
}
const auth = {
async claimFull(_one: any, _two: any) {
return {
encoded() {
return 'todo'
},
}
},
}
export class API {
userCfg?: UserConfig
@ -51,9 +72,9 @@ export interface SerializedUserConfig {
export class UserConfig {
serverUrl?: string
did?: string
keypair?: ucan.EdKeypair
keypair?: any //ucan.EdKeypair
rootAuthToken?: string
ucanStore?: ucan.Store
ucanStore?: any //ucan.Store
get hasWriteCaps() {
return Boolean(this.did && this.keypair && this.ucanStore)
@ -62,10 +83,10 @@ export class UserConfig {
static async createTest(serverUrl: string) {
const cfg = new UserConfig()
cfg.serverUrl = serverUrl
cfg.keypair = await ucan.EdKeypair.create()
cfg.keypair = true //await ucan.EdKeypair.create()
cfg.did = cfg.keypair.did()
cfg.rootAuthToken = (await auth.claimFull(cfg.did, cfg.keypair)).encoded()
cfg.ucanStore = await ucan.Store.fromTokens([cfg.rootAuthToken])
cfg.ucanStore = true // await ucan.Store.fromTokens([cfg.rootAuthToken])
return cfg
}
@ -88,10 +109,10 @@ export class UserConfig {
async hydrate(state: SerializedUserConfig) {
this.serverUrl = state.serverUrl
if (state.secretKeyStr && state.rootAuthToken) {
this.keypair = ucan.EdKeypair.fromSecretKey(state.secretKeyStr)
this.keypair = true // ucan.EdKeypair.fromSecretKey(state.secretKeyStr)
this.did = this.keypair.did()
this.rootAuthToken = state.rootAuthToken
this.ucanStore = await ucan.Store.fromTokens([this.rootAuthToken])
this.ucanStore = true // await ucan.Store.fromTokens([this.rootAuthToken])
}
}
}