Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Add optional params
if (
responseType === c.RESPONSE_TYPE_ID_TOKEN_TOKEN ||
responseType === c.RESPONSE_TYPE_ID_TOKEN
) {
const nonce = Math.floor(Math.random() * 9999999999999) + 1;
params.push({ name: c.P_NONCE, value: nonce });
}
redirectUri && params.push({ name: c.P_REDIRECT_URI, value: redirectUri });
scope && params.push({ name: c.P_SCOPE, value: scope });
state && params.push({ name: c.P_STATE, value: state });
audience && params.push({ name: c.P_AUDIENCE, value: audience });
// Add query params to URL
const qs = buildQueryStringFromParams(params);
const finalUrl = joinUrlAndQueryString(authorizationUrl, qs);
const redirectedTo = await authorizeUserInWindow(
finalUrl,
/(access_token=|id_token=)/,
/(error=)/,
);
const fragment = redirectedTo.split('#')[1];
if (fragment) {
const results = responseToObject(fragment, [
c.P_ACCESS_TOKEN,
c.P_ID_TOKEN,
c.P_TOKEN_TYPE,
c.P_EXPIRES_IN,
c.P_SCOPE,
resource = '',
) {
const params = [
{ name: c.P_RESPONSE_TYPE, value: c.RESPONSE_TYPE_CODE },
{ name: c.P_CLIENT_ID, value: clientId },
];
// Add optional params
redirectUri && params.push({ name: c.P_REDIRECT_URI, value: redirectUri });
scope && params.push({ name: c.P_SCOPE, value: scope });
state && params.push({ name: c.P_STATE, value: state });
audience && params.push({ name: c.P_AUDIENCE, value: audience });
resource && params.push({ name: c.P_RESOURCE, value: resource });
// Add query params to URL
const qs = buildQueryStringFromParams(params);
const finalUrl = joinUrlAndQueryString(url, qs);
const successRegex = new RegExp(`${escapeRegex(redirectUri)}.*(code=)`, 'i');
const failureRegex = new RegExp(`${escapeRegex(redirectUri)}.*(error=)`, 'i');
const redirectedTo = await authorizeUserInWindow(finalUrl, successRegex, failureRegex);
console.log('[oauth2] Detected redirect ' + redirectedTo);
const { query } = urlParse(redirectedTo);
return responseToObject(query, [
c.P_CODE,
c.P_STATE,
c.P_ERROR,
c.P_ERROR_DESCRIPTION,
c.P_ERROR_URI,
]);
setOpt(Curl.option.SSLKEY, ensureFile(key));
addTimelineText('Adding SSL KEY certificate');
}
if (passphrase) {
setOpt(Curl.option.KEYPASSWD, passphrase);
}
}
}
// Build the body
let noBody = false;
let requestBody = null;
const expectsBody = ['POST', 'PUT', 'PATCH'].includes(renderedRequest.method.toUpperCase());
if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_URLENCODED) {
requestBody = buildQueryStringFromParams(renderedRequest.body.params || [], false);
} else if (renderedRequest.body.mimeType === CONTENT_TYPE_FORM_DATA) {
const params = renderedRequest.body.params || [];
const { filePath: multipartBodyPath, boundary, contentLength } = await buildMultipart(
params,
);
// Extend the Content-Type header
const contentTypeHeader = getContentTypeHeader(headers);
if (contentTypeHeader) {
contentTypeHeader.value = `multipart/form-data; boundary=${boundary}`;
} else {
headers.push({
name: 'Content-Type',
value: `multipart/form-data; boundary=${boundary}`,
});
}
const { request } = props;
const enabledParameters = request.parameters.filter(p => !p.disabled);
let result;
try {
result = await props.handleRender({
url: request.url,
parameters: enabledParameters,
});
} catch (err) {
// Just ignore failures
}
if (result) {
const { url, parameters } = result;
const qs = buildQueryStringFromParams(parameters);
const fullUrl = joinUrlAndQueryString(url, qs);
this.setState({
string: smartEncodeUrl(fullUrl, request.settingEncodeUrl),
});
}
}
_isMatch(request: Request, searchStrings: string): number | null {
let finalUrl = request.url;
if (request.parameters) {
finalUrl = joinUrlAndQueryString(finalUrl, buildQueryStringFromParams(request.parameters));
}
const match = fuzzyMatchAll(
searchStrings,
[request.name, finalUrl, request.method || '', this._groupOf(request).join('/')],
{ splitSpace: true },
);
// Match exact Id
const matchesId = request._id === searchStrings;
// _id match is the highest;
if (matchesId) {
return Infinity;
}
module.exports = function (context) {
//handling for comma values because the gateway expects it to be percent encoded
context.request.getParameters().forEach( (entry) => {
context.request.setParameter(entry.name, entry.value.replace(/,/g, "%25252C"));
});
const qs = buildQueryStringFromParams(context.request.getParameters());
const fullUrl = joinUrlAndQueryString(context.request.getUrl(), qs);
const url = smartEncodeUrl(fullUrl, true);
const mastercard = context.request.getEnvironmentVariable('mastercard');
if (mastercard) {
try {
const p12Content = fs.readFileSync(mastercard.keystoreP12Path, 'binary');
const p12Asn1 = forge.asn1.fromDer(p12Content, false);
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, mastercard.keystorePassword);
const keyObj = p12.getBags({
friendlyName: mastercard.keyAlias,
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
}).friendlyName[0];
const signingKey = forge.pki.privateKeyToPem(keyObj.key);
const authHeader = oauth.getAuthorizationHeader(URL.parse(url), context.request.getMethod(), context.request.getBodyText(), mastercard.consumerKey, signingKey);
render() {
const { email, body, subject, children } = this.props;
const params = [];
if (subject) {
params.push({ name: 'subject', value: subject });
}
if (body) {
params.push({ name: 'body', value: body });
}
const qs = buildQueryStringFromParams(params);
const href = joinUrlAndQueryString(`mailto:${email}`, qs);
return {children || email};
}
}