forked from llun/le-store-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypairs.js
41 lines (35 loc) · 938 Bytes
/
keypairs.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
const Promise = require('bluebird')
class Keypairs {
constructor(store) {
this.store = store
}
checkAsync(keypath, format) {
if (!keypath) return null
const { s3, options } = this.store
const Bucket = options.S3.bucketName
return s3.getObjectAsync({
Bucket,
Key: keypath
}).then(body => {
const content = body.Body.toString()
return format === 'jwk'
? { privateKeyJwk: JSON.parse(content) }
: { privateKeyPem: content }
}).catch(error => {
return null
})
}
setAsync(keypath, keypair, format) {
const key = format === 'jwk'
? JSON.stringify(keypair.privateKeyJwk, null, ' ')
: keypair.privateKeyPem
const { s3, options } = this.store
const { bucketName } = options.S3
return s3.putObjectAsync({
Bucket: bucketName,
Key: keypath,
Body: key
}).then(() => keypair)
}
}
module.exports = Keypairs