Guest User

Untitled

a guest
May 12th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. let rubyResponse = {};
  2.  
  3.  
  4. // Timestamp for log
  5. var date = new Date();
  6. var timeStamp = date.getTime();
  7.  
  8. //richieo l' ndirizzo ip al client
  9. var ip = req.headers['x-forwarded-for'] ||
  10. req.connection.remoteAddress ||
  11. req.socket.remoteAddress ||
  12. req.connection.socket.remoteAddress;;
  13.  
  14.  
  15. //crypto il timestamp da inviare al server ruby
  16. const shasum = crypto.createHash('sha1');
  17. const update = shasum.update(timeStamp.toString());
  18. const cryptoStamp = shasum.digest('hex');
  19. //creo un token con una scadenza
  20. const token = jwt.sign({ userId: req.body.userid }, config.secret, { expiresIn: '4m' }) // Create a token for client
  21.  
  22. //creo un oggetto json da inviare al server in Ruby
  23. const obj = {
  24. userid: req.body.userid,
  25. password: req.body.password,
  26. sessionId: cryptoStamp,
  27. data: timeStamp,
  28. ipUser: ip,
  29. active: token
  30.  
  31. }
  32.  
  33. //creo un Json di Option da passare come header alla chiamata al server
  34. var options = {
  35. host: '',
  36. port: 80,
  37. path: '/users/login',
  38. method: 'POST',
  39. headers: {
  40. 'Content-Type': 'application/x-www-form-urlencoded',
  41. //imposto la lungezza del dato di tipo buffer
  42. 'Content-Length': Buffer.byteLength(JSON.stringify(obj))
  43. }
  44. };
  45.  
  46.  
  47. //make a request to other server
  48. var req = http.request(options, (resp) => {
  49. let data = '';
  50.  
  51. resp.setEncoding('utf8');
  52. resp.on('data', (chunk) => {
  53. // console.log("body: " + chunk);
  54.  
  55. data += chunk;
  56. });
  57.  
  58. resp.on('end',() => {
  59. // console.log(data)
  60. rubyResponse = JSON.parse(data);
  61.  
  62. if(rubyResponse.authenticatedUser){
  63. const token = jwt.sign({ userId: rubyResponse.userid }, config.secret, { expiresIn: '4m' }) // Create a token for client
  64.  
  65. res.json({ success: true, message: 'Hai effettuato il login', token: token, user: { username: rubyResponse.userid } });
  66.  
  67. }else{
  68. res.json({
  69. "Autenticazione":rubyResponse.authenticatedUser,
  70. "Msg":"credenziali sbagliate"
  71. });
  72. }
  73.  
  74.  
  75.  
  76. })
  77.  
  78.  
  79. })
  80. //error handling from http request
  81. .on("error", (err) => {
  82. console.log("Error: " + err.message);
  83. res.json({
  84. "msg": err.message,
  85. "info": `server spento : ${err.message}`
  86. })
  87. });
  88.  
  89. //closing call end finish
  90. req.write(JSON.stringify(obj));
  91.  
  92. req.end();
Add Comment
Please, Sign In to add comment