-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.js
More file actions
168 lines (142 loc) · 5.16 KB
/
rest.js
File metadata and controls
168 lines (142 loc) · 5.16 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const bcrypt = require('bcrypt');
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const winston = require('winston');
const morgan = require('morgan');
const { Etcd3 } = require('etcd3');
const models = require('./models');
const winstonFileTransportConfig = require('./config/winston.json');
const winstonFileTransport = new winston.transports.File(winstonFileTransportConfig);
let winstonTransports = [winstonFileTransport];
if (process.env.NODE_ENV === 'development') {
const winstonConsoleTransport = new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
});
winstonTransports.push(winstonConsoleTransport);
}
winston.emitErrs = true;
global.logger = new winston.Logger({transports: winstonTransports, exitOnError: false});
global.etcd3 = new Etcd3({hosts: process.env.ETCD_HOST});
let ready = false;
process.on('unhandledRejection', (error) => {
global.logger.error(error);
process.exit(1);
});
process.on('SIGINT', () => {
ready = false;
global.etcd3.close();
});
const rest = express();
rest.use(bodyParser.json());
rest.use(bodyParser.urlencoded({ extended: true }));
rest.use(morgan('combined', { stream: { write: message => global.logger.info(message) }}));
rest.get('/alive', (req, res) => {
res.sendStatus(200);
});
rest.get('/ready', (req, res) => {
ready ? res.sendStatus(200) : res.sendStatus(500);
});
rest.get('/bnetserver/login/:loginTicket', (req, res) => {
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.json({
"type": 1,
"inputs": [{
"input_id": "account_name",
"type": "text",
"label": "E-mail",
"max_length": 320
}, {
"input_id": "password",
"type": "password",
"label": "Password",
"max_length": 16
}, {
"input_id": "log_in_submit",
"type": "submit",
"label": "Log In"
}]
});
});
rest.post('/bnetserver/login/:loginTicket', (req, res) => {
let username = null;
let password = null;
res.setHeader('Content-Type', 'application/json');
req.body.inputs.forEach(function (input) {
if (input.input_id === 'account_name')
username = input.value;
if (input.input_id === 'password')
password = input.value;
});
if (req.params.hasOwnProperty('loginTicket')) {
let loginTicketPath = '/aurora/services/AuthenticationService/loginTickets/' + req.params.loginTicket;
let loginResult = {};
global.etcd3.get(loginTicketPath).string().then((loginTicket) => {
if (loginTicket) {
if (username && password) {
return models.Account.findOne({where: {email: username}}).then((account) => {
if (account) {
if(bcrypt.compareSync(password, account.hash)) {
let loginTicketLease = global.etcd3.lease(30);
return loginTicketLease.put(loginTicketPath + '/accountId').value(account.id).exec();
}
else {
return Promise.reject('Invalid password for account: ' + username);
}
}
else {
return Promise.reject('No account found: ' + username);
}
}).
then(() => {
loginResult.login_ticket = loginTicket;
return Promise.resolve('DONE');
}).
catch((error) => {
global.logger.error(error);
return Promise.resolve('LOGIN');
});
}
}
else {
return Promise.reject('No such login ticket exists');
}
}).
then((state) => {
loginResult.authentication_state = state;
res.json(loginResult);
}).
catch(() => {
res.sendStatus(500);
});
}
else {
res.sendStatus(400);
}
});
models.sequelize.sync().then(() => {
global.logger.debug('Database synced');
ready = true;
});
if(process.env.NODE_ENV === 'development') {
const https = require('https');
const fs = require('fs');
https.createServer({
key: fs.readFileSync('certs/tls.key'),
cert: fs.readFileSync('certs/tls.crt')
}, rest).listen(443);
global.etcd3.put('/aurora/services/AuthenticationService/WebAuthUrl').value('https://127.0.0.1:443/bnetserver/login/').then(() => {
global.logger.info('REST Service listening');
});
}
else {
http.createServer(rest).listen(80, () => {
//TODO add a method to dynamically retrieve the external address of the service
//global.etcd.set('/aurora/services/AuthenticationService/WebAuthUrl', 'https://127.0.0.1:443/bnetserver/login/');
global.logger.info('REST Service listening');
});
}