From bf09b38fed9bb842754fca57aaff680dfb50b215 Mon Sep 17 00:00:00 2001 From: pjain22 Date: Mon, 23 Oct 2023 23:23:13 +0530 Subject: [PATCH 1/4] Resolved dependency issue || Axios Introduced --- package.json | 2 +- src/OAuthClient.js | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index fb21b934..fb92e186 100644 --- a/package.json +++ b/package.json @@ -68,9 +68,9 @@ "homepage": "https://github.com/intuit/oauth-jsclient", "dependencies": { "atob": "2.1.2", + "axios": "^1.5.1", "csrf": "^3.0.4", "jsonwebtoken": "^9.0.2", - "popsicle": "^11.0.5", "query-string": "^6.12.1", "rsa-pem-from-mod-exp": "^0.8.4", "winston": "^3.1.0" diff --git a/src/OAuthClient.js b/src/OAuthClient.js index 9662c002..af3e440c 100644 --- a/src/OAuthClient.js +++ b/src/OAuthClient.js @@ -27,7 +27,7 @@ const atob = require('atob'); const Csrf = require('csrf'); const queryString = require('query-string'); -const popsicle = require('popsicle'); +const axios = require('axios'); const os = require('os'); const winston = require('winston'); const path = require('path'); @@ -372,7 +372,7 @@ OAuthClient.prototype.getUserInfo = function getUserInfo() { OAuthClient.prototype.makeApiCall = function makeApiCall(params) { return new Promise((resolve) => { params = params || {}; - const transport = params.transport ? params.transport : popsicle.createTransport({ type: "text" }); + const transport = params.transport ? params.transport : { responseType: 'text' }; const headers = params.headers && typeof params.headers === 'object' @@ -547,12 +547,12 @@ OAuthClient.prototype.validateToken = function validateToken() { }; /** - * Make HTTP Request using Popsicle Client + * Make HTTP Request using Axios Client * @param request * @returns response */ OAuthClient.prototype.loadResponse = function loadResponse(request) { - return popsicle.get(request).then((response) => response); + return axios.get(request).then((response) => response); }; /** @@ -561,7 +561,7 @@ OAuthClient.prototype.loadResponse = function loadResponse(request) { * @returns response */ OAuthClient.prototype.loadResponseFromJWKsURI = function loadResponseFromJWKsURI(request) { - return popsicle.get(request).then((response) => response); + return axios.get(request).then((response) => response); }; /** From 65290fc45ba5118733c2081476e57891936786bb Mon Sep 17 00:00:00 2001 From: ben Date: Sun, 19 Nov 2023 18:23:08 -0500 Subject: [PATCH 2/4] fix: createToken requests --- src/OAuthClient.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OAuthClient.js b/src/OAuthClient.js index af3e440c..c4ee4823 100644 --- a/src/OAuthClient.js +++ b/src/OAuthClient.js @@ -169,7 +169,7 @@ OAuthClient.prototype.createToken = function createToken(uri) { const request = { url: OAuthClient.tokenEndpoint, - body, + data: body, method: 'POST', headers: { Authorization: `Basic ${this.authHeader()}`, @@ -383,7 +383,7 @@ OAuthClient.prototype.makeApiCall = function makeApiCall(params) { Accept: AuthResponse._jsonContentType, 'User-Agent': OAuthClient.user_agent, }, - params.headers + params.headers, ) : Object.assign( {}, @@ -391,7 +391,7 @@ OAuthClient.prototype.makeApiCall = function makeApiCall(params) { Authorization: `Bearer ${this.getToken().access_token}`, Accept: AuthResponse._jsonContentType, 'User-Agent': OAuthClient.user_agent, - } + }, ); const request = { @@ -552,7 +552,7 @@ OAuthClient.prototype.validateToken = function validateToken() { * @returns response */ OAuthClient.prototype.loadResponse = function loadResponse(request) { - return axios.get(request).then((response) => response); + return axios(request).then((response) => response); }; /** From d720400334fed07a447d4111435c1b41645fc028 Mon Sep 17 00:00:00 2001 From: Ben Lapp Date: Wed, 22 Nov 2023 11:24:49 -0500 Subject: [PATCH 3/4] get createToken() to work with axois --- src/OAuthClient.js | 4 ++-- src/response/AuthResponse.js | 16 +++++----------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/OAuthClient.js b/src/OAuthClient.js index c4ee4823..065e95c5 100644 --- a/src/OAuthClient.js +++ b/src/OAuthClient.js @@ -182,8 +182,8 @@ OAuthClient.prototype.createToken = function createToken(uri) { resolve(this.getTokenRequest(request)); }) .then((res) => { - const authResponse = res.json ? res : null; - const json = (authResponse && authResponse.getJson()) || res; + const { response, ...authResponse } = res.json ? res : null; + const json = (authResponse && authResponse.json) || res; this.token.setToken(json); this.log('info', 'Create Token response is : ', JSON.stringify(authResponse, null, 2)); return authResponse; diff --git a/src/response/AuthResponse.js b/src/response/AuthResponse.js index 5a4abee5..63b64269 100644 --- a/src/response/AuthResponse.js +++ b/src/response/AuthResponse.js @@ -44,8 +44,8 @@ function AuthResponse(params) { */ AuthResponse.prototype.processResponse = function processResponse(response) { this.response = response || ''; - this.body = (response && response.body) || ''; - this.json = this.body && this.isJson() ? JSON.parse(this.body) : null; + this.body = (response && response.data) || ''; + this.json = this.body && this.isJson() ? this.body : null; this.intuit_tid = (response && response.headers && response.headers.intuit_tid) || ''; }; @@ -58,7 +58,6 @@ AuthResponse.prototype.getToken = function getToken() { return this.token.getToken(); }; - /** * Get Token * * @@ -92,11 +91,9 @@ AuthResponse.prototype.headers = function headers() { * @returns {*|boolean} */ AuthResponse.prototype.valid = function valid() { - return (this.response && Number(this.response.status) >= 200 - && Number(this.response.status) < 300); + return this.response && Number(this.response.status) >= 200 && Number(this.response.status) < 300; }; - /** * Get Json () { returns token as JSON } * * @@ -119,7 +116,6 @@ AuthResponse.prototype.get_intuit_tid = function get_intuit_tid() { return this.intuit_tid; }; - /** * isContentType * * @@ -135,7 +131,7 @@ AuthResponse.prototype.isContentType = function isContentType(contentType) { * @returns {string} getContentType */ AuthResponse.prototype.getContentType = function getContentType() { - return this.response.get(AuthResponse._contentType) || ''; + return this.response.headers[AuthResponse._contentType] || ''; }; /** @@ -147,10 +143,8 @@ AuthResponse.prototype.isJson = function isJson() { return this.isContentType('application/json'); }; - -AuthResponse._contentType = 'Content-Type'; +AuthResponse._contentType = 'content-type'; AuthResponse._jsonContentType = 'application/json'; AuthResponse._urlencodedContentType = 'application/x-www-form-urlencoded'; - module.exports = AuthResponse; From f123f5b411cdfb2dab668761447fb48f2601552d Mon Sep 17 00:00:00 2001 From: ben Date: Thu, 23 Nov 2023 08:11:26 -0500 Subject: [PATCH 4/4] migrate rest of the request to axios --- src/OAuthClient.js | 56 +++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/src/OAuthClient.js b/src/OAuthClient.js index 065e95c5..c6b3b0d8 100644 --- a/src/OAuthClient.js +++ b/src/OAuthClient.js @@ -210,7 +210,7 @@ OAuthClient.prototype.refresh = function refresh() { const request = { url: OAuthClient.tokenEndpoint, - body, + data: body, method: 'POST', headers: { Authorization: `Basic ${this.authHeader()}`, @@ -223,7 +223,7 @@ OAuthClient.prototype.refresh = function refresh() { resolve(this.getTokenRequest(request)); }) .then((res) => { - const authResponse = res.json ? res : null; + const { request, ...authResponse } = res.json ? res : null; const json = (authResponse && authResponse.getJson()) || res; this.token.setToken(json); this.log('info', 'Refresh Token () response is : ', JSON.stringify(authResponse, null, 2)); @@ -252,7 +252,7 @@ OAuthClient.prototype.refreshUsingToken = function refreshUsingToken(refresh_tok const request = { url: OAuthClient.tokenEndpoint, - body, + data: body, method: 'POST', headers: { Authorization: `Basic ${this.authHeader()}`, @@ -265,7 +265,7 @@ OAuthClient.prototype.refreshUsingToken = function refreshUsingToken(refresh_tok resolve(this.getTokenRequest(request)); }) .then((res) => { - const authResponse = res.json ? res : null; + const { request, ...authResponse } = res.json ? res : null; const json = (authResponse && authResponse.getJson()) || res; this.token.setToken(json); this.log( @@ -303,7 +303,7 @@ OAuthClient.prototype.revoke = function revoke(params) { const request = { url: OAuthClient.revokeEndpoint, - body, + data: body, method: 'POST', headers: { Authorization: `Basic ${this.authHeader()}`, @@ -315,7 +315,7 @@ OAuthClient.prototype.revoke = function revoke(params) { resolve(this.getTokenRequest(request)); }) - .then((authResponse) => { + .then(({ request, ...authResponse }) => { this.token.clearToken(); this.log('info', 'Revoke Token () response is : ', JSON.stringify(authResponse, null, 2)); return authResponse; @@ -349,7 +349,7 @@ OAuthClient.prototype.getUserInfo = function getUserInfo() { resolve(this.getTokenRequest(request)); }) .then((res) => { - const authResponse = res.json ? res : null; + const { request, ...authResponse } = res.json ? res : null; this.log( 'info', 'The Get User Info () response is : ', @@ -365,47 +365,43 @@ OAuthClient.prototype.getUserInfo = function getUserInfo() { /** * Make API call. Pass the url,method,headers using `params` object - * * - * @param {Object} params + * + * @param {params} params + * @param {string} params.url + * @param {string} params.method (optional) default is GET + * @param {Object} params.headers (optional) + * @param {Object} params.body (optional) + * @param {string} params.responseType (optional) default is json - options are json, text, stream, arraybuffer * @returns {Promise} */ OAuthClient.prototype.makeApiCall = function makeApiCall(params) { return new Promise((resolve) => { params = params || {}; - const transport = params.transport ? params.transport : { responseType: 'text' }; + const responseType = params.responseType ? params.responseType : 'json'; + + const baseHeaders = { + Authorization: `Bearer ${this.getToken().access_token}`, + Accept: AuthResponse._jsonContentType, + 'User-Agent': OAuthClient.user_agent, + }; const headers = params.headers && typeof params.headers === 'object' - ? Object.assign( - {}, - { - Authorization: `Bearer ${this.getToken().access_token}`, - Accept: AuthResponse._jsonContentType, - 'User-Agent': OAuthClient.user_agent, - }, - params.headers, - ) - : Object.assign( - {}, - { - Authorization: `Bearer ${this.getToken().access_token}`, - Accept: AuthResponse._jsonContentType, - 'User-Agent': OAuthClient.user_agent, - }, - ); + ? Object.assign({}, baseHeaders, params.headers) + : Object.assign({}, baseHeaders); const request = { url: params.url, method: params.method || 'GET', headers, - transport, + responseType, }; - params.body && (request.body = params.body); + params.body && (request.data = params.body); resolve(this.getTokenRequest(request)); }) - .then((authResponse) => { + .then(({ request, ...authResponse }) => { this.log('info', 'The makeAPICall () response is : ', JSON.stringify(authResponse, null, 2)); return authResponse; })