-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
144 lines (125 loc) · 3.98 KB
/
server.js
File metadata and controls
144 lines (125 loc) · 3.98 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//import modules
var http = require('http');
var HttpDispatcher = require('httpdispatcher');
var request = require('request');
var url = require('url');
var config = require('./config.js');
var uuid = require('node-uuid');
var host = 'localhost';
var options = { //for github api
clientID: config.client_id,
secret: config.secret,
scope: '',
redirectURI: 'http://'+host+':8080/callback', //make sure this is the same as the callback URI in github
};
var state = uuid.v4(); //Random string
var dispatcher = new HttpDispatcher();
//main page
dispatcher.onGet("/", function(req, res) {
var data = "<!DOCTYPE html><html><body><a href='login'>Login to App</a></body></html>";
res.write(data);
res.end();
});
//login page
dispatcher.onGet("/login", function(req, res) {
var url = 'https://github.com/login/oauth/authorize'
+ '?client_id=' + options.clientID
+ (options.scope ? '&scope=' + options.scope : '')
+ '&redirect_uri=' + options.redirectURI
+ '&state=' + state
;
res.statusCode = 302;
res.setHeader('location', url);
res.end();
});
dispatcher.onGet("/callback", function(req, res) {
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
//returns something like { code: '6cd032d64f7b45f0d339', state: '10' }
if(query.state == state) { //supposed to be if states match
var arguments = {
code: query.code,
client_id: options.clientID,
client_secret: options.secret
};
request.post({url: 'https://github.com/login/oauth/access_token', formData: arguments, headers: {'Accept': 'application/json'}}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var token = JSON.parse(body).access_token;
res.statusCode = 302;
welcome(res, token);
}
});
}
else {
res.end('Sorry, an error occured.');
}
});
//print welcome statement
function welcome(res, token) {
request.get({url: "https://api.github.com/user", headers: {'Authorization': 'token '+token, 'User-Agent': 'Mozilla/5.0'}}, function(error, response, body) {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
var user = body.login;
checkMembership(user, token, res);
} else {
console.log(body);
}
});
function checkMembership(user, token, res) {
var checking = {
teamID: 1163900, //GCI Students Team for Fossasia
user: user
};
//options for the https request
var options = {
url: 'https://api.github.com/teams/'+checking.teamID+'/memberships/'+checking.user,
headers: {
'Authorization': 'token '+token,
'User-Agent': 'Mozilla/5.0'
}
};
request.get(options, function(error, response, body) {
var active;
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
console.log(body);
if(body.state == "active") {
active = true;
} else {
active = false;
}
printStatement(user, active, res);
} else {
console.log(body);
res.end("You don't have permission to check for team membership.");
}
});
}
function printStatement(name, isActive, res) {
if(isActive) {
res.write("<!DOCTYPE html><html><body><h1>Dashboard</h1>Welcome to your dashboard, "+name+"! You are part of the FOSSASIA GCI Student Team.</body></html>");
} else {
res.write("<!DOCTYPE html><html><body><h1>Dashboard</h1>Welcome to your dashboard, "+name+"! You are not part of the FOSSASIA GCI Student Team.</body></html>");
}
res.end();
}
}
//define port for listening for web server
const PORT = 8080;
//handle requests function
function handleRequest (request, response) {
try {
//log the request on console
console.log(request.url);
//Disptach
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
//create a server
var server = http.createServer(handleRequest);
//start the server on the port
server.listen(PORT, function () {
console.log("Server listening on: http://"+host+":%s", PORT);
});