-
Notifications
You must be signed in to change notification settings - Fork 3
/
keypairs.js
56 lines (48 loc) · 1.25 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @flow
const Promise = require('bluebird')
/*::
import type Store from './store'
export type KeyPairFormat = 'jwk' | 'pem'
export type KeyPair = {
privateKeyPem:any,
publicKeyPem:any,
privateKeyJwk:any
}
*/
class Keypairs {
/*::
store:Store
*/
constructor(store/*: Store */) {
this.store = store
}
checkAsync(keypath/*:string */, format /*:KeyPairFormat*/)/*:Promise<?KeyPair>*/ {
if (!keypath) return null
const { s3, options } = this.store
const Bucket = options.S3.bucketName
return s3.getObject({
Bucket,
Key: keypath
}).promise().then(body => {
const content = body.Body.toString()
return format === 'jwk'
? { privateKeyJwk: JSON.parse(content) }
: { privateKeyPem: content }
}).catch(error => {
return null
})
}
setAsync(keypath/*:string*/, keypair/*:KeyPair*/, format/*:KeyPairFormat*/)/*:Promise<KeyPair>*/ {
const key = format === 'jwk'
? JSON.stringify(keypair.privateKeyJwk, null, ' ')
: keypair.privateKeyPem
const { s3, options } = this.store
const { bucketName } = options.S3
return s3.putObject({
Bucket: bucketName,
Key: keypath,
Body: key
}).promise().then(() => keypair)
}
}
module.exports = Keypairs