Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
davesag committed Dec 4, 2018
2 parents 196f31e + 7c5af94 commit 90a9277
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ Works fine under Node 11.3+, and might run under versions of node going back to
})
})

### Options

See [encrypt.js#L661](https://github.com/cisco/node-jose/blob/master/lib/jwe/encrypt.js#L661)

You can add `encrypt` options as follows:

const { encrypt, decrypt } = jose(privateKey, publicKey, {
format: 'compact'
protect: true,
// or any of the encrypt options than can be passed to JWE.createEncrypt.
// https://github.com/cisco/node-jose/blob/master/lib/jwe/encrypt.js#L661
})

## Issues

Cisco's [node-jose](https://github.com/cisco/node-jose/issues) library has issues with **private keys with a passphrase** and cypher set. See [add support for passphrase in pem certificate](https://github.com/cisco/node-jose/issues/234).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jose-simple",
"version": "1.1.0",
"version": "1.2.0",
"description": "A very simple JOSE encryption/decription utility",
"engines": {
"node": ">= 10.14.1",
Expand Down
4 changes: 2 additions & 2 deletions src/utils/jose.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { JWE } = require('node-jose')
const { encode, decode } = require('./base64')

const jose = (privateKey, publicKey) => {
const jose = (privateKey, publicKey, options = {}) => {
const encrypt = async raw => {
if (!raw) throw new Error('Missing raw data.')
const buffer = Buffer.from(JSON.stringify(raw))
const encrypted = await JWE.createEncrypt(publicKey)
const encrypted = await JWE.createEncrypt(options, publicKey)
.update(buffer)
.final()
return encode(encrypted)
Expand Down
75 changes: 47 additions & 28 deletions test/unit/utils/jose_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ const jose = require('../../../src/utils/jose')
const makeKey = async pem => JWK.asKey(pem, 'pem')

describe('jose', () => {
let privateKey
let publicKey
let j

before(async () => {
const keys = await keygen()
const privateKey = await makeKey(keys.privateKey)
const publicKey = await makeKey(keys.publicKey)
j = jose(privateKey, publicKey)
privateKey = await makeKey(keys.privateKey)
publicKey = await makeKey(keys.publicKey)
})

context('happy path', () => {
const doTests = () => {
const raw = {
iss: 'test',
exp: faker.date.future().getTime(),
Expand All @@ -26,40 +27,58 @@ describe('jose', () => {
}
}

let encrypted
let decrypted
context('happy path', () => {
let encrypted
let decrypted

before(async () => {
encrypted = await j.encrypt(raw)
decrypted = await j.decrypt(encrypted)
})
before(async () => {
encrypted = await j.encrypt(raw)
decrypted = await j.decrypt(encrypted)
})

it('encrypted', () => {
expect(encrypted).to.exist
expect(encrypted).to.be.a('string')
})

it('encrypted', () => {
expect(encrypted).to.exist
expect(encrypted).to.be.a('string')
it('decrypted', () => {
expect(decrypted).to.exist
expect(decrypted).to.be.an('object')
})

it('decrypted version of encrypted is raw', () => {
expect(decrypted).to.eql(raw)
})
})

it('decrypted', () => {
expect(decrypted).to.exist
expect(decrypted).to.be.an('object')
context('unhappy path', () => {
describe('encrypt', () => {
context('given no input', () => {
it('rejects', () => expect(j.encrypt()).to.be.rejected)
})
})

describe('decrypt', () => {
context('given no input', () => {
it('rejects', () => expect(j.decrypt()).to.be.rejected)
})
})
})
}

it('decrypted version of encrypted is raw', () => {
expect(decrypted).to.eql(raw)
context('without options', () => {
before(() => {
j = jose(privateKey, publicKey)
})

doTests()
})

context('unhappy path', () => {
describe('encrypt', () => {
context('given no input', () => {
it('rejects', () => expect(j.encrypt()).to.be.rejected)
})
context('with options', () => {
before(() => {
j = jose(privateKey, publicKey, { format: 'compact' })
})

describe('decrypt', () => {
context('given no input', () => {
it('rejects', () => expect(j.decrypt()).to.be.rejected)
})
})
doTests()
})
})

0 comments on commit 90a9277

Please sign in to comment.