Skip to content

Commit c77a58a

Browse files
authored
fix: encode url components (#163)
1 parent df7b381 commit c77a58a

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function urlBuilderAuthorize(
8080
// Finally, build the URL
8181
.forEach(([key, value], index) => {
8282
url += index === 0 ? `?` : "&";
83-
url += `${key}=${value}`;
83+
url += `${key}=${encodeURIComponent(value)}`;
8484
});
8585

8686
return url;

test/index.test.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ test('oauthAuthorizationUrl({clientId: "1234567890abcdef1234"})', () => {
1717
redirectUrl: null,
1818
scopes: [],
1919
state: "4feornbt361",
20-
url:
21-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
20+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
2221
});
2322
});
2423

@@ -35,8 +34,7 @@ test('oauthAuthorizationUrl({clientId: "1234567890abcdef1234", clientType: "oaut
3534
redirectUrl: null,
3635
scopes: [],
3736
state: "4feornbt361",
38-
url:
39-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
37+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
4038
});
4139
});
4240

@@ -53,8 +51,7 @@ test('oauthAuthorizationUrl({clientId: "lv1.1234567890abcdef", clientType: "gith
5351
login: null,
5452
redirectUrl: null,
5553
state: "4feornbt361",
56-
url:
57-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=lv1.1234567890abcdef&state=4feornbt361",
54+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=lv1.1234567890abcdef&state=4feornbt361",
5855
});
5956
});
6057

@@ -71,27 +68,25 @@ test('oauthAuthorizationUrl({clientId: "4321fedcba0987654321"})', () => {
7168
redirectUrl: null,
7269
scopes: [],
7370
state: "4feornbt361",
74-
url:
75-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=4321fedcba0987654321&state=4feornbt361",
71+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=4321fedcba0987654321&state=4feornbt361",
7672
});
7773
});
7874

7975
test("redirectUrl option", () => {
8076
expect(
8177
oauthAuthorizationUrl({
8278
clientId: "1234567890abcdef1234",
83-
redirectUrl: "https://example.com",
79+
redirectUrl: "https://example.com?q=octocat&sort=date",
8480
})
8581
).toEqual({
8682
allowSignup: true,
8783
clientId: "1234567890abcdef1234",
8884
clientType: "oauth-app",
8985
login: null,
90-
redirectUrl: "https://example.com",
86+
redirectUrl: "https://example.com?q=octocat&sort=date",
9187
scopes: [],
9288
state: "4feornbt361",
93-
url:
94-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&redirect_uri=https://example.com&state=4feornbt361",
89+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&redirect_uri=https%3A%2F%2Fexample.com%3Fq%3Doctocat%26sort%3Ddate&state=4feornbt361",
9590
});
9691
});
9792

@@ -109,8 +104,7 @@ test("login option", () => {
109104
redirectUrl: null,
110105
scopes: [],
111106
state: "4feornbt361",
112-
url:
113-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
107+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
114108
});
115109
});
116110

@@ -129,8 +123,7 @@ test("scopes = []", () => {
129123
redirectUrl: null,
130124
scopes: [],
131125
state: "4feornbt361",
132-
url:
133-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
126+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
134127
});
135128
});
136129

@@ -149,8 +142,7 @@ test('scopes = ""', () => {
149142
redirectUrl: null,
150143
scopes: [],
151144
state: "4feornbt361",
152-
url:
153-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
145+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
154146
});
155147
});
156148

@@ -169,8 +161,7 @@ test('scopes = "user,public_repo, gist notifications"', () => {
169161
redirectUrl: null,
170162
scopes: ["user", "public_repo", "gist", "notifications"],
171163
state: "4feornbt361",
172-
url:
173-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&scope=user,public_repo,gist,notifications&state=4feornbt361",
164+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&scope=user%2Cpublic_repo%2Cgist%2Cnotifications&state=4feornbt361",
174165
});
175166
});
176167

@@ -190,8 +181,7 @@ test("allowSignup = false", () => {
190181
redirectUrl: null,
191182
scopes: ["user", "public_repo", "gist", "notifications"],
192183
state: "4feornbt361",
193-
url:
194-
"https://github.com/login/oauth/authorize?allow_signup=false&client_id=1234567890abcdef1234&login=octocat&scope=user,public_repo,gist,notifications&state=4feornbt361",
184+
url: "https://github.com/login/oauth/authorize?allow_signup=false&client_id=1234567890abcdef1234&login=octocat&scope=user%2Cpublic_repo%2Cgist%2Cnotifications&state=4feornbt361",
195185
});
196186
});
197187

@@ -210,8 +200,7 @@ test("state = Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI", () => {
210200
redirectUrl: null,
211201
scopes: [],
212202
state: "Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI",
213-
url:
214-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI",
203+
url: "https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI",
215204
});
216205
});
217206

@@ -229,7 +218,6 @@ test('oauthAuthorizationUrl({clientId: "1234567890abcdef1234", baseUrl: "https:/
229218
redirectUrl: null,
230219
scopes: [],
231220
state: "4feornbt361",
232-
url:
233-
"https://github.my-enterprise.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
221+
url: "https://github.my-enterprise.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
234222
});
235223
});

0 commit comments

Comments
 (0)