From 39a98e70ade333116aac22f48a57fcb67324f53b Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Sat, 3 Nov 2018 14:40:03 +0100 Subject: [PATCH] refactor: move kid material to the key level --- lib/jwk/base.js | 6 +++++- lib/jwk/ec.js | 4 ++++ lib/jwk/oct.js | 4 ++++ lib/jwk/rsa.js | 4 ++++ lib/jwk/thumbprint.js | 18 +----------------- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/jwk/base.js b/lib/jwk/base.js index 31a9fa0c..a3724770 100644 --- a/lib/jwk/base.js +++ b/lib/jwk/base.js @@ -74,7 +74,7 @@ class Key { enumerable: true, ...(kid ? { value: kid } : { get () { - Object.defineProperty(this, 'kid', { value: thumbprint.kid(this), configurable: false }) + Object.defineProperty(this, 'kid', { value: thumbprint.kid(this.thumbprintMaterial()), configurable: false }) return this.kid }, configurable: true @@ -86,6 +86,10 @@ class Key { defineLazyComponents(this) } } + + thumbprintMaterial () { + throw new Error('not implemented') + } } module.exports = Key diff --git a/lib/jwk/ec.js b/lib/jwk/ec.js index 37a9eadd..2abf5919 100644 --- a/lib/jwk/ec.js +++ b/lib/jwk/ec.js @@ -34,6 +34,10 @@ class ECKey extends Key { }) } + thumbprintMaterial () { + return { crv: this.crv, kty: 'EC', x: this.x, y: this.y } + } + algorithms (operation) { // TODO: operation empty, return all supported or throw diff --git a/lib/jwk/oct.js b/lib/jwk/oct.js index 67ec9564..ccaa4aa0 100644 --- a/lib/jwk/oct.js +++ b/lib/jwk/oct.js @@ -23,6 +23,10 @@ class OctKey extends Key { }) } + thumbprintMaterial () { + return { k: this.k, kty: 'oct' } + } + algorithms (operation) { // TODO: operation empty, return all supported or throw diff --git a/lib/jwk/rsa.js b/lib/jwk/rsa.js index 44d6afcb..0de3930b 100644 --- a/lib/jwk/rsa.js +++ b/lib/jwk/rsa.js @@ -34,6 +34,10 @@ class RSAKey extends Key { }) } + thumbprintMaterial () { + return { e: this.e, kty: 'RSA', n: this.n } + } + algorithms (operation) { // TODO: operation empty, return all supported or throw diff --git a/lib/jwk/thumbprint.js b/lib/jwk/thumbprint.js index 98c1bb56..a21a8641 100644 --- a/lib/jwk/thumbprint.js +++ b/lib/jwk/thumbprint.js @@ -2,20 +2,4 @@ const { createHash } = require('crypto') const base64url = require('../help/base64url') -module.exports.kid = (key) => { - let json - - switch (key.kty) { - case 'RSA': - json = JSON.stringify({ e: key.e, kty: 'RSA', n: key.n }) - break - case 'EC': - json = JSON.stringify({ crv: key.crv, kty: 'EC', x: key.x, y: key.y }) - break - case 'oct': - json = JSON.stringify({ k: key.k, kty: 'oct' }) - break - } - - return base64url.encode(createHash('sha256').update(json).digest()) -} +module.exports.kid = components => base64url.encode(createHash('sha256').update(JSON.stringify(components)).digest())