forked from des-des/oauth-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
110 lines (102 loc) · 3.02 KB
/
server.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var Hapi = require('hapi');
var querystring = require('querystring');
var http = require('https');
require('env2')('config.env');
var port = process.env.PORT || 8000;
var server = new Hapi.Server();
server.connection({ port: port });
server.register(require("inert"), function(err){
if(err){
throw err;
}
});
var makeRequest = function(options, cb) {
var request = http.request(options, function(response) {
var body = '';
if (response.statusCode !== 200) {
}
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
cb(null, body);
});
});
request.on('error', function(error) {
console.error('request failed!');
cb(error);
});
request.write(options.body); //required to send the client_id, client_secret and temporary code
request.end();
};
server.route([{
path: '/getData',
method: 'GET',
handler: function(request, reply) {
var token = request.state.access_token; //retrieving access_token from the cookies
var options = {
hostname: 'api.github.com',
path: '/user',
method: 'GET',
body: "", //body of the options is empty to be able to use the makeRequest function
headers: {
'Authorization': 'token ' + token,
'User-Agent': 'github_oath'
}
};
makeRequest(options,function(err, response){
console.log(JSON.parse(response));
reply(response);
});
},
},{
path: '/login',
method: 'GET',
handler: function(request, reply) {
var params = {
client_id : process.env.GITHUB_CLIENT_ID,
redirect_uri : process.env.BASE_URL + '/welcome'
};
reply.redirect(
'https://github.com/login/oauth/authorize?'+ querystring.stringify(params)
);
},
}, {
path:'/welcome',
method: 'GET',
handler: function(request, reply) {
console.log(request.url.query.code);
var payload = querystring.stringify({
client_id: process.env.GITHUB_CLIENT_ID, //given to you when you register your app with github
client_secret: process.env.GITHUB_CLIENT_SECRET, //given to you when you register your app with github
code: request.query.code // temporary code from github in query of redirect
});
makeRequest({
hostname : 'github.com',
path : '/login/oauth/access_token',
method : 'POST',
port : '443',
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length': payload.length
},
body: payload
}, function(err, response) {
if (err) {
throw err;
}
var token = JSON.parse(response).access_token;
console.log(token);
reply.file("./public/index.html").state("access_token", token); //sets a cookie under the name "access_token"
});
}
}]);
server.start(function(err) {
if (err) {
console.error(err);
} else {
console.log('server listening on port ' + port);
}
});
module.exports = server;