This repository was archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
157 lines (127 loc) · 5.4 KB
/
worker.js
File metadata and controls
157 lines (127 loc) · 5.4 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import CtrlpanelCore from '@ctrlpanel/core'
import findAccountsForHostname from '@ctrlpanel/find-accounts-for-hostname'
import FannyPackReactNative from '@fanny-pack/react-native'
/** @type {CtrlpanelCore} */
let core = null
let state = null
function jsonState (state) {
let result = {}
if (state.kind != null) result.kind = state.kind
// Locked
if (state.handle != null) result.handle = state.handle
if (state.secretKey != null) result.secretKey = state.secretKey
if (state.handle != null && state.secretKey != null) result.syncToken = core.getSyncToken(state)
// Unlocked
if (state.decryptedEntries != null) {
result.parsedEntries = core.getParsedEntries(state)
}
// Connected
if (state.hasPaymentInformation != null) result.hasPaymentInformation = state.hasPaymentInformation
if (state.subscriptionStatus != null) result.subscriptionStatus = state.subscriptionStatus
if (state.trialDaysLeft != null) result.trialDaysLeft = state.trialDaysLeft
return result
}
window['Ctrlpanel'] = {
randomAccountPassword () {
return CtrlpanelCore.randomAccountPassword()
},
randomHandle () {
return CtrlpanelCore.randomHandle()
},
randomMasterPassword () {
return CtrlpanelCore.randomMasterPassword()
},
randomSecretKey () {
return CtrlpanelCore.randomSecretKey()
},
boot (apiHost, deseatmeApiHost) {
if (apiHost == null) apiHost = undefined
if (deseatmeApiHost == null) deseatmeApiHost = undefined
const storage = new FannyPackReactNative('ctrlpanel')
core = new CtrlpanelCore({ apiHost, deseatmeApiHost, storage })
},
async init (syncToken) {
if (syncToken == null) syncToken = undefined
return jsonState(state = await core.init(syncToken))
},
lock () {
return jsonState(state = core.lock(state))
},
async signup (handle, secretKey, masterPassword) {
return jsonState(state = await core.signup(state, { handle, secretKey, masterPassword }))
},
async login (handle, secretKey, masterPassword) {
const connectedState = await core.login(state, { handle, secretKey, masterPassword })
return jsonState(state = await core.sync(connectedState))
},
async unlock (masterPassword) {
let nextState = await core.unlock(state, { masterPassword })
/*
A connected state means that we did not make an offline login, which means that no data have
been synced to this device yet. In order not to flash an empty state, we run a sync together
with this unlock action.
*/
if (nextState.kind === 'connected') {
nextState = await core.sync(nextState)
}
return jsonState(state = nextState)
},
async connect () {
return jsonState(state = await core.connect(state))
},
async sync () {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error(`Invalid state: ${state.kind}`)
const nextState = await core.sync(state)
if (state.kind !== 'unlocked' && state.kind !== 'connected') {
throw Object.assign(new Error('Sync aborted'), { code: 'LOCKED_DURING_SYNC' })
}
return jsonState(state = nextState)
},
async setPaymentInformation (paymentInformation) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error(`Invalid state: ${state.kind}`)
return jsonState(state = await core.setPaymentInformation(state, paymentInformation))
},
accountsForHostname (hostname) {
const { accounts } = core.getParsedEntries(state)
const accountList = Object.keys(accounts).map(id => Object.assign({ id }, accounts[id]))
return findAccountsForHostname(hostname, accountList)
},
async createAccount (id, account) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error('Vault is locked')
return jsonState(state = await core.createAccount(state, id.toLowerCase(), account))
},
async deleteAccount (id) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error('Vault is locked')
return jsonState(state = await core.deleteAccount(state, id.toLowerCase()))
},
async updateAccount (id, account) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error('Vault is locked')
return jsonState(state = await core.updateAccount(state, id.toLowerCase(), account))
},
async createInboxEntry (id, inboxEntry) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error('Vault is locked')
return jsonState(state = await core.createInboxEntry(state, id.toLowerCase(), inboxEntry))
},
async deleteInboxEntry (id) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error('Vault is locked')
return jsonState(state = await core.deleteInboxEntry(state, id.toLowerCase()))
},
async importFromDeseatme (exportToken) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error('Vault is locked')
return jsonState(state = await core.importFromDeseatme(state, exportToken))
},
async clearStoredData () {
return jsonState(state = await core.clearStoredData(state))
},
async deleteUser () {
return jsonState(state = await core.deleteUser(state))
}
}