|
1 | 1 | /* jshint node: true, esnext: true */ |
2 | 2 | "use strict"; |
3 | | -var router = require('express').Router(); |
4 | | -var request = require('request'); |
5 | | -var bodyParser = require('body-parser'); |
6 | | -var url = require('url'); |
7 | | - |
8 | | -module.exports = function(options) { |
9 | | - if (!options || !options.servers) { |
10 | | - return; |
| 3 | +var router = require("express").Router(); |
| 4 | +var request = require("request"); |
| 5 | +var bodyParser = require("body-parser"); |
| 6 | +var url = require("url"); |
| 7 | + |
| 8 | +module.exports = function (options) { |
| 9 | + if (!options || !options.servers) { |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + // The maximum size of the JSON data. |
| 14 | + let postSizeLimit = options.postSizeLimit || "1024"; |
| 15 | + |
| 16 | + let tokenServers = parseUrls(options.servers); |
| 17 | + tokenServers = validateServerConfig(tokenServers); |
| 18 | + |
| 19 | + router.use( |
| 20 | + bodyParser.json({ limit: postSizeLimit, type: "application/json" }) |
| 21 | + ); |
| 22 | + router.post("/", function (req, res, next) { |
| 23 | + let parameters = req.body; |
| 24 | + |
| 25 | + if (!parameters.url) { |
| 26 | + return res.status(400).send("No URL specified."); |
11 | 27 | } |
12 | 28 |
|
13 | | - // The maximum size of the JSON data. |
14 | | - let postSizeLimit = options.postSizeLimit || '1024'; |
15 | | - |
16 | | - let tokenServers = parseUrls(options.servers); |
17 | | - tokenServers = validateServerConfig(tokenServers); |
18 | | - |
19 | | - router.use(bodyParser.json({limit:postSizeLimit, type:'application/json'})); |
20 | | - router.post('/', function(req, res, next) { |
21 | | - let parameters = req.body; |
| 29 | + let targetUrl = parseUrl(parameters.url); |
| 30 | + if (!targetUrl || targetUrl.length === 0 || typeof targetUrl !== "string") { |
| 31 | + return res.status(400).send("Invalid URL specified."); |
| 32 | + } |
22 | 33 |
|
23 | | - if (!parameters.url) { |
24 | | - return res.status(400).send('No URL specified.'); |
25 | | - } |
| 34 | + let tokenServer = tokenServers[targetUrl]; |
| 35 | + if (!tokenServer) { |
| 36 | + return res.status(400).send("Unsupported URL specified."); |
| 37 | + } |
26 | 38 |
|
27 | | - let targetUrl = parseUrl(parameters.url); |
28 | | - if (!targetUrl || (targetUrl.length === 0) || (typeof targetUrl !== 'string')) { |
29 | | - return res.status(400).send('Invalid URL specified.'); |
| 39 | + request( |
| 40 | + { |
| 41 | + url: tokenServer.tokenUrl, |
| 42 | + method: "POST", |
| 43 | + headers: { |
| 44 | + "User-Agent": "TerriaJSESRITokenAuth" |
| 45 | + }, |
| 46 | + form: { |
| 47 | + username: tokenServer.username, |
| 48 | + password: tokenServer.password, |
| 49 | + f: "JSON" |
30 | 50 | } |
31 | | - |
32 | | - let tokenServer = tokenServers[targetUrl]; |
33 | | - if (!tokenServer) { |
34 | | - return res.status(400).send('Unsupported URL specified.'); |
| 51 | + }, |
| 52 | + function (error, response, body) { |
| 53 | + try { |
| 54 | + res.set("Content-Type", "application/json"); |
| 55 | + |
| 56 | + if (response.statusCode !== 200) { |
| 57 | + return res.status(502).send("Token server failed."); |
| 58 | + } else { |
| 59 | + let value = JSON.parse(response.body); |
| 60 | + return res.status(200).send(JSON.stringify(value)); |
| 61 | + } |
| 62 | + } catch (error) { |
| 63 | + return res.status(500).send("Error processing server response."); |
35 | 64 | } |
| 65 | + } |
| 66 | + ); |
| 67 | + }); |
36 | 68 |
|
37 | | - request({ |
38 | | - url: tokenServer.tokenUrl, |
39 | | - method: 'POST', |
40 | | - headers: { |
41 | | - 'User-Agent': 'TerriaJSESRITokenAuth', |
42 | | - }, |
43 | | - form:{ |
44 | | - username: tokenServer.username, |
45 | | - password: tokenServer.password, |
46 | | - f: 'JSON' |
47 | | - } |
48 | | - }, function(error, response, body) { |
49 | | - try { |
50 | | - res.set('Content-Type', 'application/json'); |
51 | | - |
52 | | - if (response.statusCode !== 200) { |
53 | | - return res.status(502).send('Token server failed.'); |
54 | | - } else { |
55 | | - let value = JSON.parse(response.body); |
56 | | - return res.status(200).send(JSON.stringify(value)); |
57 | | - } |
58 | | - } |
59 | | - catch (error) { |
60 | | - return res.status(500).send('Error processing server response.'); |
61 | | - } |
62 | | - }); |
63 | | - }); |
64 | | - |
65 | | - return router; |
| 69 | + return router; |
66 | 70 | }; |
67 | 71 |
|
68 | 72 | function parseUrls(servers) { |
69 | | - let result = {}; |
70 | | - |
71 | | - Object.keys(servers).forEach(server => { |
72 | | - let parsedUrl = parseUrl(server) |
73 | | - if (parsedUrl) { |
74 | | - result[parsedUrl] = servers[server]; |
75 | | - } |
76 | | - else { |
77 | | - console.error('Invalid configuration. The URL: \'' + server + '\' is not valid.'); |
78 | | - } |
79 | | - }); |
| 73 | + let result = {}; |
| 74 | + |
| 75 | + Object.keys(servers).forEach((server) => { |
| 76 | + let parsedUrl = parseUrl(server); |
| 77 | + if (parsedUrl) { |
| 78 | + result[parsedUrl] = servers[server]; |
| 79 | + } else { |
| 80 | + console.error( |
| 81 | + "Invalid configuration. The URL: '" + server + "' is not valid." |
| 82 | + ); |
| 83 | + } |
| 84 | + }); |
80 | 85 |
|
81 | | - return result; |
| 86 | + return result; |
82 | 87 | } |
83 | 88 |
|
84 | 89 | function parseUrl(urlString) { |
85 | | - try { |
86 | | - return url.format(url.parse(urlString)); |
87 | | - } |
88 | | - catch (error) { |
89 | | - return ''; |
90 | | - } |
| 90 | + try { |
| 91 | + return url.format(url.parse(urlString)); |
| 92 | + } catch (error) { |
| 93 | + return ""; |
| 94 | + } |
91 | 95 | } |
92 | 96 |
|
93 | | -function validateServerConfig(servers) |
94 | | -{ |
95 | | - let result = {}; |
96 | | - |
97 | | - Object.keys(servers).forEach(url => { |
98 | | - let server = servers[url]; |
99 | | - if (server.username && server.password && server.tokenUrl) { |
100 | | - result[url] = server; |
101 | | - |
102 | | - // Note: We should really only validate URLs that are HTTPS to save us from ourselves, but the current |
103 | | - // servers we need to support don't support HTTPS :( so the best that we can do is warn against it. |
104 | | - if (!isHttps(server.tokenUrl)) { |
105 | | - console.error('All communications should be TLS but the URL \'' + server.tokenUrl + '\' does not use https.'); |
106 | | - } |
107 | | - } else { |
108 | | - console.error('Bad Configuration. \'' + url + '\' does not supply all of the required properties.'); |
109 | | - } |
110 | | - }); |
| 97 | +function validateServerConfig(servers) { |
| 98 | + let result = {}; |
| 99 | + |
| 100 | + Object.keys(servers).forEach((url) => { |
| 101 | + let server = servers[url]; |
| 102 | + if (server.username && server.password && server.tokenUrl) { |
| 103 | + result[url] = server; |
| 104 | + |
| 105 | + // Note: We should really only validate URLs that are HTTPS to save us from ourselves, but the current |
| 106 | + // servers we need to support don't support HTTPS :( so the best that we can do is warn against it. |
| 107 | + if (!isHttps(server.tokenUrl)) { |
| 108 | + console.error( |
| 109 | + "All communications should be TLS but the URL '" + |
| 110 | + server.tokenUrl + |
| 111 | + "' does not use https." |
| 112 | + ); |
| 113 | + } |
| 114 | + } else { |
| 115 | + console.error( |
| 116 | + "Bad Configuration. '" + |
| 117 | + url + |
| 118 | + "' does not supply all of the required properties." |
| 119 | + ); |
| 120 | + } |
| 121 | + }); |
111 | 122 |
|
112 | | - return result; |
| 123 | + return result; |
113 | 124 | } |
114 | 125 |
|
115 | | -function isHttps(urlString){ |
116 | | - try { |
117 | | - return (url.parse(urlString).protocol === 'https:') |
118 | | - } |
119 | | - catch (error) |
120 | | - { |
121 | | - return false; |
122 | | - } |
| 126 | +function isHttps(urlString) { |
| 127 | + try { |
| 128 | + return url.parse(urlString).protocol === "https:"; |
| 129 | + } catch (error) { |
| 130 | + return false; |
| 131 | + } |
123 | 132 | } |
0 commit comments