This repository has been archived by the owner on Feb 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
205 lines (163 loc) · 3.94 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
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
/* global process */
/**
* Module dependencies
*/
var fs = require('fs')
var crypto = require('crypto')
var mkdirp = require('mkdirp')
var path = require('path')
var pemjwk = require('pem-jwk')
var childProcess = require('child_process')
/**
* Constructor
*/
function AnvilConnectKeys (directory) {
// base directory for keys to be read from and written to
this.directory = path.join(directory || process.cwd(), 'keys')
// signature key pair file paths
this.sig = {
pub: path.join(this.directory, 'sig.rsa.pub.pem'),
prv: path.join(this.directory, 'sig.rsa.prv.pem')
}
// encryption key pair file paths
this.enc = {
pub: path.join(this.directory, 'enc.rsa.pub.pem'),
prv: path.join(this.directory, 'enc.rsa.prv.pem')
}
// setup token
this.setup = path.join(this.directory, 'setup.token')
}
/**
* Generate key pairs
*/
function generateKeyPairs () {
AnvilConnectKeys.generateKeyPair(this.sig.pub, this.sig.prv)
AnvilConnectKeys.generateKeyPair(this.enc.pub, this.enc.prv)
}
AnvilConnectKeys.prototype.generateKeyPairs = generateKeyPairs
/**
* Generate single key pair
*/
function generateKeyPair (pub, prv) {
try {
mkdirp.sync(path.dirname(pub))
mkdirp.sync(path.dirname(prv))
childProcess.execFileSync('openssl', [
'genrsa',
'-out',
prv,
'4096'
], {
stdio: 'ignore'
})
childProcess.execFileSync('openssl', [
'rsa',
'-pubout',
'-in',
prv,
'-out',
pub
], {
stdio: 'ignore'
})
} catch (e) {
throw new Error(
'Failed to generate keys using OpenSSL. Please ensure you have OpenSSL ' +
'installed and configured on your system.'
)
}
}
AnvilConnectKeys.generateKeyPair = generateKeyPair
/**
* Load key pairs
*/
function loadKeyPairs () {
var sig = AnvilConnectKeys.loadKeyPair(this.sig.pub, this.sig.prv, 'sig')
var enc = AnvilConnectKeys.loadKeyPair(this.enc.pub, this.enc.prv, 'enc')
var jwkKeys = []
jwkKeys.push(sig.jwk.pub, enc.jwk.pub)
return {
sig: sig.pem,
enc: enc.pem,
jwks: {
keys: jwkKeys
}
}
}
AnvilConnectKeys.prototype.loadKeyPairs = loadKeyPairs
/**
* Load single key pair
*/
function loadKeyPair (pub, prv, use) {
var pubPEM, prvPEM, pubJWK
try {
pubPEM = fs.readFileSync(pub).toString('ascii')
} catch (e) {
throw new Error('Unable to read the public key from ' + pub)
}
try {
prvPEM = fs.readFileSync(prv).toString('ascii')
} catch (e) {
throw new Error('Unable to read the private key from ' + pub)
}
try {
pubJWK = pemjwk.pem2jwk(pubPEM)
} catch (e) {
throw new Error('Unable to convert the public key ' + pub + ' to a JWK')
}
return {
pem: {
pub: pubPEM,
prv: prvPEM
},
jwk: {
pub: {
kty: pubJWK.kty,
use: use,
alg: 'RS256',
n: pubJWK.n,
e: pubJWK.e
}
}
}
}
AnvilConnectKeys.loadKeyPair = loadKeyPair
/**
* Generate setup token
*/
function generateSetupToken (tokenPath) {
mkdirp.sync(path.dirname(tokenPath))
var token = crypto.randomBytes(256).toString('hex')
try {
fs.writeFileSync(tokenPath, token, 'utf8')
} catch (e) {
throw new Error('Unable to save setup token to ' + tokenPath)
}
return token
}
AnvilConnectKeys.generateSetupToken = generateSetupToken
/**
* Load setup token
*/
function loadSetupToken (tokenPath) {
return fs.readFileSync(tokenPath, 'utf8').toString().trim()
}
AnvilConnectKeys.loadSetupToken = loadSetupToken
/**
* Generate setup token from scoped path
*/
function generateSetupTokenLocal () {
return AnvilConnectKeys.generateSetupToken(this.setup)
}
AnvilConnectKeys.prototype.generateSetupToken = generateSetupTokenLocal
/**
* Load setup token from scoped path
*/
function loadSetupTokenLocal () {
return AnvilConnectKeys.loadSetupToken(this.setup)
}
AnvilConnectKeys.prototype.loadSetupToken = loadSetupTokenLocal
/**
* Export
*/
module.exports = AnvilConnectKeys