Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "^12.1.2",
"query-string": "^6.12.1",
"rsa-pem-from-mod-exp": "^0.8.4",
"winston": "^3.1.0"
Expand Down
70 changes: 33 additions & 37 deletions src/OAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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()}`,
Expand All @@ -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;
Expand All @@ -210,7 +210,7 @@ OAuthClient.prototype.refresh = function refresh() {

const request = {
url: OAuthClient.tokenEndpoint,
body,
data: body,
method: 'POST',
headers: {
Authorization: `Basic ${this.authHeader()}`,
Expand All @@ -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));
Expand Down Expand Up @@ -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()}`,
Expand All @@ -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(
Expand Down Expand Up @@ -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()}`,
Expand All @@ -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;
Expand Down Expand Up @@ -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 : ',
Expand All @@ -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 : popsicle.createTransport({ type: "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;
})
Expand Down Expand Up @@ -547,12 +543,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(request).then((response) => response);
};

/**
Expand All @@ -561,7 +557,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);
};

/**
Expand Down
16 changes: 5 additions & 11 deletions src/response/AuthResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) || '';
};

Expand All @@ -58,7 +58,6 @@ AuthResponse.prototype.getToken = function getToken() {
return this.token.getToken();
};


/**
* Get Token
* *
Expand Down Expand Up @@ -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 }
* *
Expand All @@ -119,7 +116,6 @@ AuthResponse.prototype.get_intuit_tid = function get_intuit_tid() {
return this.intuit_tid;
};


/**
* isContentType
* *
Expand All @@ -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] || '';
};

/**
Expand All @@ -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;