feat: add key.secret<boolean> and key.type<string> for completeness

This commit is contained in:
Filip Skokan 2019-03-30 14:09:03 +01:00
parent 70b944634a
commit 2dd7053a4a
6 changed files with 38 additions and 0 deletions

View file

@ -29,8 +29,10 @@ I can continue maintaining it and adding new features carefree. You may also don
- [key.alg](#keyalg) - [key.alg](#keyalg)
- [key.use](#keyuse) - [key.use](#keyuse)
- [key.kid](#keykid) - [key.kid](#keykid)
- [key.type](#keytype)
- [key.public](#keypublic) - [key.public](#keypublic)
- [key.private](#keyprivate) - [key.private](#keyprivate)
- [key.secret](#keysecret)
- [key.algorithms([operation])](#keyalgorithmsoperation) - [key.algorithms([operation])](#keyalgorithmsoperation)
- [key.toJWK([private])](#keytojwkprivate) - [key.toJWK([private])](#keytojwkprivate)
- JWK.importKey - JWK.importKey
@ -110,6 +112,14 @@ defined in [RFC7638][spec-thumbprint].
--- ---
#### `key.type`
Returns the type of key. One of 'private', 'public' or 'secret'
- `<string>`
---
#### `key.public` #### `key.public`
Returns true/false if the key is asymmetric and public. Returns false for symmetric keys. 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
- `<boolean>` - `<boolean>`
#### `key.secret`
Returns true/false if the key is symmetric. Returns false for asymmetric keys.
- `<boolean>`
--- ---
#### `key.algorithms([operation])` #### `key.algorithms([operation])`

10
lib/index.d.ts vendored
View file

@ -11,13 +11,17 @@ interface KeyParameters {
type curve = 'P-256' | 'P-256K' | 'P-384' | 'P-521' type curve = 'P-256' | 'P-256K' | 'P-384' | 'P-521'
type keyType = 'RSA' | 'EC' | 'oct' type keyType = 'RSA' | 'EC' | 'oct'
type keyOperation = 'encrypt' | 'decrypt' | 'sign' | 'verify' | 'wrapKey' | 'unwrapKey' type keyOperation = 'encrypt' | 'decrypt' | 'sign' | 'verify' | 'wrapKey' | 'unwrapKey'
type asymmetricKeyObjectTypes = 'private' | 'public'
type keyObjectTypes = asymmetricKeyObjectTypes | 'secret'
export namespace JWK { export namespace JWK {
class Key { class Key {
kty: keyType kty: keyType
type: keyObjectTypes
private: boolean private: boolean
public: boolean public: boolean
secret: boolean
alg?: string alg?: string
use?: use use?: use
kid: string kid: string
@ -53,6 +57,8 @@ export namespace JWK {
class RSAKey extends Key { class RSAKey extends Key {
kty: 'RSA' kty: 'RSA'
type: asymmetricKeyObjectTypes
secret: false
e: string e: string
n: string n: string
d?: string d?: string
@ -67,6 +73,8 @@ export namespace JWK {
class ECKey extends Key { class ECKey extends Key {
kty: 'EC' kty: 'EC'
secret: false
type: asymmetricKeyObjectTypes
crv: curve crv: curve
x: string x: string
y: string y: string
@ -77,8 +85,10 @@ export namespace JWK {
class OctKey extends Key { class OctKey extends Key {
kty: 'oct' kty: 'oct'
type: 'secret'
private: false private: false
public: false public: false
secret: true
k: string k: string
toJWK(private?: boolean): JWKOctKey toJWK(private?: boolean): JWKOctKey

View file

@ -28,8 +28,10 @@ class Key {
Object.defineProperties(this, { Object.defineProperties(this, {
[KEYOBJECT]: { value: isObject(keyObject) ? undefined : keyObject }, [KEYOBJECT]: { value: isObject(keyObject) ? undefined : keyObject },
type: { value: keyObject.type },
private: { value: keyObject.type === 'private' }, private: { value: keyObject.type === 'private' },
public: { value: keyObject.type === 'public' }, public: { value: keyObject.type === 'public' },
secret: { value: keyObject.type === 'secret' },
alg: { value: alg, enumerable: alg !== undefined }, alg: { value: alg, enumerable: alg !== undefined },
use: { value: use, enumerable: use !== undefined }, use: { value: use, enumerable: use !== undefined },
kid: { kid: {

View file

@ -51,6 +51,8 @@ Object.entries({
test(`${crv} EC Private key`, hasProperty, key, 'kty', 'EC') 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, 'private', true)
test(`${crv} EC Private key`, hasProperty, key, 'public', false) 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`, hasProperty, key, 'use', undefined)
test(`${crv} EC Private key algorithms (no operation)`, t => { 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, 'kty', 'EC')
test(`${crv} EC Public key`, hasProperty, key, 'private', false) 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, '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`, hasProperty, key, 'use', undefined)
test(`${crv} EC Public key algorithms (no operation)`, t => { test(`${crv} EC Public key algorithms (no operation)`, t => {

View file

@ -24,7 +24,9 @@ test('oct key', hasProperty, key, 'kid', 'DWBh0SEIAPYh1x5uvot4z3AhaikHkxNJa3Ada2
test('oct key', hasProperty, key, 'kty', 'oct') test('oct key', hasProperty, key, 'kty', 'oct')
test('oct key', hasProperty, key, 'length', 48) test('oct key', hasProperty, key, 'length', 48)
test('oct key', hasProperty, key, 'private', false) 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, 'public', false)
test('oct key', hasProperty, key, 'secret', true)
test('oct key', hasProperty, key, 'use', undefined) test('oct key', hasProperty, key, 'use', undefined)
test('supports all sign algs (no use)', t => { test('supports all sign algs (no use)', t => {

View file

@ -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, 'length', 2048)
test(`RSA Private key`, hasProperty, key, 'private', true) test(`RSA Private key`, hasProperty, key, 'private', true)
test(`RSA Private key`, hasProperty, key, 'public', false) 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`, hasProperty, key, 'use', undefined)
test('RSA Private key algorithms (no operation)', t => { 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, 'length', 2048)
test(`RSA Public key`, hasProperty, key, 'private', false) test(`RSA Public key`, hasProperty, key, 'private', false)
test(`RSA Public key`, hasProperty, key, 'public', true) 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 Public key`, hasProperty, key, 'use', undefined)
test('RSA EC Public key algorithms (no operation)', t => { test('RSA EC Public key algorithms (no operation)', t => {