Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix eslint issues
  • Loading branch information
Neha Patwardhan committed Oct 17, 2019
commit f7d2bc5e90db033eae2ff7a97b66c81934a976ab
8 changes: 1 addition & 7 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
}
],
"no-underscore-dangle": "off",
"no-unused-expressions": "off",
"func-names": "off",
"no-mixed-operators": "off",
"eqeqeq": "off",
"function-paren-newline": "off",
"consistent-return": "off",
"no-prototype-builtins": "off"
"no-unused-expressions": "off"
},
"globals": {
"sinon": true,
Expand Down
45 changes: 23 additions & 22 deletions src/OAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
const atob = require('atob');
const oauthSignature = require('oauth-signature');
const objectAssign = require('object-assign');
const Csrf = require('csrf');
const csrf = require('csrf');
const queryString = require('query-string');
const popsicle = require('popsicle');
const os = require('os');
Expand All @@ -39,7 +39,7 @@ const jwt = require('jsonwebtoken');
const AuthResponse = require('./response/AuthResponse');
const version = require('../package.json');
const Token = require('./access-token/Token');
const getPem = require('rsa-pem-from-mod-exp');


/**
* @constructor
Expand All @@ -56,7 +56,7 @@ function OAuthClient(config) {
this.token = new Token(config.token);
this.logging = !!(Object.prototype.hasOwnProperty.call(config, 'logging') && config.logging === true);
this.logger = null;
this.state = new Csrf();
this.state = new csrf(); // eslint-disable-line new-cap

if (this.logging) {
const dir = './logs';
Expand Down Expand Up @@ -98,9 +98,9 @@ OAuthClient.scopes = {
OpenId: 'openid',
Intuit_name: 'intuit_name',
};

OAuthClient.user_agent = `Intuit-OAuthClient-JS_${version}_${os.type()}_${os.release()}_${os.platform()}`;


/**
* Redirect User to Authorization Page
* @param params
Expand Down Expand Up @@ -218,18 +218,18 @@ OAuthClient.prototype.refresh = function refresh() {
* @param {Object} params.refresh_token (refresh_token)
* @returns {Promise<AuthResponse>}
*/
OAuthClient.prototype.refreshUsingToken = function (refreshToken) {
OAuthClient.prototype.refreshUsingToken = function refreshUsingToken(refresh_token) {
return (new Promise(((resolve) => {
/**
* Check if the tokens exist
*/

if (!refreshToken) throw new Error('The Refresh token is missing');
if (!refresh_token) throw new Error('The Refresh token is missing');

const body = {};

body.grant_type = 'refresh_token';
body.refresh_token = refreshToken;
body.refresh_token = refresh_token;

const request = {
url: OAuthClient.tokenEndpoint,
Expand Down Expand Up @@ -270,10 +270,10 @@ OAuthClient.prototype.revoke = function revoke(params) {

const body = {};

body.token = params.access_token || params.refresh_token
|| (this.getToken().isAccessTokenValid() ?
this.getToken().access_token :
this.getToken().refresh_token);
body.token = params.access_token || params.refresh_token ||
(this.getToken().isAccessTokenValid()
? this.getToken().access_token
: this.getToken().refresh_token);

const request = {
url: OAuthClient.revokeEndpoint,
Expand Down Expand Up @@ -442,7 +442,6 @@ OAuthClient.prototype.generateOauth1Sign = function generateOauth1Sign(params) {
if (key === 'minorversion') {
return header;
}

if (idx === array.length - 1) {
return `${header}${key}="${val}"`;
}
Expand All @@ -461,12 +460,12 @@ OAuthClient.prototype.validateIdToken = function validateIdToken(params = {}) {
return (new Promise(((resolve) => {
if (!this.getToken().id_token) throw new Error('The bearer token does not have id_token');

const idToken = this.getToken().id_token || params.id_token;
const id_token = this.getToken().id_token || params.id_token;

// Decode ID Token
const tokenParts = idToken.split('.');
const idTokenHeader = JSON.parse(atob(tokenParts[0]));
const idTokenPayload = JSON.parse(atob(tokenParts[1]));
const token_parts = id_token.split('.');
const id_token_header = JSON.parse(atob(token_parts[0]));
const id_token_payload = JSON.parse(atob(token_parts[1]));

// Step 1 : First check if the issuer is as mentioned in "issuer"
if (id_token_payload.iss !== 'https://oauth.platform.intuit.com/op/v1') return false;
Expand All @@ -475,7 +474,7 @@ OAuthClient.prototype.validateIdToken = function validateIdToken(params = {}) {
if (id_token_payload.aud !== this.clientId) return false;

// Step 3 : ensure the timestamp has not elapsed
if (idTokenPayload.exp < Date.now() / 1000) return false;
if (id_token_payload.exp < Date.now() / 1000) return false;

const request = {
url: OAuthClient.jwks_uri,
Expand All @@ -499,12 +498,12 @@ OAuthClient.prototype.validateIdToken = function validateIdToken(params = {}) {

/**
*
* @param idToken
* @param id_token
* @param kid
* @param request
* @returns {Promise<AuthResponse>}
*/
OAuthClient.prototype.getKeyFromJWKsURI = function (idToken, kid, request) {
OAuthClient.prototype.getKeyFromJWKsURI = function getKeyFromJWKsURI(id_token, kid, request) {
return (new Promise(((resolve) => {
resolve(this.loadResponse(request));
}))).then((response) => {
Expand All @@ -515,7 +514,7 @@ OAuthClient.prototype.getKeyFromJWKsURI = function (idToken, kid, request) {
const key = responseBody.keys.find(el => (el.kid === kid));
const cert = this.getPublicKey(key.n, key.e);

return jwt.verify(idToken, cert);
return jwt.verify(id_token, cert);
}).catch((e) => {
e = this.createError(e);
this.log('error', 'The getKeyFromJWKsURI () threw an exception : ', JSON.stringify(e, null, 2));
Expand All @@ -528,7 +527,9 @@ OAuthClient.prototype.getKeyFromJWKsURI = function (idToken, kid, request) {
* @param modulus
* @param exponent
*/
OAuthClient.prototype.getPublicKey = function (modulus, exponent) {
OAuthClient.prototype.getPublicKey = function getPublicKey(modulus, exponent) {
// eslint-disable-next-line global-require
const getPem = require('rsa-pem-from-mod-exp');
const pem = getPem(modulus, exponent);
return pem;
};
Expand Down Expand Up @@ -656,7 +657,7 @@ OAuthClient.prototype.setToken = function setToken(params) {
*/
OAuthClient.prototype.authHeader = function authHeader() {
const apiKey = `${this.clientId}:${this.clientSecret}`;
return (typeof btoa === 'function') ? btoa(apiKey) : new Buffer(apiKey).toString('base64');
return (typeof btoa === 'function') ? btoa(apiKey) : Buffer.from(apiKey).toString('base64');
};

OAuthClient.prototype.log = function log(level, message, messageData) {
Expand Down
15 changes: 8 additions & 7 deletions test/OAuthClientTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const expectedvalidateIdToken = require('./mocks/validateIdToken.json');
const expectedOpenIDToken = require('./mocks/openID-token.json');
// var expectedErrorResponse = require('./mocks/errorResponse.json');
const expectedMigrationResponse = require('./mocks/authResponse.json');
const refreshAccessToken = require('./mocks/refreshResponse.json');

const oauthClient = new OAuthClientTest({
clientId: 'clientID',
Expand Down Expand Up @@ -94,8 +93,8 @@ describe('Tests for OAuthClient', () => {
const parseRedirect = 'http://localhost:8000/callback?state=testState&code=Q011535008931rqveFweqmueq0GlOHhLPAFMp3NI2KJm5gbMMx';
return oauthClient.createToken(parseRedirect)
.then((authResponse) => {
expect(authResponse.getToken().access_token).to.be
.equal(expectedAccessToken.access_token);
expect(authResponse.getToken().access_token)
.to.be.equal(expectedAccessToken.access_token);
});
});

Expand Down Expand Up @@ -123,6 +122,8 @@ describe('Tests for OAuthClient', () => {
// Refresh bearer tokens
describe('Refresh Bearer Token', () => {
before(() => {
// eslint-disable-next-line global-require
const refreshAccessToken = require('./mocks/refreshResponse.json');
nock('https://oauth.platform.intuit.com').persist()
.post('/oauth2/v1/tokens/bearer')
.reply(200, refreshAccessToken, {
Expand All @@ -138,8 +139,8 @@ describe('Tests for OAuthClient', () => {

it('Refresh the existing tokens', () => oauthClient.refresh()
.then((authResponse) => {
expect(authResponse.getToken().refresh_token).to.be
.equal(expectedAccessToken.refresh_token);
expect(authResponse.getToken().refresh_token)
.to.be.equal(expectedAccessToken.refresh_token);
}));

it('Refresh : refresh token is missing', () => {
Expand Down Expand Up @@ -278,7 +279,7 @@ describe('Tests for OAuthClient', () => {
it('Make API Call in Sandbox Environment', () => {
oauthClient.getToken().realmId = '12345';
// eslint-disable-next-line no-useless-concat
return oauthClient.makeApiCall({ url: 'https://sandbox-quickbooks.api.intuit.com/v3/company/12345/companyinfo/12345' })
return oauthClient.makeApiCall({ url: 'https://sandbox-quickbooks.api.intuit.com/v3/company/' + '12345' + '/companyinfo/' + '12345' })
.then((authResponse) => {
expect(JSON.stringify(authResponse.getJson()))
.to.be.equal(JSON.stringify(expectedMakeAPICall));
Expand Down Expand Up @@ -313,7 +314,7 @@ describe('Tests for OAuthClient', () => {
oauthClient.environment = 'production';
oauthClient.getToken().realmId = '12345';
// eslint-disable-next-line no-useless-concat
return oauthClient.makeApiCall({ url: 'https://quickbooks.api.intuit.com/v3/company/12345/companyinfo/12345' })
return oauthClient.makeApiCall({ url: 'https://quickbooks.api.intuit.com/v3/company/' + '12345' + '/companyinfo/' + '12345' })
.then((authResponse) => {
expect(JSON.stringify(authResponse.getJson()))
.to.be.equal(JSON.stringify(expectedMakeAPICall));
Expand Down