Skip to content

Commit

Permalink
feat: add key.secret<boolean> and key.type<string> for completeness
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Apr 1, 2019
1 parent 70b9446 commit 2dd7053
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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])`
Expand Down
10 changes: 10 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +57,8 @@ export namespace JWK {

class RSAKey extends Key {
kty: 'RSA'
type: asymmetricKeyObjectTypes
secret: false
e: string
n: string
d?: string
Expand All @@ -67,6 +73,8 @@ export namespace JWK {

class ECKey extends Key {
kty: 'EC'
secret: false
type: asymmetricKeyObjectTypes
crv: curve
x: string
y: string
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/jwk/key/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 4 additions & 0 deletions test/jwk/ec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions test/jwk/oct.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 4 additions & 0 deletions test/jwk/rsa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 2dd7053

Please sign in to comment.