-
Notifications
You must be signed in to change notification settings - Fork 3
/
accounts.js
218 lines (190 loc) · 6.07 KB
/
accounts.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// @flow
const Promise = require('bluebird')
const crypto = require('crypto')
const path = require('path')
const os = require('os')
/*::
import type Store from './store'
import type KeyPairs, { KeyPair } from './keypairs'
type AccountKeyPathArgs = {
accountsDir: string,
accountId: ?string,
accountKeyPath?: string,
email: ?string
}
type AccountIdArgs = {
email: string,
accountsDir: string
}
type CheckAccountArgs = AccountIdArgs & {
accountId: string
}
type ErrorFile = {
error: any
}
type AccountFile = {
meta: Object | ErrorFile,
private_key: Object | ErrorFile,
regr: Object | ErrorFile,
accountId: string,
id: string,
keypair: KeyPair
}
type SetAccountValue = {
keypair: KeyPair,
receipt: any
}
type SetAccountResult = {
id: string,
accountId: string,
email: string,
keypair: KeyPair,
receipt: any
}
*/
class Accounts {
/*::
store:Store
keypairs:KeyPairs
*/
constructor(store/*:Store*/, keypairs/*:KeyPairs*/) {
this.store = store
this.keypairs = keypairs
}
getAccountKeyPath(
{ accountsDir, accountId, accountKeyPath, email }/*:AccountKeyPathArgs*/)/*:Promise<?string>*/ {
let head = Promise.resolve(accountId)
if (email && !accountId) head = this.getAccountIdByEmail({ email, accountsDir })
return head.then(accountId => {
if (!accountId) return null
return accountKeyPath || path.join(accountsDir, accountId, 'private_key.json')
})
}
getAccountIdByEmail(
{ email, accountsDir }/*:AccountIdArgs*/)/*:Promise<?string>*/ {
const { s3, options } = this.store
const Bucket = options.S3.bucketName
return s3.listObjects({ Bucket, Prefix: accountsDir }).promise()
.then(objects => objects.Contents
.filter(item => item.Key.endsWith('regr.json'))
.map(item => item.Key))
.then(regrs => {
return Promise.all(regrs.map(item => s3.getObject({ Bucket, Key: item }).promise()))
.then(contents => {
return contents.map((item, index) => {
const parts = regrs[index].split('/')
const json = JSON.parse(item.Body.toString('utf8'))
json.accountId = parts[parts.length - 2]
return json
})
})
})
.then(jsons => {
const contact = jsons
.find(json => json.body.contact
.find(contact => contact.toLowerCase().endsWith(email.toLowerCase())))
if (contact) return contact.accountId
return null
})
}
getAccountIdByPublicKey(keypair/*:KeyPair*/)/*:string*/ {
return crypto.createHash('md5').update(keypair.publicKeyPem).digest('hex')
}
checkAsync({ accountId, email, accountsDir }/*:CheckAccountArgs*/)/*:Promise<AccountFile>*/ {
if (!(accountId || email)) return Promise.reject(new Error('must provide accountId or email'))
let head = Promise.resolve(accountId)
if (email) {
head = this.getAccountIdByEmail({ email, accountsDir })
}
const files = {}
const { s3, options } = this.store
const Bucket = options.S3.bucketName
return head.then(accountId => {
if (!accountId) return false
const accountDir = path.join(accountsDir, accountId)
const keys = ['meta', 'private_key', 'regr']
return Promise.all(keys.map(key => {
return s3.getObject({ Bucket, Key: path.join(accountDir, `${key}.json`) }).promise()
.then(item => {
const body = item.Body.toString('utf8')
try {
files[key] = JSON.parse(body)
} catch (error) {
files[key] = { error }
}
return true
})
.catch(error => { files[key] = { error }})
}))
}).then(hasAccount => {
if (!hasAccount) return null
if (!Object.keys(files).every(key => !files[key].error) ||
!files.private_key || !files.private_key.n) {
const error = new Error(`Account ${accountId} was corrupt (had id, but was missing files).`)
// $FlowFixMe
error.code = 'E_ACCOUNT_CORRUPT'
// $FlowFixMe
error.data = files
return Promise.reject(error)
}
files.accountId = accountId
files.id = accountId
files.keypair = { privateKeyJwk: files.private_key }
return files
})
}
setAsync(
{ accountsDir, email }/*:AccountIdArgs*/,
{ keypair, receipt }/*:SetAccountValue*/)/*:Promise<SetAccountResult>*/ {
const accountId = this.getAccountIdByPublicKey(keypair)
const accountDir = path.join(accountsDir, accountId)
const accountMeta = {
creation_host: os.hostname(),
creation_dt: new Date().toISOString()
}
const { s3, options } = this.store
const { bucketName } = options.S3
const Bucket = bucketName
return Promise.all([
s3.putObject({
Bucket,
Key: path.join(accountDir, 'meta.json'),
Body: JSON.stringify(accountMeta)
}).promise(),
s3.putObject({
Bucket,
Key: path.join(accountDir, 'private_key.json'),
Body: JSON.stringify(keypair.privateKeyJwk)
}).promise(),
s3.putObject({
Bucket,
Key: path.join(accountDir, 'regr.json'),
Body: JSON.stringify({ body: receipt })
}).promise()
]).then(() => ({
id: accountId,
accountId,
email,
keypair,
receipt
}))
}
checkKeypairAsync(
{ accountId, email, accountKeyPath, accountsDir }/*:AccountKeyPathArgs*/)/*:Promise<?KeyPair>*/ {
if (!(accountKeyPath || accountsDir)) {
return Promise.reject(new Error('must provide one of options.accountKeyPath or options.accountsDir'))
}
return this.getAccountKeyPath({ accountId, email, accountKeyPath, accountsDir })
.then(keypath => this.keypairs.checkAsync(keypath, 'jwk'))
}
setKeypairAsync(
{ email, accountId, accountsDir }/*:CheckAccountArgs*/,
keypair/*:KeyPair*/)/*:Promise<KeyPair>*/ {
if (!accountId) {
accountId = this.getAccountIdByPublicKey(keypair)
}
return this.getAccountKeyPath({ accountsDir, accountId, email })
.then(keypath => this.keypairs.setAsync(keypath, keypair, 'jwk'))
}
}
module.exports = Accounts