From 3948f6e1a70ad29c17215259bf81bb077c25a715 Mon Sep 17 00:00:00 2001 From: Andrew Barba Date: Fri, 30 Sep 2016 11:30:07 -0400 Subject: [PATCH] fix singing token implementation --- .gitignore | 1 + README.md | 2 ++ lib/apns.js | 7 +++- package.json | 2 +- test/test.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 105 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7b37fde4..e5f63f17 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules *.env *.log *.pem +*.p8 diff --git a/README.md b/README.md index 8d6ed9db..5e772731 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,9 @@ Create an APNS client using a signing key: const APNS = require('apns2'); let client = new APNS({ + defaultTopic: `com.tablelist.Tablelist`, team: `TFLP87PW54`, + keyId: `123ABC456`, signingKey: fs.readFileSync(`${__dirname}/path/to/auth.p8`) }); ``` diff --git a/lib/apns.js b/lib/apns.js index 3e7876e5..cdd5b8a8 100644 --- a/lib/apns.js +++ b/lib/apns.js @@ -104,6 +104,7 @@ class APNS extends EventEmitter { this._pfx = options.pfx; this._passphrase = options.passphrase; this._signingKey = options.signingKey; + this._keyId = options.keyId; this._defaultTopic = options.defaultTopic; this._concurrency = options.concurrency || CONCURRENCY; this._client = new HTTP2Client(options.host || HOST, options.port || PORT); @@ -228,7 +229,11 @@ class APNS extends EventEmitter { let key = this._signingKey; let options = { - algorithm: SIGNING_ALGORITHM + algorithm: SIGNING_ALGORITHM, + header: { + alg: SIGNING_ALGORITHM, + kid: this._keyId + } }; let token; diff --git a/package.json b/package.json index 6c696e36..41f24609 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apns2", - "version": "1.1.0", + "version": "1.2.0", "description": "Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.", "author": "Andrew Barba ", "main": "lib/apns.js", diff --git a/test/test.js b/test/test.js index 8271eb3a..98d417d1 100644 --- a/test/test.js +++ b/test/test.js @@ -35,7 +35,7 @@ describe('http2', () => { describe('apns', () => { - let deviceToken = `570e137a42cb2782527a52fe0b5a9fc8b3e63d3249c505ad15e89bdef6d5c434`; + let deviceToken = `5ab4be4b2e511acfc64405be02a9544295f29b6157b75e3fbc5b2f5300eeda45`; describe('certs', () => { @@ -228,6 +228,99 @@ describe('apns', () => { }); describe('signing token', () => { - //TODO - add tests for signing token + + let apns; + + before(() => { + apns = new APNS({ + defaultTopic: `com.tablelist.Tablelist`, + team: `TFLP87PW54`, + keyId: `74QQRV9RW2`, + signingKey: fs.readFileSync(`${__dirname}/certs/token.p8`) + }); + }); + + it('should send a basic notification', () => { + let basicNotification = new APNS.BasicNotification(deviceToken, `Hello, Basic`); + return apns.send(basicNotification); + }); + + it('should send a basic notification with options', () => { + let basicNotification = new APNS.BasicNotification(deviceToken, `Hello, 1`, { + badge: 1 + }); + return apns.send(basicNotification); + }); + + it('should send a basic notification with additional data', () => { + let basicNotification = new APNS.BasicNotification(deviceToken, `Hello, ICON`, { + badge: 0, + data: { + url: `venue/icon` + } + }); + return apns.send(basicNotification); + }); + + it('should send a silent notification', () => { + let silentNotification = new APNS.SilentNotification(deviceToken); + return apns.send(silentNotification); + }); + + it('should send a notification', () => { + let notification = new APNS.Notification(deviceToken, { + aps: { + alert: { + body: `Hello, Tablelist` + } + } + }); + return apns.send(notification); + }); + + it('should send both notifications', () => { + let basicNotification = new APNS.BasicNotification(deviceToken, `Hello, Multiple`); + let silentNotification = new APNS.SilentNotification(deviceToken); + return apns.send([basicNotification, silentNotification]).then(result => { + should.exist(result); + result.length.should.equal(2); + }); + }); + + it('should fail to send a notification', () => { + let noti = new APNS.BasicNotification(`fakedevicetoken`, `Hello, bad token`); + return apns.send(noti).catch(err => { + should.exist(err); + err.reason.should.equal(errors.badDeviceToken); + }); + }); + + it('should fail to send a notification and emit an error', done => { + + apns.once(errors.error, err => { + should.exist(err); + err.reason.should.equal(errors.badDeviceToken); + done(); + }); + + let noti = new APNS.BasicNotification(`fakedevicetoken`, `Hello, bad token`); + apns.send(noti).catch(err => { + should.exist(err); + }); + }); + + it('should fail to send a notification and emit an error', done => { + + apns.once(errors.badDeviceToken, err => { + should.exist(err); + err.reason.should.equal(errors.badDeviceToken); + done(); + }); + + let noti = new APNS.BasicNotification(`fakedevicetoken`, `Hello, bad token`); + apns.send(noti).catch(err => { + should.exist(err); + }); + }); }); });