-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (116 loc) · 5.08 KB
/
index.js
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 CtrlpanelCore from '@ctrlpanel/core'
import findAccountsForHostname from '@ctrlpanel/find-accounts-for-hostname'
let core = new CtrlpanelCore()
let state = null
function swiftState (state) {
let result = {}
if (state.kind != null) result.kind = state.kind
// Locked
if (state.handle != null) result.handle = state.handle
if (state.saveDevice != null) result.saveDevice = state.saveDevice
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) {
const parsedEntries = core.getParsedEntries(state)
// Swift handles UUID keys very poorly :(
result.parsedEntries = {
accounts: Object.keys(parsedEntries.accounts).reduce((mem, key) => [...mem, key, parsedEntries.accounts[key]], []),
inbox: Object.keys(parsedEntries.inbox).reduce((mem, key) => [...mem, key, parsedEntries.inbox[key]], [])
}
}
// 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
core = new CtrlpanelCore(apiHost, deseatmeApiHost)
},
init (syncToken) {
if (syncToken == null) syncToken = undefined
return swiftState(state = core.init(syncToken))
},
lock () {
return swiftState(state = core.lock(state))
},
async signup (handle, secretKey, masterPassword, saveDevice) {
return swiftState(state = await core.signup(state, { handle, secretKey, masterPassword }, saveDevice))
},
async login (handle, secretKey, masterPassword, saveDevice) {
return swiftState(state = await core.login(state, { handle, secretKey, masterPassword }, saveDevice))
},
async unlock (masterPassword) {
return swiftState(state = await core.unlock(state, { masterPassword }))
},
async connect () {
return swiftState(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}`)
return swiftState(state = await core.sync(state))
},
async setPaymentInformation (paymentInformation) {
if (state.kind === 'unlocked') await window['Ctrlpanel'].connect()
if (state.kind !== 'connected') throw new Error(`Invalid state: ${state.kind}`)
return swiftState(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 swiftState(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 swiftState(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 swiftState(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 swiftState(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 swiftState(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 swiftState(state = await core.importFromDeseatme(state, exportToken))
},
async clearStoredData () {
return swiftState(state = await core.clearStoredData(state))
},
async deleteUser () {
return swiftState(state = await core.deleteUser(state))
}
}