forked from llun/le-store-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.js
156 lines (137 loc) · 4.87 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
const Promise = require('bluebird')
const crypto = require('crypto')
const path = require('path')
const os = require('os')
class Accounts {
constructor(store, keypairs) {
this.store = store
this.keypairs = keypairs
}
getAccountKeyPath({ accountsDir, accountId, accountKeyPath, email }) {
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 }) {
const { s3, options } = this.store
const Bucket = options.S3.bucketName
return s3.listObjectsAsync({ Bucket, Prefix: accountsDir })
.then(objects => objects.Contents
.filter(item => item.Key.endsWith('regr.json'))
.map(item => item.Key))
.then(regrs => {
return Promise.all(regrs.map(item => s3.getObjectAsync({ Bucket, Key: item })))
.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) {
return crypto.createHash('md5').update(keypair.publicKeyPem).digest('hex')
}
checkAsync({ accountId, email, accountsDir }) {
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.getObjectAsync({ Bucket, Key: path.join(accountDir, `${key}.json`) })
.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).`)
error.code = 'E_ACCOUNT_CORRUPT'
error.data = files
return Promise.reject(error)
}
files.accountId = accountId
files.id = accountId
files.keypair = { privateKeyJwk: files.private_key }
return files
})
}
setAsync({ accountsDir, email }, { keypair, receipt }) {
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.putObjectAsync({
Bucket,
Key: path.join(accountDir, 'meta.json'),
Body: JSON.stringify(accountMeta)
}),
s3.putObjectAsync({
Bucket,
Key: path.join(accountDir, 'private_key.json'),
Body: JSON.stringify(keypair.privateKeyJwk)
}),
s3.putObjectAsync({
Bucket,
Key: path.join(accountDir, 'regr.json'),
Body: JSON.stringify({ body: receipt })
})
]).then(() => ({
id: accountId,
accountId,
email,
keypair,
receipt
}))
}
checkKeypairAsync({ accountId, email, accountKeyPath, accountsDir }) {
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 }, keypair) {
if (!accountId) {
accountId = this.getAccountIdByPublicKey(keypair)
}
return this.getAccountKeyPath({ accountsDir, accountId, email })
.then(keypath => this.keypairs.setAsync(keypath, keypair, 'jwk'))
}
}
module.exports = Accounts