forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: cloudflare pages deploy * chore: cloudflare page functions * feat: use openvsx
- Loading branch information
Showing
8 changed files
with
167 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @file github auth callback | ||
* @author netcon | ||
*/ | ||
|
||
const createResponseHtml = (text: string, script: string) => ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Connect to GitHub</title> | ||
</head> | ||
<body> | ||
<h1>${text}</h1> | ||
<script>${script}</script> | ||
</body> | ||
</html> | ||
`; | ||
|
||
// return the data to the opener window by postMessage API, | ||
// and close current window if successfully connected | ||
const createAuthorizeResultHtml = (data: Record<any, any>, origins: string) => { | ||
const errorText = 'Failed! You can close this window and retry.'; | ||
const successText = 'Connected! You can now close this window.'; | ||
const resultStr = `{ type: 'authorizing', payload: ${JSON.stringify(data)} }`; | ||
const script = ` | ||
'${origins}'.split(',').forEach(function(allowedOrigin) { | ||
window.opener.postMessage(${resultStr}, allowedOrigin); | ||
}); | ||
${data.error ? '' : 'setTimeout(() => window.close(), 50);'}`; | ||
return createResponseHtml(data.error ? errorText : successText, script); | ||
}; | ||
|
||
const MISSING_CODE_ERROR = { | ||
error: 'request_invalid', | ||
error_description: 'Missing code', | ||
}; | ||
const UNKNOWN_ERROR = { | ||
error: 'internal_error', | ||
error_description: 'Unknown error', | ||
}; | ||
|
||
export const onRequest: PagesFunction<{ | ||
GITHUB_OAUTH_ID: string; | ||
GITHUB_OAUTH_SECRET: string; | ||
GITHUB1S_ALLOWED_ORIGINS: string; | ||
}> = async ({ request, env }) => { | ||
const code = new URL(request.url).searchParams.get('code'); | ||
|
||
const createResponse = (status, data) => { | ||
const body = createAuthorizeResultHtml(data, env.GITHUB1S_ALLOWED_ORIGINS); | ||
return new Response(body, { status }); | ||
}; | ||
|
||
if (!code) { | ||
return createResponse(401, MISSING_CODE_ERROR); | ||
} | ||
|
||
try { | ||
// https://docs.github.com/en/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github | ||
const response = await fetch('https://github.com/login/oauth/access_token', { | ||
method: 'POST', | ||
body: JSON.stringify({ client_id: env.GITHUB_OAUTH_ID, client_secret: env.GITHUB_OAUTH_SECRET, code }), | ||
}); | ||
return response.json().then((result) => createResponse(response.status, result)); | ||
} catch (e) { | ||
return createResponse(500, UNKNOWN_ERROR); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @file gitlab auth callback | ||
* @author netcon | ||
*/ | ||
|
||
const AUTH_REDIRECT_URI = 'https://auth.gitlab1s.com/api/gitlab-auth-callback'; | ||
|
||
const createResponseHtml = (text: string, script: string) => ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Connect to GitLab</title> | ||
</head> | ||
<body> | ||
<h1>${text}</h1> | ||
<script>${script}</script> | ||
</body> | ||
</html> | ||
`; | ||
|
||
// return the data to the opener window by postMessage API, | ||
// and close current window if successfully connected | ||
const createAuthorizeResultHtml = (data: Record<any, any>, origins: string) => { | ||
const errorText = 'Failed! You can close this window and retry.'; | ||
const successText = 'Connected! You can now close this window.'; | ||
const resultStr = `{ type: 'authorizing', payload: ${JSON.stringify(data)} }`; | ||
const script = ` | ||
'${origins}'.split(',').forEach(function(allowedOrigin) { | ||
window.opener.postMessage(${resultStr}, allowedOrigin); | ||
}); | ||
${data.error ? '' : 'setTimeout(() => window.close(), 50);'}`; | ||
return createResponseHtml(data.error ? errorText : successText, script); | ||
}; | ||
|
||
const MISSING_CODE_ERROR = { | ||
error: 'request_invalid', | ||
error_description: 'Missing code', | ||
}; | ||
const UNKNOWN_ERROR = { | ||
error: 'internal_error', | ||
error_description: 'Unknown error', | ||
}; | ||
|
||
export const onRequest: PagesFunction<{ | ||
GITLAB_OAUTH_ID: string; | ||
GITLAB_OAUTH_SECRET: string; | ||
GITLAB1S_ALLOWED_ORIGINS: string; | ||
}> = async ({ request, env }) => { | ||
const code = new URL(request.url).searchParams.get('code'); | ||
|
||
const createResponse = (status, data) => { | ||
const body = createAuthorizeResultHtml(data, env.GITLAB1S_ALLOWED_ORIGINS); | ||
return new Response(body, { status }); | ||
}; | ||
|
||
if (!code) { | ||
return createResponse(401, MISSING_CODE_ERROR); | ||
} | ||
|
||
try { | ||
// https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-flow | ||
const response = await fetch('https://gitlab.com/oauth/token', { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
code, | ||
client_id: env.GITLAB_OAUTH_ID, | ||
client_secret: env.GITLAB_OAUTH_SECRET, | ||
redirect_uri: AUTH_REDIRECT_URI, | ||
grant_type: 'authorization_code', | ||
}), | ||
}); | ||
return response.json().then((result) => createResponse(response.status, result)); | ||
} catch (e) { | ||
return createResponse(500, UNKNOWN_ERROR); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"include": ["**/*"], | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "esnext", | ||
"lib": ["esnext"], | ||
"types": ["@cloudflare/workers-types"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters