Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let rubyResponse = {};
- // Timestamp for log
- var date = new Date();
- var timeStamp = date.getTime();
- //richieo l' ndirizzo ip al client
- var ip = req.headers['x-forwarded-for'] ||
- req.connection.remoteAddress ||
- req.socket.remoteAddress ||
- req.connection.socket.remoteAddress;;
- //crypto il timestamp da inviare al server ruby
- const shasum = crypto.createHash('sha1');
- const update = shasum.update(timeStamp.toString());
- const cryptoStamp = shasum.digest('hex');
- //creo un token con una scadenza
- const token = jwt.sign({ userId: req.body.userid }, config.secret, { expiresIn: '4m' }) // Create a token for client
- //creo un oggetto json da inviare al server in Ruby
- const obj = {
- userid: req.body.userid,
- password: req.body.password,
- sessionId: cryptoStamp,
- data: timeStamp,
- ipUser: ip,
- active: token
- }
- //creo un Json di Option da passare come header alla chiamata al server
- var options = {
- host: '',
- port: 80,
- path: '/users/login',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- //imposto la lungezza del dato di tipo buffer
- 'Content-Length': Buffer.byteLength(JSON.stringify(obj))
- }
- };
- //make a request to other server
- var req = http.request(options, (resp) => {
- let data = '';
- resp.setEncoding('utf8');
- resp.on('data', (chunk) => {
- // console.log("body: " + chunk);
- data += chunk;
- });
- resp.on('end',() => {
- // console.log(data)
- rubyResponse = JSON.parse(data);
- if(rubyResponse.authenticatedUser){
- const token = jwt.sign({ userId: rubyResponse.userid }, config.secret, { expiresIn: '4m' }) // Create a token for client
- res.json({ success: true, message: 'Hai effettuato il login', token: token, user: { username: rubyResponse.userid } });
- }else{
- res.json({
- "Autenticazione":rubyResponse.authenticatedUser,
- "Msg":"credenziali sbagliate"
- });
- }
- })
- })
- //error handling from http request
- .on("error", (err) => {
- console.log("Error: " + err.message);
- res.json({
- "msg": err.message,
- "info": `server spento : ${err.message}`
- })
- });
- //closing call end finish
- req.write(JSON.stringify(obj));
- req.end();
Add Comment
Please, Sign In to add comment