proxy.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import MsRest from 'ms-rest-azure'
  15. import bodyParser from 'body-parser'
  16. import request from 'request'
  17. const forwardHeaders = ['authorization']
  18. module.exports = app => {
  19. const jsonParser = bodyParser.json()
  20. app.post('/azure-login', jsonParser, (req, res) => {
  21. MsRest.loginWithUsernamePassword(req.body.username, req.body.password, (err, credentials) => {
  22. if (err) {
  23. res.status(400).send(err)
  24. } else {
  25. res.send(credentials)
  26. }
  27. })
  28. })
  29. app.get('/proxy/*', (req, res) => {
  30. let url = req.url.substr('/proxy/'.length)
  31. let headers = {}
  32. forwardHeaders.forEach(headerName => {
  33. if (req.headers[headerName] !== null && req.headers[headerName] !== undefined) {
  34. headers[headerName] = req.headers[headerName]
  35. }
  36. })
  37. request({
  38. url,
  39. headers,
  40. }, (err, resp, body) => {
  41. if (!err) {
  42. res.send(body)
  43. } else {
  44. res.status(500).send(err)
  45. }
  46. })
  47. })
  48. }