Comparing version 3.0.1 to 3.1.0
@@ -6,2 +6,6 @@ const { createSecretKey } = require('crypto') | ||
module.exports = function checkKey(header, key) { | ||
if (typeof key === 'string' && key.startsWith(`k${header.substr(1)}`)) { | ||
key = Buffer.from(key.substr(header.length), 'base64url') | ||
} | ||
if (!isKeyObject(key)) { | ||
@@ -8,0 +12,0 @@ try { |
@@ -9,8 +9,24 @@ const crypto = require('crypto') | ||
async function generateKey(purpose) { | ||
async function generateKey(purpose, { format = 'keyobject' } = {}) { | ||
if (format !== 'keyobject' && format !== 'paserk') throw new TypeError('invalid format') | ||
switch (purpose) { | ||
case 'local': | ||
return generateSecretKey('aes', { length: 256 }) | ||
case 'local': { | ||
const keyobject = await generateSecretKey('aes', { length: 256 }) | ||
if (format === 'paserk') { | ||
return `k1.local.${keyobject.export().toString('base64url')}` | ||
} | ||
return keyobject | ||
} | ||
case 'public': { | ||
const { privateKey } = await generateKeyPair('rsa', { modulusLength: 2048 }) | ||
const { privateKey, publicKey } = await generateKeyPair('rsa', { modulusLength: 2048 }) | ||
if (format === 'paserk') { | ||
return { | ||
secretKey: `k1.secret.${privateKey | ||
.export({ format: 'der', type: 'pkcs1' }) | ||
.toString('base64url')}`, | ||
publicKey: `k1.public.${publicKey | ||
.export({ format: 'der', type: 'pkcs1' }) | ||
.toString('base64url')}`, | ||
} | ||
} | ||
return privateKey | ||
@@ -17,0 +33,0 @@ } |
@@ -12,2 +12,9 @@ const { | ||
function checkKey(key) { | ||
if (typeof key === 'string' && key.startsWith('k1.secret.')) { | ||
try { | ||
const der = Buffer.from(key.substr(10), 'base64url') | ||
key = { key: der, format: 'der', type: 'pkcs1' } | ||
} catch {} | ||
} | ||
if (!isKeyObject(key)) { | ||
@@ -14,0 +21,0 @@ try { |
@@ -11,2 +11,9 @@ const { | ||
function checkKey(key) { | ||
if (typeof key === 'string' && key.startsWith('k1.public.')) { | ||
try { | ||
const der = Buffer.from(key.substr(10), 'base64url') | ||
key = { key: der, format: 'der', type: 'pkcs1' } | ||
} catch {} | ||
} | ||
if (!isKeyObject(key) || key.type === 'private') { | ||
@@ -13,0 +20,0 @@ try { |
@@ -0,1 +1,2 @@ | ||
const assert = require('assert') | ||
const crypto = require('crypto') | ||
@@ -10,2 +11,9 @@ const { promisify } = require('util') | ||
function _checkPrivateKey(v, key) { | ||
if (typeof key === 'string' && key.startsWith(`k${v.substr(1)}.secret.`)) { | ||
try { | ||
key = Buffer.from(key.substr(10), 'base64url') | ||
assert.strictEqual(key.byteLength, 64) | ||
} catch {} | ||
} | ||
if (Buffer.isBuffer(key)) { | ||
@@ -35,2 +43,9 @@ try { | ||
function _checkPublicKey(v, key) { | ||
if (typeof key === 'string' && key.startsWith(`k${v.substr(1)}.public.`)) { | ||
try { | ||
key = Buffer.from(key.substr(10), 'base64url') | ||
assert.strictEqual(key.byteLength, 32) | ||
} catch {} | ||
} | ||
if (Buffer.isBuffer(key)) { | ||
@@ -59,6 +74,13 @@ try { | ||
async function _generateKey(v, purpose) { | ||
async function _generateKey(v, purpose, { format = 'keyobject' } = {}) { | ||
if (format !== 'keyobject' && format !== 'paserk') throw new TypeError('invalid format') | ||
switch (purpose) { | ||
case 'public': { | ||
const { privateKey } = await generateKeyPair('ed25519') | ||
const { privateKey, publicKey } = await generateKeyPair('ed25519') | ||
if (format === 'paserk') { | ||
return { | ||
secretKey: `k${v.substr(1)}.secret.${keyObjectToBytes(privateKey).toString('base64url')}`, | ||
publicKey: `k${v.substr(1)}.public.${keyObjectToBytes(publicKey).toString('base64url')}`, | ||
} | ||
} | ||
return privateKey | ||
@@ -65,0 +87,0 @@ } |
@@ -11,8 +11,20 @@ const crypto = require('crypto') | ||
async function generateKey(purpose) { | ||
async function generateKey(purpose, { format = 'keyobject' } = {}) { | ||
if (format !== 'keyobject' && format !== 'paserk') throw new TypeError('invalid format') | ||
switch (purpose) { | ||
case 'local': | ||
return generateSecretKey('aes', { length: 256 }) | ||
case 'local': { | ||
const keyobject = await generateSecretKey('aes', { length: 256 }) | ||
if (format === 'paserk') { | ||
return `k3.local.${keyobject.export().toString('base64url')}` | ||
} | ||
return keyobject | ||
} | ||
case 'public': { | ||
const { privateKey } = await generateKeyPair('ec', { namedCurve: 'P-384' }) | ||
const { privateKey, publicKey } = await generateKeyPair('ec', { namedCurve: 'P-384' }) | ||
if (format === 'paserk') { | ||
return { | ||
secretKey: `k3.secret.${keyObjectToBytes(privateKey).toString('base64url')}`, | ||
publicKey: `k3.public.${keyObjectToBytes(publicKey).toString('base64url')}`, | ||
} | ||
} | ||
return privateKey | ||
@@ -19,0 +31,0 @@ } |
@@ -12,2 +12,9 @@ const { createPrivateKey } = require('crypto') | ||
function checkKey(key) { | ||
if (typeof key === 'string' && key.startsWith('k3.secret.')) { | ||
try { | ||
key = Buffer.from(key.substr(10), 'base64url') | ||
assert.strictEqual(key.byteLength, 48) | ||
} catch {} | ||
} | ||
if (Buffer.isBuffer(key)) { | ||
@@ -14,0 +21,0 @@ try { |
@@ -11,2 +11,9 @@ const { createPublicKey } = require('crypto') | ||
function checkKey(key) { | ||
if (typeof key === 'string' && key.startsWith('k3.public.')) { | ||
try { | ||
key = Buffer.from(key.substr(10), 'base64url') | ||
assert.strictEqual(key.byteLength, 49) | ||
} catch {} | ||
} | ||
if (Buffer.isBuffer(key)) { | ||
@@ -13,0 +20,0 @@ try { |
{ | ||
"name": "paseto", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "PASETO for Node.js with no dependencies", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -64,3 +64,3 @@ /// <reference types="node" /> | ||
payload: object | Buffer, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ProduceOptions, 'assertion'>, | ||
@@ -70,3 +70,3 @@ ): Promise<string> | ||
payload: object | Buffer, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: Omit<ProduceOptions, 'assertion'>, | ||
@@ -76,3 +76,3 @@ ): Promise<string> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptions<false>, 'assertion'>, | ||
@@ -82,3 +82,3 @@ ): Promise<object> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptions<true>, 'assertion'>, | ||
@@ -88,3 +88,3 @@ ): Promise<CompleteResult> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptionsBuffer<false>, 'assertion'>, | ||
@@ -94,3 +94,3 @@ ): Promise<Buffer> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptionsBuffer<true>, 'assertion'>, | ||
@@ -100,3 +100,3 @@ ): Promise<CompleteResultBuffer> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: Omit<ConsumeOptions<false>, 'assertion'>, | ||
@@ -106,3 +106,3 @@ ): Promise<object> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: Omit<ConsumeOptions<true>, 'assertion'>, | ||
@@ -112,3 +112,3 @@ ): Promise<CompleteResult> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: Omit<ConsumeOptionsBuffer<false>, 'assertion'>, | ||
@@ -118,6 +118,9 @@ ): Promise<Buffer> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: Omit<ConsumeOptionsBuffer<true>, 'assertion'>, | ||
): Promise<CompleteResultBuffer> | ||
function generateKey(purpose: 'local' | 'public'): Promise<KeyObject> | ||
function generateKey(purpose: 'local' | 'public', options: { format: 'keyobject' }): Promise<KeyObject> | ||
function generateKey(purpose: 'local', options: { format: 'paserk' }): Promise<string> | ||
function generateKey(purpose: 'public', options: { format: 'paserk' }): Promise<{ secretKey: string, publicKey: string }> | ||
} | ||
@@ -127,3 +130,3 @@ export namespace V2 { | ||
payload: object | Buffer, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ProduceOptions, 'assertion'>, | ||
@@ -133,3 +136,3 @@ ): Promise<string> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptions<false>, 'assertion'>, | ||
@@ -139,3 +142,3 @@ ): Promise<object> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptions<true>, 'assertion'>, | ||
@@ -145,3 +148,3 @@ ): Promise<CompleteResult> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptionsBuffer<false>, 'assertion'>, | ||
@@ -151,6 +154,8 @@ ): Promise<Buffer> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: Omit<ConsumeOptionsBuffer<true>, 'assertion'>, | ||
): Promise<CompleteResultBuffer> | ||
function generateKey(purpose: 'public'): Promise<KeyObject> | ||
function generateKey(purpose: 'public', options: { format: 'keyobject' }): Promise<KeyObject> | ||
function generateKey(purpose: 'public', options: { format: 'paserk' }): Promise<{ secretKey: string, publicKey: string }> | ||
function bytesToKeyObject(bytes: Buffer): KeyObject | ||
@@ -162,3 +167,3 @@ function keyObjectToBytes(keyObject: KeyObject): Buffer | ||
payload: object | Buffer, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput | string, | ||
options?: ProduceOptions, | ||
@@ -168,3 +173,3 @@ ): Promise<string> | ||
payload: object | Buffer, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: ProduceOptions, | ||
@@ -174,3 +179,3 @@ ): Promise<string> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptions<false>, | ||
@@ -180,3 +185,3 @@ ): Promise<object> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptions<true>, | ||
@@ -186,3 +191,3 @@ ): Promise<CompleteResult> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptionsBuffer<false>, | ||
@@ -192,3 +197,3 @@ ): Promise<Buffer> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptionsBuffer<true>, | ||
@@ -198,3 +203,3 @@ ): Promise<CompleteResultBuffer> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: ConsumeOptions<false>, | ||
@@ -204,3 +209,3 @@ ): Promise<object> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: ConsumeOptions<true>, | ||
@@ -210,3 +215,3 @@ ): Promise<CompleteResult> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: ConsumeOptionsBuffer<false>, | ||
@@ -216,6 +221,9 @@ ): Promise<Buffer> | ||
token: string, | ||
key: KeyObject | Buffer, | ||
key: KeyObject | Buffer | string, | ||
options?: ConsumeOptionsBuffer<true>, | ||
): Promise<CompleteResultBuffer> | ||
function generateKey(purpose: 'local' | 'public'): Promise<KeyObject> | ||
function generateKey(purpose: 'local' | 'public', options: { format: 'keyobject' }): Promise<KeyObject> | ||
function generateKey(purpose: 'local', options: { format: 'paserk' }): Promise<string> | ||
function generateKey(purpose: 'public', options: { format: 'paserk' }): Promise<{ secretKey: string, publicKey: string }> | ||
function bytesToKeyObject(bytes: Buffer): KeyObject | ||
@@ -227,3 +235,3 @@ function keyObjectToBytes(keyObject: KeyObject): Buffer | ||
payload: object | Buffer, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PrivateKeyInput | JsonWebKeyInput | string, | ||
options?: ProduceOptions, | ||
@@ -233,3 +241,3 @@ ): Promise<string> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptions<false>, | ||
@@ -239,3 +247,3 @@ ): Promise<object> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptions<true>, | ||
@@ -245,3 +253,3 @@ ): Promise<CompleteResult> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptionsBuffer<false>, | ||
@@ -251,6 +259,8 @@ ): Promise<Buffer> | ||
token: string, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput, | ||
key: KeyObject | Buffer | PublicKeyInput | JsonWebKeyInput | string, | ||
options?: ConsumeOptionsBuffer<true>, | ||
): Promise<CompleteResultBuffer> | ||
function generateKey(purpose: 'public'): Promise<KeyObject> | ||
function generateKey(purpose: 'public', options: { format: 'keyobject' }): Promise<KeyObject> | ||
function generateKey(purpose: 'public', options: { format: 'paserk' }): Promise<{ secretKey: string, publicKey: string }> | ||
function bytesToKeyObject(bytes: Buffer): KeyObject | ||
@@ -257,0 +267,0 @@ function keyObjectToBytes(keyObject: KeyObject): Buffer |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53101
1442