Skip to content

Commit

Permalink
fix singing token implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Sep 30, 2016
1 parent 3f038ea commit 3948f6e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
*.env
*.log
*.pem
*.p8
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
});
```
Expand Down
7 changes: 6 additions & 1 deletion lib/apns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
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": "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 <[email protected]>",
"main": "lib/apns.js",
Expand Down
97 changes: 95 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('http2', () => {

describe('apns', () => {

let deviceToken = `570e137a42cb2782527a52fe0b5a9fc8b3e63d3249c505ad15e89bdef6d5c434`;
let deviceToken = `5ab4be4b2e511acfc64405be02a9544295f29b6157b75e3fbc5b2f5300eeda45`;

describe('certs', () => {

Expand Down Expand Up @@ -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);
});
});
});
});

0 comments on commit 3948f6e

Please sign in to comment.