Guest User

Untitled

a guest
Oct 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. //app stuff
  2. const client_id = "x";
  3. const client_secret = "y";
  4. const callback = "https://myurl;
  5.  
  6. // Basic Setup
  7. var http = require('http'),
  8. express = require('express'),
  9. mysql = require('mysql'),
  10. parser = require('body-parser'),
  11. Client = require('node-rest-client').Client;
  12.  
  13. var client = new Client();
  14.  
  15. // Setup express
  16. var app = express();
  17. app.use(parser.json());
  18. app.use(parser.urlencoded({ extended: true }));
  19. app.set('port', process.env.PORT || 5000);
  20.  
  21. // Set default route
  22. app.get('/', function (req, res) {
  23. res.send('<html><body><p>Welcome to Bank API Wrapper</p></body></html>');
  24. });
  25.  
  26. app.post('/authorize', function (req,res) {
  27. var response = [];
  28.  
  29. if (typeof req.body.code !== 'undefined' && typeof req.body.state !== 'undefined' ){
  30. var code = req.body.code, state = req.body.state;
  31.  
  32. //conversion to base64 because citi api wants it this way
  33. var authorization = "Basic " + Buffer.from(client_id + ":" + client_secret).toString('base64');
  34.  
  35. var args = {
  36. data:{"grant_type":"authorization_code","code":code,"redirect_uri":callback},
  37. headers:{"Authorization":authorization,"Content-Type":"application/x-www-form-urlencoded"}
  38. };
  39.  
  40. //get access and refresh token
  41. client.post("https://sandbox.apihub.citi.com/gcb/api/authCode/oauth2/token/sg/gcb", args, function (citidata, citiresponse) {
  42. //console.log(citidata);
  43. //console.log(citiresponse);
  44. });
  45.  
  46. client.on('error', function (err) {
  47. response.push({'result' : 'error', 'msg' : 'unauthorized access'});
  48. res.setHeader('Content-Type', 'application/json');
  49. res.status(200).send(JSON.stringify(response));
  50. });
  51. }
  52. else {
  53. response.push({'result' : 'error', 'msg' : 'Please fill required details'});
  54. res.setHeader('Content-Type', 'application/json');
  55. res.status(200).send(JSON.stringify(response));
  56. }
  57. });
  58.  
  59.  
  60. // Create server
  61. http.createServer(app).listen(app.get('port'), function(){
  62. console.log('Server listening on port ' + app.get('port'));
  63. });
Add Comment
Please, Sign In to add comment