Skip to content

Commit

Permalink
refactor: less relying on KeyObject prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Mar 4, 2019
1 parent b9908e8 commit d5fc18e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
7 changes: 1 addition & 6 deletions lib/help/key_utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const keyto = require('@trust/keyto')

const KeyObject = require('./key_object')
const SUPPORTED = new Set(['EC', 'RSA'])

module.exports.pemToJwk = (keyObject) => {
if (!(keyObject instanceof KeyObject) || keyObject.type === 'secret') {
throw new TypeError('unsupported keyObject type')
}

module.exports.keyObjectToJWK = (keyObject) => {
const type = keyObject.type === 'private' ? 'pkcs8' : 'spki'
const format = 'pem'

Expand Down
8 changes: 4 additions & 4 deletions lib/jwk/key/base.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { pemToJwk } = require('../../help/key_utils')
const { keyObjectToJWK } = require('../../help/key_utils')
const thumbprint = require('../thumbprint')
const { THUMBPRINT_MATERIAL, PUBLIC_MEMBERS, PRIVATE_MEMBERS } = require('../../help/symbols')
const KeyObject = require('../../help/key_object')
const { KEYOBJECT } = require('../../help/symbols')
const isObject = require('../../help/is_object')

const USES = new Set(['sig', 'enc'])

Expand All @@ -11,7 +11,7 @@ const defineLazyComponents = (obj) => {
Object.defineProperties(obj, [...props].reduce((acc, component) => {
acc[component] = {
get () {
const jwk = pemToJwk(this[KEYOBJECT])
const jwk = keyObjectToJWK(this[KEYOBJECT])
Object.defineProperties(
this,
Object.entries(jwk)
Expand Down Expand Up @@ -52,7 +52,7 @@ class Key {
}

Object.defineProperties(this, {
[KEYOBJECT]: { value: keyObject instanceof KeyObject ? keyObject : undefined },
[KEYOBJECT]: { value: isObject(keyObject) ? undefined : keyObject },
kty: { value: keyObject.type === 'secret' ? 'oct' : keyObject.asymmetricKeyType.toUpperCase(), enumerable: true },
private: { value: keyObject.type === 'private', enumerable: keyObject.type === 'private' },
public: { value: keyObject.type === 'public', enumerable: keyObject.type === 'public' },
Expand Down
28 changes: 10 additions & 18 deletions test/help/key_utils.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
const test = require('ava')
const { createPublicKey, createPrivateKey, createSecretKey } = require('crypto')
const { createPublicKey, createPrivateKey } = require('crypto')

const { pemToJwk, jwkToPem } = require('../../lib/help/key_utils')
const { keyObjectToJWK, jwkToPem } = require('../../lib/help/key_utils')
const { JWK: fixtures } = require('../fixtures')
const clone = obj => JSON.parse(JSON.stringify(obj))

test('pemToJwk only works for EC and RSA', t => {
;[{}, null, undefined, false, 1, '', 'foo', Buffer, createSecretKey(Buffer.from('foo'))].forEach((value) => {
t.throws(() => {
pemToJwk(value)
}, { instanceOf: TypeError, message: 'unsupported keyObject type' })
})
})

test('jwkToPem only works for EC and RSA', t => {
t.throws(() => {
jwkToPem({ kty: 'oct' })
Expand All @@ -22,15 +14,15 @@ test('jwkToPem only works for EC and RSA', t => {
test('RSA Public key', t => {
const expected = fixtures.RSA_PUBLIC
const pem = createPublicKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})

test('RSA Private key', t => {
const expected = fixtures.RSA_PRIVATE
const pem = createPrivateKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})
Expand All @@ -39,15 +31,15 @@ test('EC P-256 Public key', t => {
const expected = clone(fixtures['P-256'])
delete expected.d
const pem = createPublicKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})

test('EC P-256 Private key', t => {
const expected = fixtures['P-256']
const pem = createPrivateKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})
Expand All @@ -56,15 +48,15 @@ test('EC P-384 Public key', t => {
const expected = clone(fixtures['P-384'])
delete expected.d
const pem = createPublicKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})

test('EC P-384 Private key', t => {
const expected = fixtures['P-384']
const pem = createPrivateKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})
Expand All @@ -73,15 +65,15 @@ test('EC P-521 Public key', t => {
const expected = clone(fixtures['P-521'])
delete expected.d
const pem = createPublicKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})

test('EC P-521 Private key', t => {
const expected = fixtures['P-521']
const pem = createPrivateKey(jwkToPem(expected))
const actual = pemToJwk(pem)
const actual = keyObjectToJWK(pem)

t.deepEqual(actual, expected)
})

0 comments on commit d5fc18e

Please sign in to comment.