From e97219e062955da01722d18f19cb62384fe9fd30 Mon Sep 17 00:00:00 2001 From: Nadun Indunil Date: Thu, 14 Feb 2019 00:09:14 +0530 Subject: [PATCH 1/5] add: node-jose typings --- types/node-jose/index.d.ts | 342 +++++++++++++++++++++++++++++ types/node-jose/node-jose-tests.ts | 300 +++++++++++++++++++++++++ types/node-jose/tsconfig.json | 23 ++ types/node-jose/tslint.json | 3 + 4 files changed, 668 insertions(+) create mode 100644 types/node-jose/index.d.ts create mode 100644 types/node-jose/node-jose-tests.ts create mode 100644 types/node-jose/tsconfig.json create mode 100644 types/node-jose/tslint.json diff --git a/types/node-jose/index.d.ts b/types/node-jose/index.d.ts new file mode 100644 index 0000000000..b0b3bd0bc8 --- /dev/null +++ b/types/node-jose/index.d.ts @@ -0,0 +1,342 @@ +// Type definitions for node-jose 1.1.1 +// Project: https://github.com/cisco/node-jose +// Definitions by: Nadun Indunil +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +export function canYouSee(ks: JWK.Key | JWK.KeyStore, opts: object): JWS.Verifier; + +export namespace JWA { + type decryptEncryptOptions = { + aad?: Buffer; + adata?: Buffer; + iv?: Buffer; + tag?: Buffer; // Not used in encrypt + mac?: Buffer; // Not used in encrypt + epu?: Buffer; // encryption party info + epv?: Buffer; // encryption party info + kdata?: Buffer; + epk?: Buffer; // ephemeral pub key used in ec + enc?: string; // algorithm to use in ec + alg?: string; // variation of enc, probably oversight in lib code + apu?: Buffer; // agreement party info used in ec + apv?: Buffer; // agreement party info used in ec + p2s?: Buffer; // used in pbes + p2c?: number; // used in pbes + }; + + type deriveOptions = { + length?: number; // key length + otherInfo?: Buffer; // info used in concatkdf + public?: Buffer; // public key used in ecdh + hash?: Buffer; // hash used in ecdh + salt?: Buffer; // salt value used in hkdf + info?: Buffer; // app identifier info used in hkdf + }; + + type encryptReturn = { + data: Buffer; // The cipher text + tag?: Buffer; // The tag used in some algorithms + }; + + type signReturn = { + data: Buffer; // the data passed into the sign function + mac: Buffer; // the signature for `data` + }; + + type signVerifyOptions = { loose?: boolean }; + + type verifyReturn = { + data: Buffer; // the data passed into the verify function + mac: Buffer; // the signature for `data` + valid: boolean; // whether the signature matches the data + }; + + function decrypt( + alg: string, + key: string | Buffer, + cdata: string | Buffer, + props?: decryptEncryptOptions + ): Promise; + + function derive(alg: string, key: string | Buffer, props?: deriveOptions): Promise; + + function digest(alg: string, data: string | Buffer, props?: any): Promise; + + function encrypt( + alg: string, + key: string | Buffer, + pdata: string | Buffer, + props?: decryptEncryptOptions + ): Promise; + + function sign( + alg: string, + key: string | Buffer, + pdata: string | Buffer, + props: signVerifyOptions + ): Promise; + + function verify( + alg: string, + key: string | Buffer, + pdata: string | Buffer, + mac: string | Buffer, + props: signVerifyOptions + ): Promise; +} + +export namespace JWE { + function createEncrypt(key: JWK.Key): JWE.Encryptor; + function createEncrypt(keys: JWK.Key[]): JWE.Encryptor; + function createEncrypt( + options: { + format?: 'compact' | 'flattened'; + zip?: boolean; + fields?: object; + }, + key: JWK.Key + ): JWE.Encryptor; + + function createDecrypt(key: JWK.Key | JWK.KeyStore, opts?: any): JWE.Decryptor; + + export interface Encryptor { + update(input: any): this; + final(): Promise; + } + + export interface Decryptor { + decrypt(input: string): Promise; + } + + export interface DecryptResult { + /** + * an array of the member names from the "protected" member + */ + protected: string[]; + /** + * the decrypted content (alternate) + */ + plaintext: Buffer; + } +} + +export namespace JWK { + const MODE_DECRYPT: string; + + const MODE_ENCRYPT: string; + + const MODE_SIGN: string; + + const MODE_UNWRAP: string; + + const MODE_VERIFY: string; + + const MODE_WRAP: string; + + function asKey( + key: string | Buffer | object | RawKey, + form?: 'json' | 'private' | 'pkcs8' | 'public' | 'spki' | 'pkix' | 'x509' | 'pem' + ): Promise; + /** + * To import a JWK-set as a keystore + */ + function asKeyStore(ks: object | string): Promise; + + function createKey(kty: any, size: any, props: any): Promise; + /** + * To create an empty keystore + */ + function createKeyStore(): JWK.KeyStore; + + function isKey(input: any): input is JWK.Key; + + function isKeyStore(input: any): input is JWK.KeyStore; + + export type KeyUse = 'sig' | 'enc' | 'desc'; + + export interface JWEEncryptor { + update(input: any): this; + final(): Promise; + } + + export interface RawKey { + alg: string; + kty: string; + use: KeyUse; + + // e and n make up the public key + e: string; + n: string; + } + + export interface KeyStoreGetFilter { + kty?: string; + use?: KeyUse; + alg?: string; + } + + export interface KeyStoreGetOptions extends KeyStoreGetFilter { + kid: string; + } + + export interface KeyStore { + /** + * To export the public keys of a keystore as a JWK-set + */ + toJSON(exportPrivateKeys?: boolean): object; + /** + * To retrieve a key from a keystore + */ + get(kid: string, filter?: KeyStoreGetFilter): RawKey; + get(options: KeyStoreGetOptions): RawKey; + all(options?: Partial): RawKey[]; + add(key: RawKey): Promise; + /** + * @param key + * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER + * Buffer of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER + * @param form + * is either a: + * - "json" for a JSON stringified JWK + * - "private" for a DER encoded 'raw' private key + * - "pkcs8" for a DER encoded (unencrypted!) PKCS8 private key + * - "public" for a DER encoded SPKI public key (alternate to 'spki') + * - "spki" for a DER encoded SPKI public key + * - "pkix" for a DER encoded PKIX X.509 certificate + * - "x509" for a DER encoded PKIX X.509 certificate + * - "pem" for a PEM encoded of PKCS8 / SPKI / PKIX + */ + add( + key: string | Buffer | JWK.Key | object, + form?: 'json' | 'private' | 'pkcs8' | 'public' | 'spki' | 'pkix' | 'x509' | 'pem' + ): Promise; + + generate(kty: string, size?: string | number, props?: any): Promise; + + remove(key: JWK.Key): void; + } + + export interface Key { + keystore: JWK.KeyStore; + length: number; + kty: string; + kid: string; + use: KeyUse; + alg: string; + + toPEM(isPrivate?: boolean): string; + toJSON(isPrivate?: boolean, excluded?: string[]): object; + thumbprint(hash?: string): Promise; + } +} + +export namespace JWS { + function createSign(key: JWK.Key): JWS.Signer; + function createSign(keys: JWK.Key[]): JWS.Signer; + function createSign( + options: { + format?: 'compact' | 'flattened'; + alg?: string; + compact?: boolean; + fields?: object; + }, + key: JWK.Key | JWK.Key[] + ): JWS.Signer; + + /** + * Using a keystore. + */ + function createVerify(keyStore: JWK.KeyStore): JWS.Verifier; + + /** + * To verify using a key embedded in the JWS + */ + function createVerify(): JWS.Verifier; + + function createVerify( + input: string | JWK.Key | object, + opts?: { allowEmbeddedKey?: boolean; algorithms?: string[]; handlers?: any } + ): JWS.Verifier; + + export interface createSignResult { + signResult: object; + } + + export interface Signer { + update(input: Buffer | string, encoding?: string): this; + final(): Promise; + } + + export interface BaseResult { + /** + * the combined 'protected' and 'unprotected' header members + */ + header: object; + /** + * the signed content + */ + payload: Buffer; + /** + * The key used to verify the signature + */ + key: JWK.Key; + protected: string[]; + } + + export interface VerificationResult extends BaseResult { + /** + * the verified signature + */ + signature: Buffer | string; + } + + export interface Verifier { + verify(input: string, opts?: { allowEmbeddedKey?: boolean }): Promise; + } + + export interface exp { + complete(jws: any): any; + } + + export interface verifyOptions { + allowEmbeddedKey?: boolean; + algorithms?: string[]; + handlers: { exp: boolean | exp }; + } +} + +type parseReturn = { + type: 'JWS' | 'JWE'; + format: 'compact' | 'json'; + input: Buffer | string | object; + header: object; + perform: (ks: JWK.KeyStore) => Promise | Promise; +}; + +export function parse(input: Buffer | string | object): parseReturn; + +export namespace parse { + function compact(input: Buffer | string | object): parseReturn; + + function json(input: Buffer | string | object): parseReturn; +} + +export namespace util { + function asBuffer(input: string | Buffer, encoding?: string): Buffer; + + function randomBytes(len: number): Buffer; + + namespace base64url { + function decode(base64url: string): string; + + function encode(buffer: string | Buffer, encoding?: string): string; + } + + namespace utf8 { + function decode(input: string): string; + + function encode(input: string): string; + } +} diff --git a/types/node-jose/node-jose-tests.ts b/types/node-jose/node-jose-tests.ts new file mode 100644 index 0000000000..230a4fc474 --- /dev/null +++ b/types/node-jose/node-jose-tests.ts @@ -0,0 +1,300 @@ +import * as jose from 'node-jose'; + +const keystore = jose.JWK.createKeyStore(); +const output = keystore.toJSON(); +keystore.toJSON(true); + +jose.JWK.asKeyStore('input').then(result => {}); + +let key = keystore.get('kid'); + +key = keystore.get('kid', { kty: 'RSA' }); + +// ... and by 'use' +key = keystore.get('kid', { use: 'enc' }); + +// ... and by 'alg' +key = keystore.get('kid', { alg: 'RSA-OAEP' }); + +// ... and by 'kty' and 'use' +key = keystore.get('kid', { kty: 'RSA', use: 'enc' }); + +// same as above, but with a single {props} argument +key = keystore.get({ kid: 'kid', kty: 'RSA', use: 'enc' }); + +let everything = keystore.all(); + +// filter by 'kid' +everything = keystore.all({ kid: 'kid' }); + +// filter by 'kty' +everything = keystore.all({ kty: 'RSA' }); + +// filter by 'use' +everything = keystore.all({ use: 'enc' }); + +// filter by 'alg' +everything = keystore.all({ alg: 'RSA-OAEP' }); + +// filter by 'kid' + 'kty' + 'alg' +everything = keystore.all({ kid: 'kid', kty: 'RSA', alg: 'RSA-OAEP' }); + +keystore.add('input').then(function(result) {}); + +keystore.add('input', 'json').then(function(result) { + // {result} is a jose.JWK.Key +}); + +keystore.generate('oct', 256).then(function(result) { + // {result} is a jose.JWK.Key +}); + +// ... with properties +var props = { + kid: 'gBdaS-G8RLax2qgObTD94w', + alg: 'A256GCM', + use: 'enc' +}; + +let key2: jose.JWK.Key; +keystore.generate('oct', 256, props).then(function(result) { + // {result} is a jose.JWK.Key + key2 = result; + keystore.remove(key2); + + // where input is either a: + // * jose.JWK.Key instance + // * JSON Object representation of a JWK + + jose.JWK.asKey(key2).then(function(result) { + // {result} is a jose.JWK.Key + // {result.keystore} is a unique jose.JWK.KeyStore + }); + + // where input is either a: + // * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER + // * Buffer of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER + // form is either a: + // * "json" for a JSON stringified JWK + // * "pkcs8" for a DER encoded (unencrypted!) PKCS8 private key + // * "spki" for a DER encoded SPKI public key + // * "pkix" for a DER encoded PKIX X.509 certificate + // * "x509" for a DER encoded PKIX X.509 certificate + // * "pem" for a PEM encoded of PKCS8 / SPKI / PKIX + jose.JWK.asKey('input', 'json').then(function(result) { + // {result} is a jose.JWK.Key + // {result.keystore} is a unique jose.JWK.KeyStore + }); +}); + +jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { + // {result} is a jose.JWK.Key + // {result.keystore} is a unique jose.JWK.KeyStore + let output4 = result.toJSON(true); + result.thumbprint('hash').then(function(print) { + // {print} is a Buffer containing the thumbprint binary value + }); + + let key = result; + jose.JWS.createSign(key) + .update('input') + .final() + .then(function(result) { + // {result} is a JSON object -- JWS using the JSON General Serialization + }); + + jose.JWS.createSign({ format: 'flattened' }, key) + .update('input') + .final() + .then(function(result) { + // {result} is a JSON object -- JWS using the JSON Flattened Serialization + }); + + jose.JWS.createSign({ format: 'compact' }, key) + .update('input') + .final() + .then(function(result) { + // {result} is a String -- JWS using the Compact Serialization + }); + + jose.JWS.createSign({ alg: 'PS256' }, key) + .update('input') + .final() + .then(function(result) { + // .... + }); + + jose.JWS.createSign({ fields: { cty: 'jwk+json' } }, key) + .update('input') + .final() + .then(function(result) { + // .... + }); + + jose.JWS.createSign(key) + .update('input', 'utf8') + .final() + .then(function(result) { + // .... + }); + + let opts = { + algorithms: ['PS*'] + }; + jose.JWS.createVerify(key, opts) + .verify('input') + .then(function(result) { + // ... + }); + + opts = { + algorithms: ['*', '!HS*'] + }; + jose.JWS.createVerify(key, opts) + .verify('input') + .then(function(result) { + // ... + }); + + const opts2 = { + handlers: { + exp: true + } + }; + + jose.JWS.createVerify(key, opts2) + .verify('input') + .then(function(result) { + // ... + }); + + jose.JWE.createEncrypt(key) + .update('input') + .final() + .then(function(result) { + // {result} is a JSON Object -- JWE using the JSON General Serialization + }); + + jose.JWE.createEncrypt({ format: 'compact' }, key) + .update('input') + .final() + .then(function(result) { + // {result} is a String -- JWE using the Compact Serialization + }); + + jose.JWE.createEncrypt({ format: 'flattened' }, key) + .update('input') + .final() + .then(function(result) { + // {result} is a JSON Object -- JWE using the JSON Flattened Serialization + }); + + jose.JWE.createEncrypt({ zip: true }, key) + .update('input') + .final() + .then(function(result) { + // .... + }); + + jose.JWE.createEncrypt({ fields: { cty: 'jwk+json' } }, key) + .update('input') + .final() + .then(function(result) { + // .... + }); + + jose.JWE.createEncrypt([key, key]) + .update('input') + .final() + .then(function(result) { + // .... + }); + + jose.JWE.createDecrypt(key) + .decrypt('input') + .then(function(result) { + // .... + }); + + const opts3 = { + algorithms: ['dir', 'A*GCM'] + }; + jose.JWE.createDecrypt(key, opts3) + .decrypt('input') + .then(function(result) { + // ... + }); + + const opts4 = { + algorithms: ['*', '!RSA*'] + }; + jose.JWS.createVerify(key, opts4) + .verify('input') + .then(function(result) { + // ... + }); + + const opts5 = { + handlers: { + exp: true + } + }; + jose.JWE.createDecrypt(key, opts5) + .decrypt('input') + .then(function(result) { + // ... + }); +}); + +jose.JWS.createVerify(keystore) + .verify('input') + .then(function(result) { + // {result} is a Object with: + // * header: the combined 'protected' and 'unprotected' header members + // * payload: Buffer of the signed content + // * signature: Buffer of the verified signature + // * key: The key used to verify the signature + }); + +// {key} can be: +// * jose.JWK.Key +// * JSON object representing a JWK +jose.JWS.createVerify(key) + .verify('input') + .then(function(result) { + // ... + }); + +jose.JWS.createVerify() + .verify('input', { allowEmbeddedKey: true }) + .then(function(result) { + // ... + }); + +var verifier = jose.JWS.createVerify({ allowEmbeddedKey: true }); + +verifier.verify('input').then(function(result) { + // ... +}); + +jose.JWE.createDecrypt(keystore) + .decrypt('input') + .then(function(result) { + // {result} is a Object with: + // * header: the combined 'protected' and 'unprotected' header members + // * protected: an array of the member names from the "protected" member + // * key: Key used to decrypt + // * payload: Buffer of the decrypted content + // * plaintext: Buffer of the decrypted content (alternate) + }); + +jose.util.asBuffer('input'); + +jose.util.base64url.encode('input'); +jose.util.base64url.encode('input', 'utf8'); + +jose.util.base64url.encode('input'); + +jose.util.base64url.decode('input'); + +jose.util.randomBytes(32); diff --git a/types/node-jose/tsconfig.json b/types/node-jose/tsconfig.json new file mode 100644 index 0000000000..c01c2b1fb7 --- /dev/null +++ b/types/node-jose/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-jose-tests.ts" + ] +} \ No newline at end of file diff --git a/types/node-jose/tslint.json b/types/node-jose/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/node-jose/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file From 69b416ed9e28541db193485a145511a7f3ff71b1 Mon Sep 17 00:00:00 2001 From: Nadun Indunil Date: Thu, 14 Feb 2019 02:54:41 +0530 Subject: [PATCH 2/5] fix: lint in index --- types/node-jose/index.d.ts | 126 +++++++++++++++-------------- types/node-jose/node-jose-tests.ts | 4 +- 2 files changed, 67 insertions(+), 63 deletions(-) diff --git a/types/node-jose/index.d.ts b/types/node-jose/index.d.ts index b0b3bd0bc8..a7b295d464 100644 --- a/types/node-jose/index.d.ts +++ b/types/node-jose/index.d.ts @@ -1,14 +1,15 @@ -// Type definitions for node-jose 1.1.1 +// Type definitions for node-jose 1.1 // Project: https://github.com/cisco/node-jose // Definitions by: Nadun Indunil // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 /// export function canYouSee(ks: JWK.Key | JWK.KeyStore, opts: object): JWS.Verifier; export namespace JWA { - type decryptEncryptOptions = { + interface decryptEncryptOptions { aad?: Buffer; adata?: Buffer; iv?: Buffer; @@ -24,34 +25,36 @@ export namespace JWA { apv?: Buffer; // agreement party info used in ec p2s?: Buffer; // used in pbes p2c?: number; // used in pbes - }; + } - type deriveOptions = { + interface deriveOptions { length?: number; // key length otherInfo?: Buffer; // info used in concatkdf public?: Buffer; // public key used in ecdh hash?: Buffer; // hash used in ecdh salt?: Buffer; // salt value used in hkdf info?: Buffer; // app identifier info used in hkdf - }; + } - type encryptReturn = { + interface encryptReturn { data: Buffer; // The cipher text tag?: Buffer; // The tag used in some algorithms - }; + } - type signReturn = { + interface signReturn { data: Buffer; // the data passed into the sign function mac: Buffer; // the signature for `data` - }; + } - type signVerifyOptions = { loose?: boolean }; + interface signVerifyOptions { + loose?: boolean; + } - type verifyReturn = { + interface verifyReturn { data: Buffer; // the data passed into the verify function mac: Buffer; // the signature for `data` valid: boolean; // whether the signature matches the data - }; + } function decrypt( alg: string, @@ -88,8 +91,7 @@ export namespace JWA { } export namespace JWE { - function createEncrypt(key: JWK.Key): JWE.Encryptor; - function createEncrypt(keys: JWK.Key[]): JWE.Encryptor; + function createEncrypt(keys: JWK.Key | JWK.Key[]): Encryptor; function createEncrypt( options: { format?: 'compact' | 'flattened'; @@ -97,20 +99,20 @@ export namespace JWE { fields?: object; }, key: JWK.Key - ): JWE.Encryptor; + ): Encryptor; - function createDecrypt(key: JWK.Key | JWK.KeyStore, opts?: any): JWE.Decryptor; + function createDecrypt(key: JWK.Key | JWK.KeyStore, opts?: any): Decryptor; - export interface Encryptor { + interface Encryptor { update(input: any): this; final(): Promise; } - export interface Decryptor { - decrypt(input: string): Promise; + interface Decryptor { + decrypt(input: string): Promise; } - export interface DecryptResult { + interface DecryptResult { /** * an array of the member names from the "protected" member */ @@ -138,30 +140,30 @@ export namespace JWK { function asKey( key: string | Buffer | object | RawKey, form?: 'json' | 'private' | 'pkcs8' | 'public' | 'spki' | 'pkix' | 'x509' | 'pem' - ): Promise; + ): Promise; /** * To import a JWK-set as a keystore */ - function asKeyStore(ks: object | string): Promise; + function asKeyStore(ks: object | string): Promise; - function createKey(kty: any, size: any, props: any): Promise; + function createKey(kty: any, size: any, props: any): Promise; /** * To create an empty keystore */ - function createKeyStore(): JWK.KeyStore; - - function isKey(input: any): input is JWK.Key; + function createKeyStore(): KeyStore; - function isKeyStore(input: any): input is JWK.KeyStore; + function isKey(input: any): input is Key; - export type KeyUse = 'sig' | 'enc' | 'desc'; + function isKeyStore(input: any): input is KeyStore; - export interface JWEEncryptor { + type KeyUse = 'sig' | 'enc' | 'desc'; + + interface JWEEncryptor { update(input: any): this; final(): Promise; } - export interface RawKey { + interface RawKey { alg: string; kty: string; use: KeyUse; @@ -171,17 +173,17 @@ export namespace JWK { n: string; } - export interface KeyStoreGetFilter { + interface KeyStoreGetFilter { kty?: string; use?: KeyUse; alg?: string; } - export interface KeyStoreGetOptions extends KeyStoreGetFilter { + interface KeyStoreGetOptions extends KeyStoreGetFilter { kid: string; } - export interface KeyStore { + interface KeyStore { /** * To export the public keys of a keystore as a JWK-set */ @@ -192,7 +194,7 @@ export namespace JWK { get(kid: string, filter?: KeyStoreGetFilter): RawKey; get(options: KeyStoreGetOptions): RawKey; all(options?: Partial): RawKey[]; - add(key: RawKey): Promise; + add(key: RawKey): Promise; /** * @param key * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER @@ -209,17 +211,17 @@ export namespace JWK { * - "pem" for a PEM encoded of PKCS8 / SPKI / PKIX */ add( - key: string | Buffer | JWK.Key | object, + key: string | Buffer | Key | object, form?: 'json' | 'private' | 'pkcs8' | 'public' | 'spki' | 'pkix' | 'x509' | 'pem' - ): Promise; + ): Promise; - generate(kty: string, size?: string | number, props?: any): Promise; + generate(kty: string, size?: string | number, props?: any): Promise; - remove(key: JWK.Key): void; + remove(key: Key): void; } - export interface Key { - keystore: JWK.KeyStore; + interface Key { + keystore: KeyStore; length: number; kty: string; kid: string; @@ -233,8 +235,7 @@ export namespace JWK { } export namespace JWS { - function createSign(key: JWK.Key): JWS.Signer; - function createSign(keys: JWK.Key[]): JWS.Signer; + function createSign(keys: JWK.Key | JWK.Key[]): Signer; function createSign( options: { format?: 'compact' | 'flattened'; @@ -243,33 +244,26 @@ export namespace JWS { fields?: object; }, key: JWK.Key | JWK.Key[] - ): JWS.Signer; + ): Signer; /** * Using a keystore. */ - function createVerify(keyStore: JWK.KeyStore): JWS.Verifier; - - /** - * To verify using a key embedded in the JWS - */ - function createVerify(): JWS.Verifier; - function createVerify( - input: string | JWK.Key | object, + input?: string | JWK.Key | JWK.KeyStore | object, opts?: { allowEmbeddedKey?: boolean; algorithms?: string[]; handlers?: any } - ): JWS.Verifier; + ): Verifier; - export interface createSignResult { + interface createSignResult { signResult: object; } - export interface Signer { + interface Signer { update(input: Buffer | string, encoding?: string): this; final(): Promise; } - export interface BaseResult { + interface BaseResult { /** * the combined 'protected' and 'unprotected' header members */ @@ -285,35 +279,35 @@ export namespace JWS { protected: string[]; } - export interface VerificationResult extends BaseResult { + interface VerificationResult extends BaseResult { /** * the verified signature */ signature: Buffer | string; } - export interface Verifier { + interface Verifier { verify(input: string, opts?: { allowEmbeddedKey?: boolean }): Promise; } - export interface exp { + interface exp { complete(jws: any): any; } - export interface verifyOptions { + interface verifyOptions { allowEmbeddedKey?: boolean; algorithms?: string[]; handlers: { exp: boolean | exp }; } } -type parseReturn = { +interface parseReturn { type: 'JWS' | 'JWE'; format: 'compact' | 'json'; input: Buffer | string | object; header: object; perform: (ks: JWK.KeyStore) => Promise | Promise; -}; +} export function parse(input: Buffer | string | object): parseReturn; @@ -340,3 +334,13 @@ export namespace util { function encode(input: string): string; } } + +declare const _default: { + JWA: typeof JWA; + JWE: typeof JWE; + JWS: typeof JWS; + JWK: typeof JWK; + parse: typeof parse; + util: typeof util; +}; +export default _default; diff --git a/types/node-jose/node-jose-tests.ts b/types/node-jose/node-jose-tests.ts index 230a4fc474..9c9866f132 100644 --- a/types/node-jose/node-jose-tests.ts +++ b/types/node-jose/node-jose-tests.ts @@ -50,7 +50,7 @@ keystore.generate('oct', 256).then(function(result) { }); // ... with properties -var props = { +let props = { kid: 'gBdaS-G8RLax2qgObTD94w', alg: 'A256GCM', use: 'enc' @@ -271,7 +271,7 @@ jose.JWS.createVerify() // ... }); -var verifier = jose.JWS.createVerify({ allowEmbeddedKey: true }); +let verifier = jose.JWS.createVerify({ allowEmbeddedKey: true }); verifier.verify('input').then(function(result) { // ... From 619748b62cd40af5b19ba67fe1bb2f97e6b1fb3c Mon Sep 17 00:00:00 2001 From: Nadun Indunil Date: Thu, 14 Feb 2019 12:03:45 +0530 Subject: [PATCH 3/5] add: new test and lint fix --- types/node-jose/index.d.ts | 2 +- types/node-jose/node-jose-tests.ts | 98 +++++++++++------------------- 2 files changed, 38 insertions(+), 62 deletions(-) diff --git a/types/node-jose/index.d.ts b/types/node-jose/index.d.ts index a7b295d464..b27ecde774 100644 --- a/types/node-jose/index.d.ts +++ b/types/node-jose/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/cisco/node-jose // Definitions by: Nadun Indunil // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 +// TypeScript Version: 3.3 /// diff --git a/types/node-jose/node-jose-tests.ts b/types/node-jose/node-jose-tests.ts index 9c9866f132..dbeaccc9dc 100644 --- a/types/node-jose/node-jose-tests.ts +++ b/types/node-jose/node-jose-tests.ts @@ -39,102 +39,78 @@ everything = keystore.all({ alg: 'RSA-OAEP' }); // filter by 'kid' + 'kty' + 'alg' everything = keystore.all({ kid: 'kid', kty: 'RSA', alg: 'RSA-OAEP' }); -keystore.add('input').then(function(result) {}); +keystore.add('input').then(result => {}); -keystore.add('input', 'json').then(function(result) { +keystore.add('input', 'json').then(result => { // {result} is a jose.JWK.Key }); -keystore.generate('oct', 256).then(function(result) { +keystore.generate('oct', 256).then(result => { // {result} is a jose.JWK.Key }); // ... with properties -let props = { +const props = { kid: 'gBdaS-G8RLax2qgObTD94w', alg: 'A256GCM', use: 'enc' }; let key2: jose.JWK.Key; -keystore.generate('oct', 256, props).then(function(result) { - // {result} is a jose.JWK.Key +keystore.generate('oct', 256, props).then(result => { key2 = result; keystore.remove(key2); - // where input is either a: - // * jose.JWK.Key instance - // * JSON Object representation of a JWK + jose.JWK.asKey(key2).then(result => {}); - jose.JWK.asKey(key2).then(function(result) { - // {result} is a jose.JWK.Key - // {result.keystore} is a unique jose.JWK.KeyStore - }); - - // where input is either a: - // * String serialization of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER - // * Buffer of a JSON JWK/(base64-encoded) PEM/(binary-encoded) DER - // form is either a: - // * "json" for a JSON stringified JWK - // * "pkcs8" for a DER encoded (unencrypted!) PKCS8 private key - // * "spki" for a DER encoded SPKI public key - // * "pkix" for a DER encoded PKIX X.509 certificate - // * "x509" for a DER encoded PKIX X.509 certificate - // * "pem" for a PEM encoded of PKCS8 / SPKI / PKIX - jose.JWK.asKey('input', 'json').then(function(result) { - // {result} is a jose.JWK.Key - // {result.keystore} is a unique jose.JWK.KeyStore - }); + jose.JWK.asKey('input', 'json').then(result => {}); }); -jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { - // {result} is a jose.JWK.Key - // {result.keystore} is a unique jose.JWK.KeyStore - let output4 = result.toJSON(true); - result.thumbprint('hash').then(function(print) { - // {print} is a Buffer containing the thumbprint binary value - }); +jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(result => { + const output4 = result.toJSON(true); + result.thumbprint('hash').then(print => {}); + + const key = result; - let key = result; jose.JWS.createSign(key) .update('input') .final() - .then(function(result) { + .then(result => { // {result} is a JSON object -- JWS using the JSON General Serialization }); jose.JWS.createSign({ format: 'flattened' }, key) .update('input') .final() - .then(function(result) { + .then(result => { // {result} is a JSON object -- JWS using the JSON Flattened Serialization }); jose.JWS.createSign({ format: 'compact' }, key) .update('input') .final() - .then(function(result) { + .then(result => { // {result} is a String -- JWS using the Compact Serialization }); jose.JWS.createSign({ alg: 'PS256' }, key) .update('input') .final() - .then(function(result) { + .then(result => { // .... }); jose.JWS.createSign({ fields: { cty: 'jwk+json' } }, key) .update('input') .final() - .then(function(result) { + .then(result => { // .... }); jose.JWS.createSign(key) .update('input', 'utf8') .final() - .then(function(result) { + .then(result => { // .... }); @@ -143,7 +119,7 @@ jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { }; jose.JWS.createVerify(key, opts) .verify('input') - .then(function(result) { + .then(result => { // ... }); @@ -152,7 +128,7 @@ jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { }; jose.JWS.createVerify(key, opts) .verify('input') - .then(function(result) { + .then(result => { // ... }); @@ -164,55 +140,55 @@ jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { jose.JWS.createVerify(key, opts2) .verify('input') - .then(function(result) { + .then(result => { // ... }); jose.JWE.createEncrypt(key) .update('input') .final() - .then(function(result) { + .then(result => { // {result} is a JSON Object -- JWE using the JSON General Serialization }); jose.JWE.createEncrypt({ format: 'compact' }, key) .update('input') .final() - .then(function(result) { + .then(result => { // {result} is a String -- JWE using the Compact Serialization }); jose.JWE.createEncrypt({ format: 'flattened' }, key) .update('input') .final() - .then(function(result) { + .then(result => { // {result} is a JSON Object -- JWE using the JSON Flattened Serialization }); jose.JWE.createEncrypt({ zip: true }, key) .update('input') .final() - .then(function(result) { + .then(result => { // .... }); jose.JWE.createEncrypt({ fields: { cty: 'jwk+json' } }, key) .update('input') .final() - .then(function(result) { + .then(result => { // .... }); jose.JWE.createEncrypt([key, key]) .update('input') .final() - .then(function(result) { + .then(result => { // .... }); jose.JWE.createDecrypt(key) .decrypt('input') - .then(function(result) { + .then(result => { // .... }); @@ -221,7 +197,7 @@ jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { }; jose.JWE.createDecrypt(key, opts3) .decrypt('input') - .then(function(result) { + .then(result => { // ... }); @@ -230,7 +206,7 @@ jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { }; jose.JWS.createVerify(key, opts4) .verify('input') - .then(function(result) { + .then(result => { // ... }); @@ -241,14 +217,14 @@ jose.JWK.createKey('oct', 256, { alg: 'A256GCM' }).then(function(result) { }; jose.JWE.createDecrypt(key, opts5) .decrypt('input') - .then(function(result) { + .then(result => { // ... }); }); jose.JWS.createVerify(keystore) .verify('input') - .then(function(result) { + .then(result => { // {result} is a Object with: // * header: the combined 'protected' and 'unprotected' header members // * payload: Buffer of the signed content @@ -261,25 +237,25 @@ jose.JWS.createVerify(keystore) // * JSON object representing a JWK jose.JWS.createVerify(key) .verify('input') - .then(function(result) { + .then(result => { // ... }); jose.JWS.createVerify() .verify('input', { allowEmbeddedKey: true }) - .then(function(result) { + .then(result => { // ... }); -let verifier = jose.JWS.createVerify({ allowEmbeddedKey: true }); +const verifier = jose.JWS.createVerify({ allowEmbeddedKey: true }); -verifier.verify('input').then(function(result) { +verifier.verify('input').then(result => { // ... }); jose.JWE.createDecrypt(keystore) .decrypt('input') - .then(function(result) { + .then(result => { // {result} is a Object with: // * header: the combined 'protected' and 'unprotected' header members // * protected: an array of the member names from the "protected" member From 1d48f565ea940f2e2700fad66525625e363349bb Mon Sep 17 00:00:00 2001 From: Nadun Indunil Date: Thu, 14 Feb 2019 12:43:59 +0530 Subject: [PATCH 4/5] add: PascalCase --- types/node-jose/index.d.ts | 46 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/types/node-jose/index.d.ts b/types/node-jose/index.d.ts index b27ecde774..92e312352f 100644 --- a/types/node-jose/index.d.ts +++ b/types/node-jose/index.d.ts @@ -9,7 +9,7 @@ export function canYouSee(ks: JWK.Key | JWK.KeyStore, opts: object): JWS.Verifier; export namespace JWA { - interface decryptEncryptOptions { + interface DecryptEncryptOptions { aad?: Buffer; adata?: Buffer; iv?: Buffer; @@ -27,7 +27,7 @@ export namespace JWA { p2c?: number; // used in pbes } - interface deriveOptions { + interface DeriveOptions { length?: number; // key length otherInfo?: Buffer; // info used in concatkdf public?: Buffer; // public key used in ecdh @@ -36,21 +36,21 @@ export namespace JWA { info?: Buffer; // app identifier info used in hkdf } - interface encryptReturn { + interface EncryptReturn { data: Buffer; // The cipher text tag?: Buffer; // The tag used in some algorithms } - interface signReturn { + interface SignReturn { data: Buffer; // the data passed into the sign function mac: Buffer; // the signature for `data` } - interface signVerifyOptions { + interface SignVerifyOptions { loose?: boolean; } - interface verifyReturn { + interface VerifyReturn { data: Buffer; // the data passed into the verify function mac: Buffer; // the signature for `data` valid: boolean; // whether the signature matches the data @@ -60,10 +60,10 @@ export namespace JWA { alg: string, key: string | Buffer, cdata: string | Buffer, - props?: decryptEncryptOptions + props?: DecryptEncryptOptions ): Promise; - function derive(alg: string, key: string | Buffer, props?: deriveOptions): Promise; + function derive(alg: string, key: string | Buffer, props?: DeriveOptions): Promise; function digest(alg: string, data: string | Buffer, props?: any): Promise; @@ -71,23 +71,23 @@ export namespace JWA { alg: string, key: string | Buffer, pdata: string | Buffer, - props?: decryptEncryptOptions - ): Promise; + props?: DecryptEncryptOptions + ): Promise; function sign( alg: string, key: string | Buffer, pdata: string | Buffer, - props: signVerifyOptions - ): Promise; + props: SignVerifyOptions + ): Promise; function verify( alg: string, key: string | Buffer, pdata: string | Buffer, mac: string | Buffer, - props: signVerifyOptions - ): Promise; + props: SignVerifyOptions + ): Promise; } export namespace JWE { @@ -254,13 +254,13 @@ export namespace JWS { opts?: { allowEmbeddedKey?: boolean; algorithms?: string[]; handlers?: any } ): Verifier; - interface createSignResult { + interface CreateSignResult { signResult: object; } interface Signer { update(input: Buffer | string, encoding?: string): this; - final(): Promise; + final(): Promise; } interface BaseResult { @@ -290,18 +290,18 @@ export namespace JWS { verify(input: string, opts?: { allowEmbeddedKey?: boolean }): Promise; } - interface exp { + interface Exp { complete(jws: any): any; } - interface verifyOptions { + interface VerifyOptions { allowEmbeddedKey?: boolean; algorithms?: string[]; - handlers: { exp: boolean | exp }; + handlers: { exp: boolean | Exp }; } } -interface parseReturn { +interface ParseReturn { type: 'JWS' | 'JWE'; format: 'compact' | 'json'; input: Buffer | string | object; @@ -309,12 +309,12 @@ interface parseReturn { perform: (ks: JWK.KeyStore) => Promise | Promise; } -export function parse(input: Buffer | string | object): parseReturn; +export function parse(input: Buffer | string | object): ParseReturn; export namespace parse { - function compact(input: Buffer | string | object): parseReturn; + function compact(input: Buffer | string | object): ParseReturn; - function json(input: Buffer | string | object): parseReturn; + function json(input: Buffer | string | object): ParseReturn; } export namespace util { From 2e848e713fd20321efa645e8b55d79d631591864 Mon Sep 17 00:00:00 2001 From: Nadun Indunil Date: Fri, 15 Feb 2019 23:06:17 +0530 Subject: [PATCH 5/5] fix: default export --- types/node-jose/index.d.ts | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/types/node-jose/index.d.ts b/types/node-jose/index.d.ts index 92e312352f..dc4caaeca5 100644 --- a/types/node-jose/index.d.ts +++ b/types/node-jose/index.d.ts @@ -301,17 +301,16 @@ export namespace JWS { } } -interface ParseReturn { - type: 'JWS' | 'JWE'; - format: 'compact' | 'json'; - input: Buffer | string | object; - header: object; - perform: (ks: JWK.KeyStore) => Promise | Promise; -} - -export function parse(input: Buffer | string | object): ParseReturn; +export function parse(input: Buffer | string | object): parse.ParseReturn; export namespace parse { + interface ParseReturn { + type: 'JWS' | 'JWE'; + format: 'compact' | 'json'; + input: Buffer | string | object; + header: object; + perform: (ks: JWK.KeyStore) => Promise | Promise; + } function compact(input: Buffer | string | object): ParseReturn; function json(input: Buffer | string | object): ParseReturn; @@ -334,13 +333,3 @@ export namespace util { function encode(input: string): string; } } - -declare const _default: { - JWA: typeof JWA; - JWE: typeof JWE; - JWS: typeof JWS; - JWK: typeof JWK; - parse: typeof parse; - util: typeof util; -}; -export default _default;