-
Notifications
You must be signed in to change notification settings - Fork 876
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support gitlab personal repo (#550)
* feat: support gitlab personal repo (#480) * support gitlab personal repo * fix: variable should convert to string * feat: add connect to gitlab command * feat: support gitlab personal repo * feat: support gitlab personal repo * fix: gitlab private repo auth * fix: gitlab private repo auth * fix: gitlab private repo auth --------- Co-authored-by: wolfsilver <[email protected]>
- Loading branch information
1 parent
ec24e80
commit 081c786
Showing
62 changed files
with
4,573 additions
and
638 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,71 @@ | ||
/** | ||
* @file github auth callback | ||
* @author netcon | ||
*/ | ||
|
||
import got from 'got'; | ||
import type { VercelRequest, VercelResponse } from '@vercel/node'; | ||
|
||
const CLIENT_ID = process.env.GITHUB_OAUTH_ID || ''; | ||
const CLIENT_SECRET = process.env.GITHUB_OAUTH_SECRET || ''; | ||
// allow origins should split by ',' | ||
const ALLOWED_ORIGINS = process.env.GITHUB1S_ALLOWED_ORIGINS || ''; | ||
|
||
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>) => { | ||
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 = ` | ||
'${ALLOWED_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 default async (req: VercelRequest, res: VercelResponse) => { | ||
const code = req.query.code; | ||
const sendResponseHtml = (status, data) => { | ||
res.status(status); | ||
res.send(createAuthorizeResultHtml(data)); | ||
}; | ||
|
||
if (!code) { | ||
return sendResponseHtml(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 got.post('https://github.com/login/oauth/access_token', { | ||
json: { client_id: CLIENT_ID, client_secret: CLIENT_SECRET, code }, | ||
responseType: 'json', | ||
}); | ||
return sendResponseHtml(response.statusCode, response.body); | ||
} catch (e) { | ||
return sendResponseHtml(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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
{ | ||
"name": "github-auth-callback", | ||
"version": "0.0.0", | ||
"main": "index.js", | ||
"main": "index.ts", | ||
"license": "MIT", | ||
"private": true, | ||
"dependencies": { | ||
"got": "^11.8.5" | ||
}, | ||
"devDependencies": { | ||
"@vercel/node": "^3.0.20" | ||
} | ||
} |
Oops, something went wrong.