Skip to content

Commit 56e1c46

Browse files
authored
Merge pull request #3 from Greenheart/feature/error-handling-and-updated-deps
Feature: Error handling and updated deps
2 parents 9bc7982 + 8521aaa commit 56e1c46

File tree

7 files changed

+123
-101
lines changed

7 files changed

+123
-101
lines changed

api/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IncomingMessage, ServerResponse } from "http";
22
import { AuthorizationCode } from "simple-oauth2";
33
import { randomBytes } from "crypto";
4-
import { config } from "../lib/config";
4+
import { config, Provider } from "../lib/config";
55
import { scopes } from "../lib/scopes";
66

77
export const randomString = () => randomBytes(4).toString("hex");
@@ -10,7 +10,7 @@ export default async (req: IncomingMessage, res: ServerResponse) => {
1010
const { host } = req.headers;
1111
const url = new URL(`https://${host}/${req.url}`);
1212
const urlParams = url.searchParams;
13-
const provider = urlParams.get("provider");
13+
const provider = urlParams.get("provider") as Provider;
1414

1515
const client = new AuthorizationCode(config(provider));
1616

api/callback.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import { IncomingMessage, ServerResponse } from "http";
22
import { AuthorizationCode } from "simple-oauth2";
3-
import { config } from "../lib/config";
3+
import { config, Provider } from "../lib/config";
44

55
export default async (req: IncomingMessage, res: ServerResponse) => {
66
const { host } = req.headers;
77
const url = new URL(`https://${host}/${req.url}`);
88
const urlParams = url.searchParams;
99
const code = urlParams.get("code");
10-
const provider = urlParams.get("provider");
11-
const client = new AuthorizationCode(config(provider));
12-
const tokenParams = {
13-
code,
14-
redirect_uri: `https://${host}/callback?provider=${provider}`,
15-
};
10+
const provider = urlParams.get("provider") as Provider;
1611

1712
try {
13+
if (!code) throw new Error(`Missing code ${code}`);
14+
15+
const client = new AuthorizationCode(config(provider));
16+
const tokenParams = {
17+
code,
18+
redirect_uri: `https://${host}/callback?provider=${provider}`,
19+
};
20+
1821
const accessToken = await client.getToken(tokenParams);
19-
const token = accessToken.token["access_token"];
22+
const token = accessToken.token["access_token"] as string;
2023

2124
const responseBody = renderBody("success", {
2225
token,

lib/config.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
export const config = (provider: string) => ({
2-
client: {
3-
id: client[provider].id,
4-
secret: client[provider].secret,
5-
},
6-
auth: {
7-
tokenHost: auth[provider].tokenHost,
8-
tokenPath: auth[provider].tokenPath,
9-
authorizePath: auth[provider].authorizePath,
10-
},
11-
});
1+
export type Provider = "github" | "gitlab";
2+
export const providers: Provider[] = ["github", "gitlab"];
3+
4+
export const config = (provider: Provider) => {
5+
if (!providers.includes(provider)) {
6+
throw new Error(`Unsupported provider ${provider}`);
7+
}
8+
return {
9+
client: client[provider],
10+
auth: auth[provider],
11+
};
12+
};
1213

13-
const auth = {
14+
const auth: Record<
15+
Provider,
16+
{ tokenHost: string; tokenPath: string; authorizePath: string }
17+
> = {
1418
github: {
1519
tokenHost: "https://github.com",
1620
tokenPath: "/login/oauth/access_token",
@@ -23,13 +27,13 @@ const auth = {
2327
},
2428
};
2529

26-
const client = {
30+
const client: Record<Provider, { id: string; secret: string }> = {
2731
github: {
28-
id: process.env.OAUTH_GITHUB_CLIENT_ID,
29-
secret: process.env.OAUTH_GITHUB_CLIENT_SECRET,
32+
id: process.env.OAUTH_GITHUB_CLIENT_ID as string,
33+
secret: process.env.OAUTH_GITHUB_CLIENT_SECRET as string,
3034
},
3135
gitlab: {
32-
id: process.env.OAUTH_GITLAB_CLIENT_ID,
33-
secret: process.env.OAUTH_GITLAB_CLIENT_SECRET,
36+
id: process.env.OAUTH_GITLAB_CLIENT_ID as string,
37+
secret: process.env.OAUTH_GITLAB_CLIENT_SECRET as string,
3438
},
3539
};

lib/scopes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const scopes = {
1+
import { Provider } from "./config";
2+
3+
export const scopes: Record<Provider, string> = {
24
github: "repo,user",
35
gitlab: "api",
46
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"license": "MIT",
66
"private": true,
77
"dependencies": {
8-
"simple-oauth2": "^4.2.0"
8+
"simple-oauth2": "~5.0.0"
99
},
1010
"devDependencies": {
11-
"@types/node": "^14.14.25",
12-
"@types/simple-oauth2": "^4.1.0"
11+
"@types/node": "^18.14.0",
12+
"@types/simple-oauth2": "^5.0.2"
1313
}
1414
}

vercel.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"rewrites": [
33
{ "source": "/auth", "destination": "/api/auth.ts" },
44
{ "source": "/callback", "destination": "/api/callback.ts" }
5-
]
5+
],
6+
"github": {
7+
"silent": true
8+
}
69
}

yarn.lock

Lines changed: 79 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,95 +2,105 @@
22
# yarn lockfile v1
33

44

5-
"@hapi/boom@9.x.x":
6-
version "9.1.1"
7-
resolved "https://registry.yarnpkg.com/@hapi/boom/-/boom-9.1.1.tgz#89e6f0e01637c2a4228da0d113e8157c93677b04"
8-
integrity sha512-VNR8eDbBrOxBgbkddRYIe7+8DZ+vSbV6qlmaN2x7eWjsUjy2VmQgChkOKcVZIeupEZYj+I0dqNg430OhwzagjA==
5+
"@hapi/boom@^10.0.1":
6+
"integrity" "sha512-ERcCZaEjdH3OgSJlyjVk8pHIFeus91CjKP3v+MpgBNp5IvGzP2l/bRiD78nqYcKPaZdbKkK5vDBVPd2ohHBlsA=="
7+
"resolved" "https://registry.npmjs.org/@hapi/boom/-/boom-10.0.1.tgz"
8+
"version" "10.0.1"
99
dependencies:
10-
"@hapi/hoek" "9.x.x"
10+
"@hapi/hoek" "^11.0.2"
1111

12-
"@hapi/bourne@2.x.x":
13-
version "2.0.0"
14-
resolved "https://registry.yarnpkg.com/@hapi/bourne/-/bourne-2.0.0.tgz#5bb2193eb685c0007540ca61d166d4e1edaf918d"
15-
integrity sha512-WEezM1FWztfbzqIUbsDzFRVMxSoLy3HugVcux6KDDtTqzPsLE8NDRHfXvev66aH1i2oOKKar3/XDjbvh/OUBdg==
12+
"@hapi/bourne@^3.0.0":
13+
"integrity" "sha512-Waj1cwPXJDucOib4a3bAISsKJVb15MKi9IvmTI/7ssVEm6sywXGjVJDhl6/umt1pK1ZS7PacXU3A1PmFKHEZ2w=="
14+
"resolved" "https://registry.npmjs.org/@hapi/bourne/-/bourne-3.0.0.tgz"
15+
"version" "3.0.0"
1616

17-
"@hapi/hoek@9.x.x", "@hapi/hoek@^9.0.0", "@hapi/hoek@^9.0.4":
18-
version "9.1.1"
19-
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.1.1.tgz#9daf5745156fd84b8e9889a2dc721f0c58e894aa"
20-
integrity sha512-CAEbWH7OIur6jEOzaai83jq3FmKmv4PmX1JYfs9IrYcGEVI/lyL1EXJGCj7eFVJ0bg5QR8LMxBlEtA+xKiLpFw==
17+
"@hapi/hoek@^10.0.1":
18+
"integrity" "sha512-CvlW7jmOhWzuqOqiJQ3rQVLMcREh0eel4IBnxDx2FAcK8g7qoJRQK4L1CPBASoCY6y8e6zuCy3f2g+HWdkzcMw=="
19+
"resolved" "https://registry.npmjs.org/@hapi/hoek/-/hoek-10.0.1.tgz"
20+
"version" "10.0.1"
21+
22+
"@hapi/hoek@^11.0.2":
23+
"integrity" "sha512-aKmlCO57XFZ26wso4rJsW4oTUnrgTFw2jh3io7CAtO9w4UltBNwRXvXIVzzyfkaaLRo3nluP/19msA8vDUUuKw=="
24+
"resolved" "https://registry.npmjs.org/@hapi/hoek/-/hoek-11.0.2.tgz"
25+
"version" "11.0.2"
26+
27+
"@hapi/hoek@^9.0.0":
28+
"integrity" "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ=="
29+
"resolved" "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz"
30+
"version" "9.3.0"
2131

2232
"@hapi/topo@^5.0.0":
23-
version "5.0.0"
24-
resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.0.0.tgz#c19af8577fa393a06e9c77b60995af959be721e7"
25-
integrity sha512-tFJlT47db0kMqVm3H4nQYgn6Pwg10GTZHb1pwmSiv1K4ks6drQOtfEF5ZnPjkvC+y4/bUPHK+bc87QvLcL+WMw==
33+
"integrity" "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg=="
34+
"resolved" "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz"
35+
"version" "5.1.0"
2636
dependencies:
2737
"@hapi/hoek" "^9.0.0"
2838

29-
"@hapi/wreck@^17.0.0":
30-
version "17.1.0"
31-
resolved "https://registry.yarnpkg.com/@hapi/wreck/-/wreck-17.1.0.tgz#fbdc380c6f3fa1f8052dc612b2d3b6ce3e88dbec"
32-
integrity sha512-nx6sFyfqOpJ+EFrHX+XWwJAxs3ju4iHdbB/bwR8yTNZOiYmuhA8eCe7lYPtYmb4j7vyK/SlbaQsmTtUrMvPEBw==
39+
"@hapi/wreck@^18.0.0":
40+
"integrity" "sha512-OLHER70+rZxvDl75xq3xXOfd3e8XIvz8fWY0dqg92UvhZ29zo24vQgfqgHSYhB5ZiuFpSLeriOisAlxAo/1jWg=="
41+
"resolved" "https://registry.npmjs.org/@hapi/wreck/-/wreck-18.0.1.tgz"
42+
"version" "18.0.1"
3343
dependencies:
34-
"@hapi/boom" "9.x.x"
35-
"@hapi/bourne" "2.x.x"
36-
"@hapi/hoek" "9.x.x"
37-
38-
"@sideway/address@^4.1.0":
39-
version "4.1.1"
40-
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.1.tgz#9e321e74310963fdf8eebfbee09c7bd69972de4d"
41-
integrity sha512-+I5aaQr3m0OAmMr7RQ3fR9zx55sejEYR2BFJaxL+zT3VM2611X0SHvPWIbAUBZVTn/YzYKbV8gJ2oT/QELknfQ==
44+
"@hapi/boom" "^10.0.1"
45+
"@hapi/bourne" "^3.0.0"
46+
"@hapi/hoek" "^11.0.2"
47+
48+
"@sideway/address@^4.1.3":
49+
"integrity" "sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw=="
50+
"resolved" "https://registry.npmjs.org/@sideway/address/-/address-4.1.4.tgz"
51+
"version" "4.1.4"
4252
dependencies:
4353
"@hapi/hoek" "^9.0.0"
4454

45-
"@sideway/formula@^3.0.0":
46-
version "3.0.0"
47-
resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c"
48-
integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg==
55+
"@sideway/formula@^3.0.1":
56+
"integrity" "sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg=="
57+
"resolved" "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.1.tgz"
58+
"version" "3.0.1"
4959

5060
"@sideway/pinpoint@^2.0.0":
51-
version "2.0.0"
52-
resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df"
53-
integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==
54-
55-
"@types/node@^14.14.25":
56-
version "14.14.25"
57-
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.25.tgz#15967a7b577ff81383f9b888aa6705d43fbbae93"
58-
integrity sha512-EPpXLOVqDvisVxtlbvzfyqSsFeQxltFbluZNRndIb8tr9KiBnYNLzrc1N3pyKUCww2RNrfHDViqDWWE1LCJQtQ==
59-
60-
"@types/simple-oauth2@^4.1.0":
61-
version "4.1.0"
62-
resolved "https://registry.yarnpkg.com/@types/simple-oauth2/-/simple-oauth2-4.1.0.tgz#0538d25909b3a28f2b4144fefbbcf3acec9b67d9"
63-
integrity sha512-FkAXBdBDFU6i2Rj+N/Sr5bZO8zrMnFnSvyzWtEUXgj7jdDZ0bcgxJ134ctwZbmNvSUzS4Qvt+NvszSbw8Zq+qg==
64-
65-
debug@^4.1.1:
66-
version "4.1.1"
67-
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
68-
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
61+
"integrity" "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ=="
62+
"resolved" "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz"
63+
"version" "2.0.0"
64+
65+
"@types/node@^18.14.0":
66+
"integrity" "sha512-5EWrvLmglK+imbCJY0+INViFWUHg1AHel1sq4ZVSfdcNqGy9Edv3UB9IIzzg+xPaUcAgZYcfVs2fBcwDeZzU0A=="
67+
"resolved" "https://registry.npmjs.org/@types/node/-/node-18.14.0.tgz"
68+
"version" "18.14.0"
69+
70+
"@types/simple-oauth2@^5.0.2":
71+
"integrity" "sha512-GoqbWy5SmiwGfBfASHcuzHqkBz/fzr0iPrlO41MqQfpGzjLzaqQDkjgghC4YgK1+SGX3pPx24urnlw89xqJ/eA=="
72+
"resolved" "https://registry.npmjs.org/@types/simple-oauth2/-/simple-oauth2-5.0.2.tgz"
73+
"version" "5.0.2"
74+
75+
"debug@^4.3.4":
76+
"integrity" "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ=="
77+
"resolved" "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
78+
"version" "4.3.4"
6979
dependencies:
70-
ms "^2.1.1"
80+
"ms" "2.1.2"
7181

72-
joi@^17.3.0:
73-
version "17.4.0"
74-
resolved "https://registry.yarnpkg.com/joi/-/joi-17.4.0.tgz#b5c2277c8519e016316e49ababd41a1908d9ef20"
75-
integrity sha512-F4WiW2xaV6wc1jxete70Rw4V/VuMd6IN+a5ilZsxG4uYtUXWu2kq9W5P2dz30e7Gmw8RCbY/u/uk+dMPma9tAg==
82+
"joi@^17.6.4":
83+
"integrity" "sha512-q5Fn6Tj/jR8PfrLrx4fpGH4v9qM6o+vDUfD4/3vxxyg34OmKcNqYZ1qn2mpLza96S8tL0p0rIw2gOZX+/cTg9w=="
84+
"resolved" "https://registry.npmjs.org/joi/-/joi-17.8.3.tgz"
85+
"version" "17.8.3"
7686
dependencies:
7787
"@hapi/hoek" "^9.0.0"
7888
"@hapi/topo" "^5.0.0"
79-
"@sideway/address" "^4.1.0"
80-
"@sideway/formula" "^3.0.0"
89+
"@sideway/address" "^4.1.3"
90+
"@sideway/formula" "^3.0.1"
8191
"@sideway/pinpoint" "^2.0.0"
8292

83-
ms@^2.1.1:
84-
version "2.1.2"
85-
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
86-
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
93+
"ms@2.1.2":
94+
"integrity" "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
95+
"resolved" "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
96+
"version" "2.1.2"
8797

88-
simple-oauth2@^4.2.0:
89-
version "4.2.0"
90-
resolved "https://registry.yarnpkg.com/simple-oauth2/-/simple-oauth2-4.2.0.tgz#8f7015f2ccd0ea6e94d35d8bd3abfa379bfba681"
91-
integrity sha512-AV62tGdq9JfLd/uveKpeNtQl+VVm89a35QKlwGuvisYIjCoz2ZmTGRGuSIGiYr+QUhSKJ5kYN1jq2BBa/ac/GQ==
98+
"simple-oauth2@~5.0.0":
99+
"integrity" "sha512-8291lo/z5ZdpmiOFzOs1kF3cxn22bMj5FFH+DNUppLJrpoIlM1QnFiE7KpshHu3J3i21TVcx4yW+gXYjdCKDLQ=="
100+
"resolved" "https://registry.npmjs.org/simple-oauth2/-/simple-oauth2-5.0.0.tgz"
101+
"version" "5.0.0"
92102
dependencies:
93-
"@hapi/hoek" "^9.0.4"
94-
"@hapi/wreck" "^17.0.0"
95-
debug "^4.1.1"
96-
joi "^17.3.0"
103+
"@hapi/hoek" "^10.0.1"
104+
"@hapi/wreck" "^18.0.0"
105+
"debug" "^4.3.4"
106+
"joi" "^17.6.4"

0 commit comments

Comments
 (0)