Skip to content

Commit 95d0407

Browse files
abisalehalliprasanoscargrpheykevin
authored
2.0.1 (#67)
* Coveralls Badge Fix * Coveralls Badge Fix:Final * Coveralls Badge Fix : istanbul package added * Coveralls Badge Fix + snyk added * Coveralls Badge Fix + snyk added * Snyk removed from Makefile * Snyk removed for timebeing * Add more code coverage for OAuthClient (#54) * Add better code coverage in OAuthClient * Fix: ValidateIdToken method and unit tests (#58) * Fix validateIdToken tests * Pointing README Badge to Develop * Pointing README Badge to Develop * Update Develop Coverage Badge * README.md corrections * Version Bump Co-authored-by: Oscar Rabasa <[email protected]> Co-authored-by: Kevin Tang <[email protected]>
1 parent 5ab8df4 commit 95d0407

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[![SDK Banner](views/SDK.png)][ss1]
22

33

4-
[![Build Status](https://travis-ci.org/intuit/oauth-jsclient.svg?branch=master)](https://travis-ci.org/intuit/oauth-jsclient)
4+
[![Build Status](https://travis-ci.org/intuit/oauth-jsclient.svg?branch=develop)](https://travis-ci.org/intuit/oauth-jsclient?branch=develop)
55
[![NPM Package Version](https://img.shields.io/npm/v/intuit-oauth.svg?style=flat-square)](https://www.npmjs.com/package/intuit-oauth)
6-
[![Coverage Status](https://coveralls.io/repos/github/intuit/oauth-jsclient/badge.svg)](https://coveralls.io/github/intuit/oauth-jsclient)
6+
[![Coverage Status](https://coveralls.io/repos/github/intuit/oauth-jsclient/badge.svg?branch=develop)](https://coveralls.io/github/intuit/oauth-jsclient?branch=develop)
77
[![Known Vulnerabilities](https://snyk.io/test/github/intuit/oauth-jsclient/badge.svg)](https://snyk.io/test/github/intuit/oauth-jsclient)
88

99

@@ -129,7 +129,7 @@ The available scopes include :
129129

130130
OpenID Scopes :
131131

132-
* `openid` - for openID assertion include `OAuthClient.scopes.Openid`
132+
* `openid` - for openID assertion include `OAuthClient.scopes.OpenId`
133133
* `profile` - for profile assertion include `OAuthClient.scopes.Profile`
134134
* `email` - for email assertion include `OAuthClient.scopes.Email`
135135
* `phone` - for phone assertion include `OAuthClient.scopes.Phone`
@@ -197,7 +197,7 @@ Access tokens are valid for 3600 seconds (one hour), after which time you need t
197197
198198
oauthClient.refresh()
199199
.then(function(authResponse) {
200-
console.log('Tokens refreshed : ' + JSON.stringify(authResponse.json()));
200+
console.log('Tokens refreshed : ' + JSON.stringify(authResponse.getJson()));
201201
})
202202
.catch(function(e) {
203203
console.error("The error message is :"+e.originalMessage);
@@ -214,7 +214,7 @@ You can call the below helper method to refresh tokens by explictly passing the
214214
215215
oauthClient.refreshUsingToken('<Enter the refresh token>')
216216
.then(function(authResponse) {
217-
console.log('Tokens refreshed : ' + JSON.stringify(authResponse.json()));
217+
console.log('Tokens refreshed : ' + JSON.stringify(authResponse.getJson()));
218218
})
219219
.catch(function(e) {
220220
console.error("The error message is :"+e.originalMessage);
@@ -231,7 +231,7 @@ When you no longer need the access_token, you could use the below helper method
231231
232232
oauthClient.revoke()
233233
.then(function(authResponse) {
234-
console.log('Tokens revoked : ' + JSON.stringify(authResponse.json()));
234+
console.log('Tokens revoked : ' + JSON.stringify(authResponse.getJson()));
235235
})
236236
.catch(function(e) {
237237
console.error("The error message is :"+e.originalMessage);
@@ -244,7 +244,7 @@ Alternatively you can also pass `access_token` or `refresh_token` to this helper
244244
245245
oauthClient.revoke(params)
246246
.then(function(authResponse) {
247-
console.log('Tokens revoked : ' + JSON.stringify(authResponse.json()));
247+
console.log('Tokens revoked : ' + JSON.stringify(authResponse.getJson()));
248248
})
249249
.catch(function(e) {
250250
console.error("The error message is :"+e.originalMessage);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "intuit-oauth",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Intuit Node.js client for OAuth2.0 and OpenIDConnect",
55
"main": "./src/OAuthClient.js",
66
"scripts": {
@@ -68,8 +68,8 @@
6868
},
6969
"homepage": "https://github.com/intuit/oauth-jsclient",
7070
"dependencies": {
71-
"csrf": "^3.0.4",
7271
"atob": "2.1.2",
72+
"csrf": "^3.0.4",
7373
"es6-promise": "^4.2.5",
7474
"events": "^3.0.0",
7575
"idtoken-verifier": "^1.2.0",
@@ -83,6 +83,7 @@
8383
},
8484
"devDependencies": {
8585
"body-parser": "^1.15.2",
86+
"btoa": "^1.2.1",
8687
"chai": "^4.1.2",
8788
"chai-as-promised": "^7.1.1",
8889
"chance": "^1.1.3",

src/OAuthClient.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,8 @@ OAuthClient.prototype.validateIdToken = function validateIdToken(params = {}) {
470470
// Step 1 : First check if the issuer is as mentioned in "issuer"
471471
if (id_token_payload.iss !== 'https://oauth.platform.intuit.com/op/v1') return false;
472472

473-
// Step 2 : check if the aud field in idToken is same as application's clientId
474-
if (id_token_payload.aud !== this.clientId) return false;
473+
// Step 2 : check if the aud field in idToken contains application's clientId
474+
if (!id_token_payload.aud.find(audience => (audience === this.clientId))) return false;
475475

476476
// Step 3 : ensure the timestamp has not elapsed
477477
if (id_token_payload.exp < Date.now() / 1000) return false;
@@ -507,8 +507,7 @@ OAuthClient.prototype.getKeyFromJWKsURI = function getKeyFromJWKsURI(id_token, k
507507
return (new Promise(((resolve) => {
508508
resolve(this.loadResponse(request));
509509
}))).then((response) => {
510-
if (response.status !== '200') throw new Error('Could not reach JWK endpoint');
511-
510+
if (Number(response.status) !== 200) throw new Error('Could not reach JWK endpoint');
512511
// Find the key by KID
513512
const responseBody = JSON.parse(response.body);
514513
const key = responseBody.keys.find(el => (el.kid === kid));

test/OAuthClientTest.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const nock = require('nock');
1111
const sinon = require('sinon');
1212
const chai = require('chai');
1313
const chaiAsPromised = require('chai-as-promised');
14+
const btoa = require('btoa');
15+
const jwt = require('jsonwebtoken');
1416

1517
// eslint-disable-next-line no-unused-vars
1618
const getPem = require('rsa-pem-from-mod-exp');
@@ -23,11 +25,14 @@ const expectedTokenResponse = require('./mocks/tokenResponse.json');
2325
const expectedUserInfo = require('./mocks/userInfo.json');
2426
const expectedMakeAPICall = require('./mocks/makeAPICallResponse.json');
2527
const expectedjwkResponseCall = require('./mocks/jwkResponse.json');
26-
const expectedvalidateIdToken = require('./mocks/validateIdToken.json');
2728
const expectedOpenIDToken = require('./mocks/openID-token.json');
2829
// var expectedErrorResponse = require('./mocks/errorResponse.json');
2930
const expectedMigrationResponse = require('./mocks/authResponse.json');
3031

32+
require.cache[require.resolve('rsa-pem-from-mod-exp')] = {
33+
exports: sinon.stub().returns(3),
34+
};
35+
3136
const oauthClient = new OAuthClientTest({
3237
clientId: 'clientID',
3338
clientSecret: 'clientSecret',
@@ -262,7 +267,7 @@ describe('Tests for OAuthClient', () => {
262267
});
263268

264269
// make API Call
265-
describe('Make API Call ', () => {
270+
describe('Make API Call', () => {
266271
before(() => {
267272
nock('https://sandbox-quickbooks.api.intuit.com').persist()
268273
.get('/v3/company/12345/companyinfo/12345')
@@ -285,6 +290,20 @@ describe('Tests for OAuthClient', () => {
285290
.to.be.equal(JSON.stringify(expectedMakeAPICall));
286291
});
287292
});
293+
it('Make API Call in Sandbox Environment with headers as parameters', () => {
294+
oauthClient.getToken().realmId = '12345';
295+
// eslint-disable-next-line no-useless-concat
296+
return oauthClient.makeApiCall({
297+
url: 'https://sandbox-quickbooks.api.intuit.com/v3/company/' + '12345' + '/companyinfo/' + '12345',
298+
headers: {
299+
Accept: "application/json",
300+
}
301+
})
302+
.then((authResponse) => {
303+
expect(JSON.stringify(authResponse.getJson()))
304+
.to.be.equal(JSON.stringify(expectedMakeAPICall));
305+
});
306+
});
288307
it('loadResponseFromJWKsURI', () => {
289308
const request = {
290309
url: 'https://sandbox-quickbooks.api.intuit.com/v3/company/12345/companyinfo/12345',
@@ -324,9 +343,6 @@ describe('Tests for OAuthClient', () => {
324343
});
325344

326345
describe('getPublicKey', () => {
327-
require.cache[require.resolve('rsa-pem-from-mod-exp')] = {
328-
exports: sinon.mock().returns(3),
329-
};
330346
const pem = oauthClient.getPublicKey(3, 4);
331347
expect(pem).to.be.equal(3);
332348
});
@@ -359,7 +375,7 @@ describe('Validate Id Token ', () => {
359375
before(() => {
360376
nock('https://oauth.platform.intuit.com').persist()
361377
.get('/op/v1/jwks')
362-
.reply(200, expectedjwkResponseCall, {
378+
.reply(200, expectedjwkResponseCall.body, {
363379
'content-type': 'application/json;charset=UTF-8',
364380
'content-length': '264',
365381
connection: 'close',
@@ -369,26 +385,41 @@ describe('Validate Id Token ', () => {
369385
'cache-control': 'no-cache, no-store',
370386
pragma: 'no-cache',
371387
});
388+
sinon.stub(jwt, 'verify').returns(true);
372389
});
373390

391+
const mockIdTokenPayload = {
392+
sub: 'b053d994-07d5-468d-b7ee-22e349d2e739',
393+
aud: ['clientID'],
394+
realmid: '1108033471',
395+
auth_time: 1462554475,
396+
iss: 'https://oauth.platform.intuit.com/op/v1',
397+
exp: Date.now() + 60000,
398+
iat: 1462557728,
399+
};
400+
401+
const tokenParts = expectedOpenIDToken.id_token.split('.');
402+
const encodedMockIdTokenPayload = tokenParts[0].concat('.', btoa(JSON.stringify(mockIdTokenPayload)));
403+
const mockToken = Object.assign({}, expectedOpenIDToken, { id_token: encodedMockIdTokenPayload });
404+
374405
it('validate id token returns error if id_token missing', async () => {
375406
delete oauthClient.getToken().id_token;
376407
await expect(oauthClient.validateIdToken()).to.be.rejectedWith(Error);
377408
});
378409

379410
it('Validate Id Token', () => {
380-
oauthClient.getToken().setToken(expectedOpenIDToken);
411+
oauthClient.getToken().setToken(mockToken);
381412
oauthClient.validateIdToken()
382413
.then((response) => {
383-
expect(response).to.be.equal(expectedvalidateIdToken);
414+
expect(response).to.be.equal(true);
384415
});
385416
});
386417

387418
it('Validate Id Token alternative', () => {
388-
oauthClient.setToken(expectedOpenIDToken);
419+
oauthClient.setToken(mockToken);
389420
oauthClient.validateIdToken()
390421
.then((response) => {
391-
expect(response).to.be.equal(expectedOpenIDToken);
422+
expect(response).to.be.equal(true);
392423
});
393424
});
394425
});
@@ -474,6 +505,10 @@ describe('Generate OAuth1Sign', () => {
474505

475506
const oauth1Sign = oauthClient.generateOauth1Sign(params);
476507
expect(oauth1Sign).to.be.a('String');
508+
expect(oauth1Sign).to.have.string('oauth_consumer_key="qyprdFsHNQtdRupMKmYnDt6MOjWBW9');
509+
expect(oauth1Sign).to.have.string('oauth_nonce="nonce');
510+
expect(oauth1Sign).to.have.string('oauth_version="1.0');
511+
expect(oauth1Sign).to.have.string('oauth_token', 'oauth_timestamp', 'oauth_signature');
477512
});
478513
});
479514

test/mocks/jwkResponse.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"body":"{\"keys\": [{\"kty\":\"sample_value_kty\",\"e\":\"sample_value_e\",\"use\":\"sample_value_use\",\"kid\":\"sample_value_kid\",\"alg\":\"sample_value_alg\",\"n\":\"sample_value_n\"}]}"
3-
}
2+
"body":"{\"keys\": [{\"kty\":\"sample_value_kty\",\"e\":\"sample_value_e\",\"use\":\"sample_value_use\",\"kid\":\"r4p5SbL2qaFehFzhj8gI\",\"alg\":\"sample_value_alg\",\"n\":\"sample_value_n\"}]}"
3+
}

0 commit comments

Comments
 (0)