Skip to content
Open
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
52 changes: 41 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ const fetch = require('node-fetch');
const jwt = require('jsonwebtoken');
const FormData = require('form-data');

const MISSING_PARAMS = 'missing_params';
const SIGN_FAILED = 'sign_failed';
const REQUEST_FAILED = 'request_failed';
const UNEXPECTED_RESPONSE_BODY = 'invalid_response_body';

const throwRequestFailedError = details => {
const error = new Error(
`Request failed while swapping the jwt token. ${details}`
);
error.code = REQUEST_FAILED;
throw error;
};

const throwUnexpectedResponseError = details => {
const error = new Error(
`Unexpected response received while swapping the jwt token. ${details}`
);
error.code = UNEXPECTED_RESPONSE_BODY;
throw error;
};

async function authorize(options) {
let {
clientId,
Expand All @@ -34,9 +55,11 @@ async function authorize(options) {
!privateKey ? errors.push('privateKey') : '';
!metaScopes || metaScopes.length === 0 ? errors.push('metaScopes') : '';
if (errors.length > 0) {
return Promise.reject(
new Error(`Required parameter(s) ${errors.join(', ')} are missing`)
const missingParamsError = new Error(
`Required parameter(s) ${errors.join(', ')} are missing`
);
missingParamsError.code = MISSING_PARAMS;
throw missingParamsError;
}

if (metaScopes.constructor !== Array) {
Expand Down Expand Up @@ -66,7 +89,8 @@ async function authorize(options) {
{ algorithm: 'RS256' }
);
} catch (tokenError) {
return Promise.reject(tokenError);
tokenError.code = SIGN_FAILED;
throw tokenError;
}

const form = new FormData();
Expand All @@ -81,19 +105,25 @@ async function authorize(options) {
};

return fetch(`${ims}/ims/exchange/jwt/`, postOptions)
.then(res => res.json())
.catch(e => throwRequestFailedError(e.message))
.then(res => {
// if (res.ok) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, even when the swap was successful, res.ok is false and the status code is 400. I don't know why this is happening.

return res.json();
// } else {
// throwRequestFailedError(res.statusText);
// }
})
.catch(e => throwUnexpectedResponseError(e.message))
.then(json => {
const { access_token, error, error_description } = json;
if (!access_token) {
if (error && error_description) {
return Promise.reject(new Error(`${error}: ${error_description}`));
const swapError = new Error(error_description);
swapError.code = error;
throw swapError;
} else {
return Promise.reject(
new Error(
`An unknown error occurred while swapping jwt. The response is as follows: ${JSON.stringify(
json
)}`
)
throwUnexpectedResponseError(
`The response body is as follows: ${JSON.stringify(json)}`
);
}
}
Expand Down