Skip to content

Commit cd0db44

Browse files
authored
Feature/oauth login (conwnet#302)
* feat: use github oauth-app authorizing flow * feat: use github oauth-app authorizing flow - dialog style * feat: add authorizing button to setttings view * feat: Web application flow * feat: serverless function for github oauth * feat: support authorizing github * feat: delete unused files * feat: update serveless postMessage origin * feat: delete unused files * feat: update serveless postMessage origin * feat: update version of vscode-web-github1s * feat: token save with refresh explorer * feat: update serveless postMessage origin
1 parent 0b89429 commit cd0db44

12 files changed

Lines changed: 435 additions & 47 deletions

File tree

api/github-auth-callback/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @file github auth callback
3+
* @author netcon
4+
*/
5+
6+
const got = require('got');
7+
8+
const CLIENT_ID = process.env.GITHUB_OAUTH_ID || '';
9+
const CLIENT_SECRET = process.env.GITHUB_OAUTH_SECRET || '';
10+
// allow origins should split by ','
11+
const ALLOWED_ORIGINS = process.env.ALLOWED_ORIGINS || '';
12+
13+
// return the data to the opener window by postMessage API,
14+
// and close current window then
15+
const getResponseHtml = (dataStr) => `
16+
<script>
17+
'${ALLOWED_ORIGINS}'.split(',').forEach(function(allowedOrigin) {
18+
window.opener.postMessage(${dataStr}, allowedOrigin);
19+
});
20+
setTimeout(() => window.close(), 50);
21+
</script>
22+
`;
23+
24+
const MISSING_CODE_ERROR = {
25+
error: 'request_invalid',
26+
error_description: 'Missing code',
27+
};
28+
const UNKNOWN_ERROR = {
29+
error: 'internal_error',
30+
error_description: 'Unknown error',
31+
};
32+
33+
module.exports = async (req, res) => {
34+
const code = req.query.code;
35+
const sendResponseHtml = (status, data) => {
36+
res.status(status);
37+
res.send(getResponseHtml(JSON.stringify(data)));
38+
};
39+
40+
if (!code) {
41+
return sendResponseHtml(401, MISSING_CODE_ERROR);
42+
}
43+
44+
try {
45+
// https://docs.github.com/en/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github
46+
const response = await got.post(
47+
'https://github.com/login/oauth/access_token',
48+
{
49+
json: { client_id: CLIENT_ID, client_secret: CLIENT_SECRET, code },
50+
responseType: 'json',
51+
}
52+
);
53+
return sendResponseHtml(response.statusCode, response.body);
54+
} catch (e) {
55+
// the error is responded by GitHub
56+
if (e.response) {
57+
return sendResponseHtml(e.response.statusCode, e.response.body);
58+
}
59+
return sendResponseHtml(500, UNKNOWN_ERROR);
60+
}
61+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "github-auth-callback",
3+
"version": "0.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"private": true,
7+
"dependencies": {
8+
"got": "^11.8.2"
9+
}
10+
}

api/github-auth-callback/yarn.lock

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@sindresorhus/is@^4.0.0":
6+
version "4.0.1"
7+
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-4.0.1.tgz#d26729db850fa327b7cacc5522252194404226f5"
8+
integrity sha512-Qm9hBEBu18wt1PO2flE7LPb30BHMQt1eQgbV76YntdNk73XZGpn3izvGTYxbGgzXKgbCjiia0uxTd3aTNQrY/g==
9+
10+
"@szmarczak/http-timer@^4.0.5":
11+
version "4.0.5"
12+
resolved "https://registry.yarnpkg.com/@szmarczak/http-timer/-/http-timer-4.0.5.tgz#bfbd50211e9dfa51ba07da58a14cdfd333205152"
13+
integrity sha512-PyRA9sm1Yayuj5OIoJ1hGt2YISX45w9WcFbh6ddT0Z/0yaFxOtGLInr4jUfU1EAFVs0Yfyfev4RNwBlUaHdlDQ==
14+
dependencies:
15+
defer-to-connect "^2.0.0"
16+
17+
"@types/cacheable-request@^6.0.1":
18+
version "6.0.1"
19+
resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.1.tgz#5d22f3dded1fd3a84c0bbeb5039a7419c2c91976"
20+
integrity sha512-ykFq2zmBGOCbpIXtoVbz4SKY5QriWPh3AjyU4G74RYbtt5yOc5OfaY75ftjg7mikMOla1CTGpX3lLbuJh8DTrQ==
21+
dependencies:
22+
"@types/http-cache-semantics" "*"
23+
"@types/keyv" "*"
24+
"@types/node" "*"
25+
"@types/responselike" "*"
26+
27+
"@types/http-cache-semantics@*":
28+
version "4.0.0"
29+
resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.0.tgz#9140779736aa2655635ee756e2467d787cfe8a2a"
30+
integrity sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A==
31+
32+
"@types/keyv@*":
33+
version "3.1.1"
34+
resolved "https://registry.yarnpkg.com/@types/keyv/-/keyv-3.1.1.tgz#e45a45324fca9dab716ab1230ee249c9fb52cfa7"
35+
integrity sha512-MPtoySlAZQ37VoLaPcTHCu1RWJ4llDkULYZIzOYxlhxBqYPB0RsRlmMU0R6tahtFe27mIdkHV+551ZWV4PLmVw==
36+
dependencies:
37+
"@types/node" "*"
38+
39+
"@types/node@*":
40+
version "15.0.2"
41+
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.0.2.tgz#51e9c0920d1b45936ea04341aa3e2e58d339fb67"
42+
integrity sha512-p68+a+KoxpoB47015IeYZYRrdqMUcpbK8re/zpFB8Ld46LHC1lPEbp3EXgkEhAYEcPvjJF6ZO+869SQ0aH1dcA==
43+
44+
"@types/responselike@*", "@types/responselike@^1.0.0":
45+
version "1.0.0"
46+
resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29"
47+
integrity sha512-85Y2BjiufFzaMIlvJDvTTB8Fxl2xfLo4HgmHzVBz08w4wDePCTjYw66PdrolO0kzli3yam/YCgRufyo1DdQVTA==
48+
dependencies:
49+
"@types/node" "*"
50+
51+
cacheable-lookup@^5.0.3:
52+
version "5.0.4"
53+
resolved "https://registry.yarnpkg.com/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz#5a6b865b2c44357be3d5ebc2a467b032719a7005"
54+
integrity sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==
55+
56+
cacheable-request@^7.0.1:
57+
version "7.0.1"
58+
resolved "https://registry.yarnpkg.com/cacheable-request/-/cacheable-request-7.0.1.tgz#062031c2856232782ed694a257fa35da93942a58"
59+
integrity sha512-lt0mJ6YAnsrBErpTMWeu5kl/tg9xMAWjavYTN6VQXM1A/teBITuNcccXsCxF0tDQQJf9DfAaX5O4e0zp0KlfZw==
60+
dependencies:
61+
clone-response "^1.0.2"
62+
get-stream "^5.1.0"
63+
http-cache-semantics "^4.0.0"
64+
keyv "^4.0.0"
65+
lowercase-keys "^2.0.0"
66+
normalize-url "^4.1.0"
67+
responselike "^2.0.0"
68+
69+
clone-response@^1.0.2:
70+
version "1.0.2"
71+
resolved "https://registry.yarnpkg.com/clone-response/-/clone-response-1.0.2.tgz#d1dc973920314df67fbeb94223b4ee350239e96b"
72+
integrity sha1-0dyXOSAxTfZ/vrlCI7TuNQI56Ws=
73+
dependencies:
74+
mimic-response "^1.0.0"
75+
76+
decompress-response@^6.0.0:
77+
version "6.0.0"
78+
resolved "https://registry.yarnpkg.com/decompress-response/-/decompress-response-6.0.0.tgz#ca387612ddb7e104bd16d85aab00d5ecf09c66fc"
79+
integrity sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==
80+
dependencies:
81+
mimic-response "^3.1.0"
82+
83+
defer-to-connect@^2.0.0:
84+
version "2.0.1"
85+
resolved "https://registry.yarnpkg.com/defer-to-connect/-/defer-to-connect-2.0.1.tgz#8016bdb4143e4632b77a3449c6236277de520587"
86+
integrity sha512-4tvttepXG1VaYGrRibk5EwJd1t4udunSOVMdLSAL6mId1ix438oPwPZMALY41FCijukO1L0twNcGsdzS7dHgDg==
87+
88+
end-of-stream@^1.1.0:
89+
version "1.4.4"
90+
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.4.tgz#5ae64a5f45057baf3626ec14da0ca5e4b2431eb0"
91+
integrity sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==
92+
dependencies:
93+
once "^1.4.0"
94+
95+
get-stream@^5.1.0:
96+
version "5.2.0"
97+
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-5.2.0.tgz#4966a1795ee5ace65e706c4b7beb71257d6e22d3"
98+
integrity sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==
99+
dependencies:
100+
pump "^3.0.0"
101+
102+
got@^11.8.2:
103+
version "11.8.2"
104+
resolved "https://registry.yarnpkg.com/got/-/got-11.8.2.tgz#7abb3959ea28c31f3576f1576c1effce23f33599"
105+
integrity sha512-D0QywKgIe30ODs+fm8wMZiAcZjypcCodPNuMz5H9Mny7RJ+IjJ10BdmGW7OM7fHXP+O7r6ZwapQ/YQmMSvB0UQ==
106+
dependencies:
107+
"@sindresorhus/is" "^4.0.0"
108+
"@szmarczak/http-timer" "^4.0.5"
109+
"@types/cacheable-request" "^6.0.1"
110+
"@types/responselike" "^1.0.0"
111+
cacheable-lookup "^5.0.3"
112+
cacheable-request "^7.0.1"
113+
decompress-response "^6.0.0"
114+
http2-wrapper "^1.0.0-beta.5.2"
115+
lowercase-keys "^2.0.0"
116+
p-cancelable "^2.0.0"
117+
responselike "^2.0.0"
118+
119+
http-cache-semantics@^4.0.0:
120+
version "4.1.0"
121+
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
122+
integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==
123+
124+
http2-wrapper@^1.0.0-beta.5.2:
125+
version "1.0.3"
126+
resolved "https://registry.yarnpkg.com/http2-wrapper/-/http2-wrapper-1.0.3.tgz#b8f55e0c1f25d4ebd08b3b0c2c079f9590800b3d"
127+
integrity sha512-V+23sDMr12Wnz7iTcDeJr3O6AIxlnvT/bmaAAAP/Xda35C90p9599p0F1eHR/N1KILWSoWVAiOMFjBBXaXSMxg==
128+
dependencies:
129+
quick-lru "^5.1.1"
130+
resolve-alpn "^1.0.0"
131+
132+
json-buffer@3.0.1:
133+
version "3.0.1"
134+
resolved "https://registry.yarnpkg.com/json-buffer/-/json-buffer-3.0.1.tgz#9338802a30d3b6605fbe0613e094008ca8c05a13"
135+
integrity sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==
136+
137+
keyv@^4.0.0:
138+
version "4.0.3"
139+
resolved "https://registry.yarnpkg.com/keyv/-/keyv-4.0.3.tgz#4f3aa98de254803cafcd2896734108daa35e4254"
140+
integrity sha512-zdGa2TOpSZPq5mU6iowDARnMBZgtCqJ11dJROFi6tg6kTn4nuUdU09lFyLFSaHrWqpIJ+EBq4E8/Dc0Vx5vLdA==
141+
dependencies:
142+
json-buffer "3.0.1"
143+
144+
lowercase-keys@^2.0.0:
145+
version "2.0.0"
146+
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-2.0.0.tgz#2603e78b7b4b0006cbca2fbcc8a3202558ac9479"
147+
integrity sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA==
148+
149+
mimic-response@^1.0.0:
150+
version "1.0.1"
151+
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"
152+
integrity sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ==
153+
154+
mimic-response@^3.1.0:
155+
version "3.1.0"
156+
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-3.1.0.tgz#2d1d59af9c1b129815accc2c46a022a5ce1fa3c9"
157+
integrity sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==
158+
159+
normalize-url@^4.1.0:
160+
version "4.5.0"
161+
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-4.5.0.tgz#453354087e6ca96957bd8f5baf753f5982142129"
162+
integrity sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==
163+
164+
once@^1.3.1, once@^1.4.0:
165+
version "1.4.0"
166+
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
167+
integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
168+
dependencies:
169+
wrappy "1"
170+
171+
p-cancelable@^2.0.0:
172+
version "2.1.1"
173+
resolved "https://registry.yarnpkg.com/p-cancelable/-/p-cancelable-2.1.1.tgz#aab7fbd416582fa32a3db49859c122487c5ed2cf"
174+
integrity sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==
175+
176+
pump@^3.0.0:
177+
version "3.0.0"
178+
resolved "https://registry.yarnpkg.com/pump/-/pump-3.0.0.tgz#b4a2116815bde2f4e1ea602354e8c75565107a64"
179+
integrity sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==
180+
dependencies:
181+
end-of-stream "^1.1.0"
182+
once "^1.3.1"
183+
184+
quick-lru@^5.1.1:
185+
version "5.1.1"
186+
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932"
187+
integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==
188+
189+
resolve-alpn@^1.0.0:
190+
version "1.1.2"
191+
resolved "https://registry.yarnpkg.com/resolve-alpn/-/resolve-alpn-1.1.2.tgz#30b60cfbb0c0b8dc897940fe13fe255afcdd4d28"
192+
integrity sha512-8OyfzhAtA32LVUsJSke3auIyINcwdh5l3cvYKdKO0nvsYSKuiLfTM5i78PJswFPT8y6cPW+L1v6/hE95chcpDA==
193+
194+
responselike@^2.0.0:
195+
version "2.0.0"
196+
resolved "https://registry.yarnpkg.com/responselike/-/responselike-2.0.0.tgz#26391bcc3174f750f9a79eacc40a12a5c42d7723"
197+
integrity sha512-xH48u3FTB9VsZw7R+vvgaKeLKzT6jOogbQhEe/jewwnZgzPcnyWui2Av6JpoYZF/91uueC+lqhWqeURw5/qhCw==
198+
dependencies:
199+
lowercase-keys "^2.0.0"
200+
201+
wrappy@1:
202+
version "1.0.2"
203+
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
204+
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=

extensions/github1s/src/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
commandValidateToken,
1010
commandUpdateToken,
1111
commandClearToken,
12+
commandAuthorizingGithub,
1213
} from './token';
1314
import { commandGetCurrentAuthority, commandCheckoutRef } from './ref';
1415
import {
@@ -48,6 +49,8 @@ const commands: { id: string; callback: (...args: any[]) => any }[] = [
4849
{ id: 'github1s.update-token', callback: commandUpdateToken },
4950
// clear GitHub OAuth Token
5051
{ id: 'github1s.clear-token', callback: commandClearToken },
52+
// authorizing github with `Web application flow`
53+
{ id: 'github1s.authorizing-github', callback: commandAuthorizingGithub },
5154

5255
// get current authority (`${owner}+${repo}+${ref}`)
5356
{ id: 'github1s.get-current-authority', callback: commandGetCurrentAuthority }, // prettier-ignore

0 commit comments

Comments
 (0)