forked from ChengGeng97/github-oauth-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
64 lines (52 loc) · 1.78 KB
/
server.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as fs from 'fs';
import * as path from 'path';
import * as url from 'url';
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
import bodyParser from 'body-parser';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const config = JSON.parse(fs.readFileSync(process.env.CONFIG_PATH || path.join(__dirname, 'config.json'), 'utf8'));
// Create application
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Set up the port
const PORT = process.env.port || 9000;
app.listen(PORT, () => console.log('Listening http://localhost:' + PORT));
app.use(cors({
methods: ['POST'],
origin: config.corsOrigin
}));
// Passes the code on to GitHub, then returns the access token
app.post('/get_access_token', async (req, res) => {
const clientSecret = config.clientSecrets[req.body.clientId];
const code = req.body.code;
if (!code || !clientSecret) {
res.status(400).end();
return;
}
const access_token = await getAccessToken(code, req.body.clientId, clientSecret);
if (!access_token) {
res.status(400).end();
return;
}
res.end(JSON.stringify({ access_token }));
});
// Exchanges the code with GitHub to receive the Access Token
async function getAccessToken(code, clientId, clientSecret) {
const request = await fetch(`https://github.com/login/oauth/access_token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
code
})
});
const data = await request.text();
const params = new URLSearchParams(data);
return params.get('access_token');
}