about summary refs log tree commit diff
path: root/src/state/auth.ts
blob: b49a11d90796cbbc041320bf0fc7f8db8e866008 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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 {makeAppUrl} from '../platform/urls'
import * as storage from './storage'
import * as env from '../env'

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() {
  if (isWeb) {
    // @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
    }
  } else {
    // TODO
  }
}

export async function requestAppUcan(authStore: ReactNativeStore) {
  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()) {
    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) {
      Linking.openURL(res.url)
    } else {
      console.error('Bad response', res)
      return false
    }
  } else {
    Linking.openURL(url)
  }
  return true
}

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<ReactNativeStore> {
    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<ucan.EdKeypair> {
    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<string[]> {
    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<ucan.EdKeypair> {
    return this.keypair
  }

  async addUcan(token: ucan.Chained): Promise<void> {
    this.ucanStore.add(token)
    const storedUcans = await ReactNativeStore.getStoredUcanStrs()
    ReactNativeStore.setStoredUcanStrs([...storedUcans, token.encoded()])
  }

  async getUcanStore(): Promise<ucan.Store> {
    return this.ucanStore
  }

  async clear(): Promise<void> {
    storage.clear()
  }

  async reset(): Promise<void> {
    this.clear()
    this.keypair = await ReactNativeStore.loadOrCreateKeypair()
    this.ucanStore = await ucan.Store.fromTokens([])
  }
}