-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
43 lines (39 loc) · 1.38 KB
/
main.js
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
var http = require('http');
var https = require('https');
var querystring = require('querystring');
var env = require('env2')('./config.env');
var sessions = {};
function handler (req, res){
if (req.url === '/'){
res.end('<a href=https://github.com/login/oauth/authorize?client_id=3704f4fe34f8b65350b3>DANGER!!! RESTRICTED AREA AUTHORISED ACCESS ONLY, ENTER AT OWN RISK</a>');
}else if(req.url.match('/login')){
loginHandler(req,res);
}
}
function loginHandler(req,res){
console.log('im in the loginhandler');
var code = req.url.split('code=')[1];
var postData = querystring.stringify({
client_id: process.env.client_id,
client_secret: process.env.client_secret,
code: code
});
https.request({
hostname: 'github.com',
path: '/login/oauth/access_token',
method: 'POST'
}, function(responseFromGithub) {
console.log('im in the github response:---->'+responseFromGithub);
responseFromGithub.on('data', function(chunk) {
var accessToken = chunk.toString().split('access_token=')[1].split('&')[0];
console.log(accessToken);
var cookie = Math.floor(Math.random() * 100000000);
sessions[cookie] = accessToken;
console.log(sessions);
res.writeHead(200, { "Set-Cookie": 'access=' + cookie });
res.end('Logged in');
});
}).end(postData);
}
http.createServer(handler).listen(2000);
console.log('listening on 2000');