Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit a2b874b

Browse files
committed
fix oauth1 params serializer
1 parent cf6c875 commit a2b874b

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

dist/axios.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const axios_1 = require("axios");
44
const buildURL = require("axios/lib/helpers/buildURL");
5+
const querystring = require("querystring");
56
const utils_1 = require("./utils");
67
function cleanObject(o) {
78
for (const k in o || {}) {
@@ -32,22 +33,15 @@ function removeSearchFromUrl(config) {
3233
config.url = url.toString(); // if ends with ? should be okay, but could be cleaner
3334
}
3435
}
35-
// this fixes query strings with spaces in them causing issues when signing
36-
// XXX https://github.com/axios/axios/pull/2563
37-
function paramsSerializer(p) {
38-
const encodeKey = (k) => {
39-
return encodeURIComponent(k)
40-
.replace(/%40/gi, '@')
41-
.replace(/%3A/gi, ':')
42-
.replace(/%24/g, '$')
43-
.replace(/%2C/gi, ',')
44-
.replace(/%20/g, '+')
45-
.replace(/%5B/gi, '[')
46-
.replace(/%5D/gi, ']');
47-
};
48-
return Object.keys(p).map(k => encodeKey(k) + '=' + encodeURIComponent(p[k])).join('&');
36+
// https://github.com/ttezel/twit/blob/master/lib/helpers.js#L11
37+
function oauth1ParamsSerializer(p) {
38+
return querystring.stringify(p)
39+
.replace(/\!/g, "%21")
40+
.replace(/\'/g, "%27")
41+
.replace(/\(/g, "%28")
42+
.replace(/\)/g, "%29")
43+
.replace(/\*/g, "%2A");
4944
}
50-
;
5145
// XXX warn about mutating config object... or clone?
5246
async function default_1(step, config, signConfig) {
5347
cleanObject(config.headers);
@@ -61,9 +55,10 @@ async function default_1(step, config, signConfig) {
6155
const { oauthSignerUri, token } = signConfig;
6256
const requestData = {
6357
method: config.method || "get",
64-
url: buildURL(config.url, config.params, paramsSerializer),
58+
url: buildURL(config.url, config.params, oauth1ParamsSerializer),
6559
data: config.data,
6660
};
61+
config.paramsSerializer = oauth1ParamsSerializer;
6762
const payload = {
6863
requestData,
6964
token,

lib/axios.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios from "axios"
22
import { AxiosRequestConfig } from "axios"
33
import * as buildURL from "axios/lib/helpers/buildURL"
4+
import * as querystring from "querystring"
45
import { cloneSafe } from "./utils"
56

67
function cleanObject(o: {string: any}) {
@@ -31,21 +32,15 @@ function removeSearchFromUrl(config: AxiosRequestConfig) {
3132
}
3233
}
3334

34-
// this fixes query strings with spaces in them causing issues when signing
35-
// XXX https://github.com/axios/axios/pull/2563
36-
function paramsSerializer(p: any) {
37-
const encodeKey = (k: string) => {
38-
return encodeURIComponent(k)
39-
.replace(/%40/gi, '@')
40-
.replace(/%3A/gi, ':')
41-
.replace(/%24/g, '$')
42-
.replace(/%2C/gi, ',')
43-
.replace(/%20/g, '+')
44-
.replace(/%5B/gi, '[')
45-
.replace(/%5D/gi, ']')
46-
}
47-
return Object.keys(p).map(k => encodeKey(k) + '=' + encodeURIComponent(p[k])).join('&')
48-
};
35+
// https://github.com/ttezel/twit/blob/master/lib/helpers.js#L11
36+
function oauth1ParamsSerializer(p: any) {
37+
return querystring.stringify(p)
38+
.replace(/\!/g, "%21")
39+
.replace(/\'/g, "%27")
40+
.replace(/\(/g, "%28")
41+
.replace(/\)/g, "%29")
42+
.replace(/\*/g, "%2A")
43+
}
4944

5045
// XXX warn about mutating config object... or clone?
5146
export default async function(step: any, config: AxiosRequestConfig, signConfig?: any) {
@@ -60,9 +55,10 @@ export default async function(step: any, config: AxiosRequestConfig, signConfig?
6055
const {oauthSignerUri, token} = signConfig
6156
const requestData = {
6257
method: config.method || "get",
63-
url: buildURL(config.url, config.params, paramsSerializer), // build url as axios will
58+
url: buildURL(config.url, config.params, oauth1ParamsSerializer), // build url as axios will
6459
data: config.data,
6560
}
61+
config.paramsSerializer = oauth1ParamsSerializer
6662
const payload = {
6763
requestData,
6864
token,

0 commit comments

Comments
 (0)