From 2dd7053a4afc9e02a3ece210ab232841d9c19a3f Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Sat, 30 Mar 2019 14:09:03 +0100 Subject: [PATCH] feat: add key.secret and key.type for completeness --- docs/README.md | 16 ++++++++++++++++ lib/index.d.ts | 10 ++++++++++ lib/jwk/key/base.js | 2 ++ test/jwk/ec.test.js | 4 ++++ test/jwk/oct.test.js | 2 ++ test/jwk/rsa.test.js | 4 ++++ 6 files changed, 38 insertions(+) diff --git a/docs/README.md b/docs/README.md index dc477fae7b..86c42b5dab 100644 --- a/docs/README.md +++ b/docs/README.md @@ -29,8 +29,10 @@ I can continue maintaining it and adding new features carefree. You may also don - [key.alg](#keyalg) - [key.use](#keyuse) - [key.kid](#keykid) + - [key.type](#keytype) - [key.public](#keypublic) - [key.private](#keyprivate) + - [key.secret](#keysecret) - [key.algorithms([operation])](#keyalgorithmsoperation) - [key.toJWK([private])](#keytojwkprivate) - JWK.importKey @@ -110,6 +112,14 @@ defined in [RFC7638][spec-thumbprint]. --- +#### `key.type` + +Returns the type of key. One of 'private', 'public' or 'secret' + +- `` + +--- + #### `key.public` Returns true/false if the key is asymmetric and public. Returns false for symmetric keys. @@ -124,6 +134,12 @@ Returns true/false if the key is asymmetric and private. Returns false for symme - `` +#### `key.secret` + +Returns true/false if the key is symmetric. Returns false for asymmetric keys. + +- `` + --- #### `key.algorithms([operation])` diff --git a/lib/index.d.ts b/lib/index.d.ts index d74fe1dcc2..18f1dce782 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -11,13 +11,17 @@ interface KeyParameters { type curve = 'P-256' | 'P-256K' | 'P-384' | 'P-521' type keyType = 'RSA' | 'EC' | 'oct' type keyOperation = 'encrypt' | 'decrypt' | 'sign' | 'verify' | 'wrapKey' | 'unwrapKey' +type asymmetricKeyObjectTypes = 'private' | 'public' +type keyObjectTypes = asymmetricKeyObjectTypes | 'secret' export namespace JWK { class Key { kty: keyType + type: keyObjectTypes private: boolean public: boolean + secret: boolean alg?: string use?: use kid: string @@ -53,6 +57,8 @@ export namespace JWK { class RSAKey extends Key { kty: 'RSA' + type: asymmetricKeyObjectTypes + secret: false e: string n: string d?: string @@ -67,6 +73,8 @@ export namespace JWK { class ECKey extends Key { kty: 'EC' + secret: false + type: asymmetricKeyObjectTypes crv: curve x: string y: string @@ -77,8 +85,10 @@ export namespace JWK { class OctKey extends Key { kty: 'oct' + type: 'secret' private: false public: false + secret: true k: string toJWK(private?: boolean): JWKOctKey diff --git a/lib/jwk/key/base.js b/lib/jwk/key/base.js index e1673a0b9c..11e38b844f 100644 --- a/lib/jwk/key/base.js +++ b/lib/jwk/key/base.js @@ -28,8 +28,10 @@ class Key { Object.defineProperties(this, { [KEYOBJECT]: { value: isObject(keyObject) ? undefined : keyObject }, + type: { value: keyObject.type }, private: { value: keyObject.type === 'private' }, public: { value: keyObject.type === 'public' }, + secret: { value: keyObject.type === 'secret' }, alg: { value: alg, enumerable: alg !== undefined }, use: { value: use, enumerable: use !== undefined }, kid: { diff --git a/test/jwk/ec.test.js b/test/jwk/ec.test.js index e8ecddf123..6c7687834a 100644 --- a/test/jwk/ec.test.js +++ b/test/jwk/ec.test.js @@ -51,6 +51,8 @@ Object.entries({ test(`${crv} EC Private key`, hasProperty, key, 'kty', 'EC') test(`${crv} EC Private key`, hasProperty, key, 'private', true) test(`${crv} EC Private key`, hasProperty, key, 'public', false) + test(`${crv} EC Private key`, hasProperty, key, 'secret', false) + test(`${crv} EC Private key`, hasProperty, key, 'type', 'private') test(`${crv} EC Private key`, hasProperty, key, 'use', undefined) test(`${crv} EC Private key algorithms (no operation)`, t => { @@ -174,6 +176,8 @@ Object.entries({ test(`${crv} EC Public key`, hasProperty, key, 'kty', 'EC') test(`${crv} EC Public key`, hasProperty, key, 'private', false) test(`${crv} EC Public key`, hasProperty, key, 'public', true) + test(`${crv} EC Public key`, hasProperty, key, 'secret', false) + test(`${crv} EC Public key`, hasProperty, key, 'type', 'public') test(`${crv} EC Public key`, hasProperty, key, 'use', undefined) test(`${crv} EC Public key algorithms (no operation)`, t => { diff --git a/test/jwk/oct.test.js b/test/jwk/oct.test.js index 01ab55b105..6b1695f38b 100644 --- a/test/jwk/oct.test.js +++ b/test/jwk/oct.test.js @@ -24,7 +24,9 @@ test('oct key', hasProperty, key, 'kid', 'DWBh0SEIAPYh1x5uvot4z3AhaikHkxNJa3Ada2 test('oct key', hasProperty, key, 'kty', 'oct') test('oct key', hasProperty, key, 'length', 48) test('oct key', hasProperty, key, 'private', false) +test('oct key', hasProperty, key, 'type', 'secret') test('oct key', hasProperty, key, 'public', false) +test('oct key', hasProperty, key, 'secret', true) test('oct key', hasProperty, key, 'use', undefined) test('supports all sign algs (no use)', t => { diff --git a/test/jwk/rsa.test.js b/test/jwk/rsa.test.js index e35d604c75..003386b184 100644 --- a/test/jwk/rsa.test.js +++ b/test/jwk/rsa.test.js @@ -27,6 +27,8 @@ test(`RSA key .algorithms invalid operation`, t => { test(`RSA Private key`, hasProperty, key, 'length', 2048) test(`RSA Private key`, hasProperty, key, 'private', true) test(`RSA Private key`, hasProperty, key, 'public', false) + test(`RSA Private key`, hasProperty, key, 'secret', false) + test(`RSA Private key`, hasProperty, key, 'type', 'private') test(`RSA Private key`, hasProperty, key, 'use', undefined) test('RSA Private key algorithms (no operation)', t => { @@ -151,6 +153,8 @@ test(`RSA key .algorithms invalid operation`, t => { test(`RSA Public key`, hasProperty, key, 'length', 2048) test(`RSA Public key`, hasProperty, key, 'private', false) test(`RSA Public key`, hasProperty, key, 'public', true) + test(`RSA Public key`, hasProperty, key, 'secret', false) + test(`RSA Public key`, hasProperty, key, 'type', 'public') test(`RSA Public key`, hasProperty, key, 'use', undefined) test('RSA EC Public key algorithms (no operation)', t => {