diff --git a/dist/axios.d.ts b/dist/axios.d.ts index 3665ccf..f6e011c 100644 --- a/dist/axios.d.ts +++ b/dist/axios.d.ts @@ -1 +1 @@ -export default function (step: any, config: any): Promise; +export default function (step: any, config: any, signConfig?: any): Promise; diff --git a/dist/axios.js b/dist/axios.js index 53d8ce8..fe3ec62 100644 --- a/dist/axios.js +++ b/dist/axios.js @@ -28,8 +28,32 @@ export default async function(step: any, config) { } } */ -async function default_1(step, config) { +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI +// see non-escaped chars +// this handles not encoding [!'()*] +function encodeReservedChars(str) { + return str.replace(/[!'()*]/g, function (c) { + return '%' + c.charCodeAt(0).toString(16); + }); +} +async function default_1(step, config, signConfig) { // XXX warn about mutating config object... or clone? + // OAuth1 request + if (signConfig) { + const { oauthSignerUri, token } = signConfig; + // this handles encoding query string to make sure we match what we sign + const url = new URL(config.url); + url.search = encodeReservedChars(url.search.substr(1)); + config.url = url.toString(); + const payload = { + requestData: config, + token, + }; + const oauthSignature = (await axios_1.default.post(oauthSignerUri, payload)).data; + if (!config.headers) + config.headers = {}; + config.headers.Authorization = oauthSignature; + } for (const k in config.headers || {}) { if (typeof config.headers[k] === "undefined") { delete config.headers[k]; diff --git a/lib/axios.ts b/lib/axios.ts index 656b7dd..1f79263 100644 --- a/lib/axios.ts +++ b/lib/axios.ts @@ -28,8 +28,34 @@ export default async function(step: any, config) { } */ -export default async function(step: any, config) { +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI +// see non-escaped chars +// this handles not encoding [!'()*] +function encodeReservedChars(str) { + return str.replace(/[!'()*]/g, function(c) { + return '%' + c.charCodeAt(0).toString(16) + }) +} + +export default async function(step: any, config, signConfig?) { // XXX warn about mutating config object... or clone? + // OAuth1 request + if (signConfig) { + const {oauthSignerUri, token} = signConfig + + // this handles encoding query string to make sure we match what we sign + const url = new URL(config.url) + url.search = encodeReservedChars(url.search.substr(1)) + config.url = url.toString() + + const payload = { + requestData: config, + token, + } + const oauthSignature = (await axios.post(oauthSignerUri, payload)).data + if (!config.headers) config.headers = {} + config.headers.Authorization = oauthSignature + } for (const k in config.headers || {}) { if (typeof config.headers[k] === "undefined") { delete config.headers[k] diff --git a/package-lock.json b/package-lock.json index 6a42055..8a8cbd8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@pipedreamhq/platform", - "version": "0.2.2", + "version": "0.2.3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2346,7 +2346,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2367,12 +2368,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2387,17 +2390,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2514,7 +2520,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2526,6 +2533,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2540,6 +2548,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2547,12 +2556,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2571,6 +2582,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2651,7 +2663,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2663,6 +2676,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2748,7 +2762,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2784,6 +2799,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2803,6 +2819,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2846,12 +2863,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/package.json b/package.json index 5573251..38dd9b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pipedreamhq/platform", - "version": "0.2.2", + "version": "0.2.3", "description": "Pipedream platform globals (typing and runtime type checking)", "homepage": "https://pipedream.com", "main": "dist/index.js",