-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeyChain.js
More file actions
217 lines (186 loc) · 5.46 KB
/
KeyChain.js
File metadata and controls
217 lines (186 loc) · 5.46 KB
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
/**
* Dependencies
*/
const supportedAlgorithms = require('./algorithms')
const InvalidDescriptorError = require('./errors/InvalidDescriptorError')
/**
* KeyChain
*/
class KeyChain {
/**
* constructor
*
* @param data {Object} Keychain data
* @param options {Object} Optional configuration
* @param options.crypto {Object} Optional crypto instance for cross-package compatibility
*/
constructor (data, options = {}) {
// use data as the descriptor if descriptor property is missing
if (!data.descriptor) {
data = { descriptor: data }
}
Object.assign(this, data)
// Store crypto instance if provided
this._crypto = options.crypto
}
/**
* generate
*/
static generate (descriptor) {
let keys = new KeyChain(descriptor)
return keys.rotate()
}
/**
* generateKey
*
* @param {Object} params
* @return {Promise}
*/
static generateKey (params, crypto) {
let normalizedAlgorithm = supportedAlgorithms.normalize('generateKey', params.alg)
if (normalizedAlgorithm instanceof Error) {
return Promise.reject(normalizedAlgorithm)
}
let algorithm = new normalizedAlgorithm({...params, crypto})
return algorithm.generateKey()
}
/**
* importKey
*
* @param {Object} params
* @param {Object} jwk
* @return {Promise}
*/
static importKey (jwk, crypto) {
let {alg} = jwk
let normalizedAlgorithm = supportedAlgorithms.normalize('importKey', alg)
if (normalizedAlgorithm instanceof Error) {
return Promise.reject(normalizedAlgorithm)
}
let algorithm = new normalizedAlgorithm({alg, crypto})
return algorithm.importKey(jwk)
}
/**
* restore
*
* @param data {Object} Keychain data
* @param options {Object} Optional configuration
* @param options.crypto {Object} Optional crypto instance for cross-package compatibility
*/
static restore (data, options) {
let keys = new KeyChain(data, options)
return keys.importKeys().then(() => keys)
}
/**
* importKeys
*/
importKeys ({props, object, container, descriptor} = {}) {
if (!descriptor) { descriptor = this.descriptor }
if (!props) { props = Object.keys(descriptor) }
if (!object) { object = this }
// import key
if (props.includes('alg')) {
return KeyChain.importKey(object, this._crypto).then(cryptoKey => {
if (cryptoKey.type === 'private' && !container.privateKey) {
Object.defineProperty(container, 'privateKey', {
enumerable: false,
value: cryptoKey
})
}
if (cryptoKey.type === 'public' && !container.publicKey) {
Object.defineProperty(container, 'publicKey', {
enumerable: false,
value: cryptoKey
})
}
})
// import key pair structure (has privateJwk and publicJwk)
} else if (props.includes('privateJwk') && props.includes('publicJwk')) {
// Import both private and public keys
return Promise.all([
KeyChain.importKey(object.privateJwk, this._crypto),
KeyChain.importKey(object.publicJwk, this._crypto)
]).then(([privateKey, publicKey]) => {
if (!object.privateKey) {
Object.defineProperty(object, 'privateKey', {
enumerable: false,
value: privateKey
})
}
if (!object.publicKey) {
Object.defineProperty(object, 'publicKey', {
enumerable: false,
value: publicKey
})
}
})
// recurse
} else {
return Promise.all(
props.map(name => {
let subDescriptor = descriptor[name]
let subObject = object[name]
let subProps = Object.keys(subObject)
//console.log('RECURSE WITH', name, subDescriptor, subObject, subProps)
return this.importKeys({
descriptor: subDescriptor,
object: subObject,
container: object,
props: subProps
})
})
)
}
}
/**
* rotate
*
* @param {Object} context
* @returns {Promise}
*/
rotate ({source, container, jwks} = {}) {
// initial call requires no arguments
// these values are passed when recursing
if (!source) { source = this.descriptor }
if (!container) { container = this }
if (!jwks) { jwks = this.jwks = { keys: [] } }
// do as much in parallel as possible
return Promise.all(
Object.keys(source).map(key => {
let params = source[key]
// generate key(pair), assign resulting object to keychain,
// and add JWK for public key to JWK Set
if (params.alg) {
return KeyChain.generateKey(params, this._crypto).then(result => {
container[key] = result
if (result.publicJwk) {
jwks.keys.push(result.publicJwk)
}
})
// recurse
} else if (typeof params === 'object') {
if (!container[key]) {
container[key] = {}
}
return this.rotate({
source: source[key],
container: container[key],
jwks
})
// invalid descriptor
} else {
throw new InvalidDescriptorError(key, params)
}
})
).then(() => {
// cache the JSON serialization of the keychain for publication
this.jwkSet = JSON.stringify(this.jwks)
// resulting value is the keychain itself
return this
})
}
}
/**
* Export
*/
module.exports = KeyChain