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.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'
- `<string>`
---
#### `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
- `<boolean>`
#### `key.secret`
Returns true/false if the key is symmetric. Returns false for asymmetric keys.
- `<boolean>`
---
#### `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 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

View file

@ -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: {

View file

@ -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 => {

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, '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 => {

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, '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 => {