Skip to content

Commit 49254c7

Browse files
authored
feat: baseUrl option (#85)
1 parent dd5208c commit 49254c7

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ const {
128128
<code>baseUrl</code>
129129
</th>
130130
<td>
131-
When using GitHub Enterprise Server, set the baseUrl to the origin, e.g. <code>https://github.my-enterprise.com/</code>.
131+
When using GitHub Enterprise Server, set the baseUrl to the origin, e.g. <code>https://github.my-enterprise.com</code>.
132132
</td>
133133
</tr>
134134
</tbody>

src/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Options, Result, ResultKeys } from "./types";
22

3-
export const BASE_URL = "https://github.com/login/oauth/authorize";
4-
53
export function oauthAuthorizationUrl(options: Options): Result {
64
const scopesNormalized =
75
typeof options.scopes === "string"
@@ -16,14 +14,12 @@ export function oauthAuthorizationUrl(options: Options): Result {
1614
login: options.login || null,
1715
redirectUrl: options.redirectUrl || null,
1816
scopes: scopesNormalized,
19-
state:
20-
options.state ||
21-
Math.random()
22-
.toString(36)
23-
.substr(2),
24-
url: ""
17+
state: options.state || Math.random().toString(36).substr(2),
18+
url: "",
2519
};
26-
result.url = urlBuilderAuthorize(BASE_URL, result);
20+
21+
const baseUrl = options.baseUrl || "https://github.com";
22+
result.url = urlBuilderAuthorize(`${baseUrl}/login/oauth/authorize`, result);
2723

2824
return result;
2925
}
@@ -35,7 +31,7 @@ function urlBuilderAuthorize(base: string, options: Result) {
3531
login: "login",
3632
redirectUrl: "redirect_uri",
3733
scopes: "scope",
38-
state: "state"
34+
state: "state",
3935
};
4036

4137
let url = base;

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export interface Options {
66
scopes?: string | string[];
77
redirectUrl?: string;
88
state?: string;
9+
baseUrl?: string;
910
}
10-
1111
export interface Result {
1212
allowSignup: boolean;
1313
clientId: string;

test/index.test.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ beforeAll(() => {
44
Math.random = jest.fn(() => 0.123);
55
});
66

7-
// afterAll(() => {
8-
// Math.random.mockRestore()
9-
// });
10-
117
test('oauthAuthorizationUrl({clientId: "1234567890abcdef1234"})', () => {
128
expect(
139
oauthAuthorizationUrl({
14-
clientId: "1234567890abcdef1234"
10+
clientId: "1234567890abcdef1234",
1511
})
1612
).toEqual({
1713
allowSignup: true,
@@ -21,14 +17,14 @@ test('oauthAuthorizationUrl({clientId: "1234567890abcdef1234"})', () => {
2117
scopes: [],
2218
state: "4feornbt361",
2319
url:
24-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361"
20+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
2521
});
2622
});
2723

2824
test('oauthAuthorizationUrl({clientId: "4321fedcba0987654321"})', () => {
2925
expect(
3026
oauthAuthorizationUrl({
31-
clientId: "4321fedcba0987654321"
27+
clientId: "4321fedcba0987654321",
3228
})
3329
).toEqual({
3430
allowSignup: true,
@@ -38,15 +34,15 @@ test('oauthAuthorizationUrl({clientId: "4321fedcba0987654321"})', () => {
3834
scopes: [],
3935
state: "4feornbt361",
4036
url:
41-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=4321fedcba0987654321&state=4feornbt361"
37+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=4321fedcba0987654321&state=4feornbt361",
4238
});
4339
});
4440

4541
test("redirectUrl option", () => {
4642
expect(
4743
oauthAuthorizationUrl({
4844
clientId: "1234567890abcdef1234",
49-
redirectUrl: "https://example.com"
45+
redirectUrl: "https://example.com",
5046
})
5147
).toEqual({
5248
allowSignup: true,
@@ -56,15 +52,15 @@ test("redirectUrl option", () => {
5652
scopes: [],
5753
state: "4feornbt361",
5854
url:
59-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&redirect_uri=https://example.com&state=4feornbt361"
55+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&redirect_uri=https://example.com&state=4feornbt361",
6056
});
6157
});
6258

6359
test("login option", () => {
6460
expect(
6561
oauthAuthorizationUrl({
6662
clientId: "1234567890abcdef1234",
67-
login: "octocat"
63+
login: "octocat",
6864
})
6965
).toEqual({
7066
allowSignup: true,
@@ -74,7 +70,7 @@ test("login option", () => {
7470
scopes: [],
7571
state: "4feornbt361",
7672
url:
77-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361"
73+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
7874
});
7975
});
8076

@@ -83,7 +79,7 @@ test("scopes = []", () => {
8379
oauthAuthorizationUrl({
8480
clientId: "1234567890abcdef1234",
8581
login: "octocat",
86-
scopes: []
82+
scopes: [],
8783
})
8884
).toEqual({
8985
allowSignup: true,
@@ -93,7 +89,7 @@ test("scopes = []", () => {
9389
scopes: [],
9490
state: "4feornbt361",
9591
url:
96-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361"
92+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
9793
});
9894
});
9995

@@ -102,7 +98,7 @@ test('scopes = ""', () => {
10298
oauthAuthorizationUrl({
10399
clientId: "1234567890abcdef1234",
104100
login: "octocat",
105-
scopes: ""
101+
scopes: "",
106102
})
107103
).toEqual({
108104
allowSignup: true,
@@ -112,7 +108,7 @@ test('scopes = ""', () => {
112108
scopes: [],
113109
state: "4feornbt361",
114110
url:
115-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361"
111+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=4feornbt361",
116112
});
117113
});
118114

@@ -121,7 +117,7 @@ test('scopes = "user,public_repo, gist notifications"', () => {
121117
oauthAuthorizationUrl({
122118
clientId: "1234567890abcdef1234",
123119
login: "octocat",
124-
scopes: "user,public_repo, gist notifications"
120+
scopes: "user,public_repo, gist notifications",
125121
})
126122
).toEqual({
127123
allowSignup: true,
@@ -131,7 +127,7 @@ test('scopes = "user,public_repo, gist notifications"', () => {
131127
scopes: ["user", "public_repo", "gist", "notifications"],
132128
state: "4feornbt361",
133129
url:
134-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&scope=user,public_repo,gist,notifications&state=4feornbt361"
130+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&scope=user,public_repo,gist,notifications&state=4feornbt361",
135131
});
136132
});
137133

@@ -141,7 +137,7 @@ test("allowSignup = false", () => {
141137
allowSignup: false,
142138
clientId: "1234567890abcdef1234",
143139
login: "octocat",
144-
scopes: "user,public_repo, gist notifications"
140+
scopes: "user,public_repo, gist notifications",
145141
})
146142
).toEqual({
147143
allowSignup: false,
@@ -151,7 +147,7 @@ test("allowSignup = false", () => {
151147
scopes: ["user", "public_repo", "gist", "notifications"],
152148
state: "4feornbt361",
153149
url:
154-
"https://github.com/login/oauth/authorize?allow_signup=false&client_id=1234567890abcdef1234&login=octocat&scope=user,public_repo,gist,notifications&state=4feornbt361"
150+
"https://github.com/login/oauth/authorize?allow_signup=false&client_id=1234567890abcdef1234&login=octocat&scope=user,public_repo,gist,notifications&state=4feornbt361",
155151
});
156152
});
157153

@@ -160,7 +156,7 @@ test("state = Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI", () => {
160156
oauthAuthorizationUrl({
161157
clientId: "1234567890abcdef1234",
162158
login: "octocat",
163-
state: "Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI"
159+
state: "Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI",
164160
})
165161
).toEqual({
166162
allowSignup: true,
@@ -170,6 +166,24 @@ test("state = Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI", () => {
170166
scopes: [],
171167
state: "Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI",
172168
url:
173-
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI"
169+
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&login=octocat&state=Sjn2oMwNFZPiVm6Mtjn2o9b3xxZ4sVEI",
170+
});
171+
});
172+
173+
test('oauthAuthorizationUrl({clientId: "1234567890abcdef1234", baseUrl: "https://github.my-enterprise.com/"})', () => {
174+
expect(
175+
oauthAuthorizationUrl({
176+
clientId: "1234567890abcdef1234",
177+
baseUrl: "https://github.my-enterprise.com",
178+
})
179+
).toEqual({
180+
allowSignup: true,
181+
clientId: "1234567890abcdef1234",
182+
login: null,
183+
redirectUrl: null,
184+
scopes: [],
185+
state: "4feornbt361",
186+
url:
187+
"https://github.my-enterprise.com/login/oauth/authorize?allow_signup=true&client_id=1234567890abcdef1234&state=4feornbt361",
174188
});
175189
});

0 commit comments

Comments
 (0)