|
| 1 | +const express = require("express"); |
| 2 | +const app = express(); |
| 3 | +const fs = require('fs'); |
| 4 | +const https = require('https'); |
| 5 | +var path = require('path'); |
| 6 | +const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest |
| 7 | + |
| 8 | +const rabbitmq_url = process.env.RABBITMQ_URL; |
| 9 | +const proxied_rabbitmq_url = process.env.PROXIED_RABBITMQ_URL; |
| 10 | +const client_id = process.env.CLIENT_ID; |
| 11 | +const client_secret = process.env.CLIENT_SECRET; |
| 12 | +const uaa_url = process.env.UAA_URL; |
| 13 | +const port = process.env.PORT || 3000; |
| 14 | + |
| 15 | +app.engine('.html', require('ejs').__express); |
| 16 | +app.set('views', path.join(__dirname, 'views')); |
| 17 | +app.set('view engine', 'html'); |
| 18 | + |
| 19 | +app.get('/', function(req, res){ |
| 20 | + let id = default_if_blank(req.query.client_id, client_id) |
| 21 | + let secret = default_if_blank(req.query.client_secret, client_secret) |
| 22 | + if (id == 'undefined' || secret == 'undefined') { |
| 23 | + res.render('unauthenticated') |
| 24 | + }else { |
| 25 | + res.render('rabbitmq', { |
| 26 | + proxied_url: proxied_rabbitmq_url, |
| 27 | + url: rabbitmq_url.replace(/\/?$/, '/') + "login", |
| 28 | + name: rabbitmq_url + " for " + id, |
| 29 | + access_token: access_token(id, secret) |
| 30 | + }) |
| 31 | + } |
| 32 | +}) |
| 33 | + |
| 34 | +app.get('/favicon.ico', (req, res) => res.status(204)); |
| 35 | + |
| 36 | +app.get('/logout', function(req, res) { |
| 37 | + const redirectUrl = uaa_url + '/logout.do?client_id=' + client_id + "&redirect=https://portal:3000" |
| 38 | + console.debug("Received /logout request -> redirect to " + redirectUrl) |
| 39 | + res.redirect(redirectUrl); |
| 40 | +}) |
| 41 | + |
| 42 | +https |
| 43 | + .createServer( |
| 44 | + { |
| 45 | + cert: fs.readFileSync('/etc/portal/server_portal_certificate.pem'), |
| 46 | + key: fs.readFileSync('/etc/portal/server_portal_key.pem') |
| 47 | + }, |
| 48 | + app |
| 49 | + ) |
| 50 | + .listen(port) |
| 51 | + |
| 52 | +console.log('Express started on port ' + port); |
| 53 | + |
| 54 | +function default_if_blank(value, defaultValue) { |
| 55 | + if (typeof value === "undefined" || value === null || value == "") { |
| 56 | + return defaultValue; |
| 57 | + } else { |
| 58 | + return value; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function access_token(id, secret) { |
| 63 | + const req = new XMLHttpRequest(); |
| 64 | + const url = uaa_url + '/oauth/token'; |
| 65 | + const params = 'client_id=' + id + |
| 66 | + '&client_secret=' + secret + |
| 67 | + '&grant_type=client_credentials' + |
| 68 | + '&token_format=jwt' + |
| 69 | + '&response_type=token'; |
| 70 | + |
| 71 | + console.debug("Sending " + url + " with params "+ params); |
| 72 | + |
| 73 | + req.open('POST', url, false); |
| 74 | + req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
| 75 | + req.setRequestHeader('Accept', 'application/json'); |
| 76 | + req.send(params); |
| 77 | + if (req.status == 200) { |
| 78 | + const token = JSON.parse(req.responseText).access_token; |
| 79 | + console.log("Token => " + token) |
| 80 | + return token |
| 81 | + } else { |
| 82 | + throw new Error(req.status + " : " + " : " + |
| 83 | + req.response + " : " + req.responseText) |
| 84 | + } |
| 85 | +} |
0 commit comments