TrustedServers.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import Uri from "urijs";
  2. import defined from "./defined.js";
  3. import DeveloperError from "./DeveloperError.js";
  4. /**
  5. * A singleton that contains all of the servers that are trusted. Credentials will be sent with
  6. * any requests to these servers.
  7. *
  8. * @namespace TrustedServers
  9. *
  10. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  11. */
  12. const TrustedServers = {};
  13. let _servers = {};
  14. /**
  15. * Adds a trusted server to the registry
  16. *
  17. * @param {string} host The host to be added.
  18. * @param {number} port The port used to access the host.
  19. *
  20. * @example
  21. * // Add a trusted server
  22. * TrustedServers.add('my.server.com', 80);
  23. */
  24. TrustedServers.add = function (host, port) {
  25. //>>includeStart('debug', pragmas.debug);
  26. if (!defined(host)) {
  27. throw new DeveloperError("host is required.");
  28. }
  29. if (!defined(port) || port <= 0) {
  30. throw new DeveloperError("port is required to be greater than 0.");
  31. }
  32. //>>includeEnd('debug');
  33. const authority = `${host.toLowerCase()}:${port}`;
  34. if (!defined(_servers[authority])) {
  35. _servers[authority] = true;
  36. }
  37. };
  38. /**
  39. * Removes a trusted server from the registry
  40. *
  41. * @param {string} host The host to be removed.
  42. * @param {number} port The port used to access the host.
  43. *
  44. * @example
  45. * // Remove a trusted server
  46. * TrustedServers.remove('my.server.com', 80);
  47. */
  48. TrustedServers.remove = function (host, port) {
  49. //>>includeStart('debug', pragmas.debug);
  50. if (!defined(host)) {
  51. throw new DeveloperError("host is required.");
  52. }
  53. if (!defined(port) || port <= 0) {
  54. throw new DeveloperError("port is required to be greater than 0.");
  55. }
  56. //>>includeEnd('debug');
  57. const authority = `${host.toLowerCase()}:${port}`;
  58. if (defined(_servers[authority])) {
  59. delete _servers[authority];
  60. }
  61. };
  62. function getAuthority(url) {
  63. const uri = new Uri(url);
  64. uri.normalize();
  65. // Removes username:password@ so we just have host[:port]
  66. let authority = uri.authority();
  67. if (authority.length === 0) {
  68. return undefined; // Relative URL
  69. }
  70. uri.authority(authority);
  71. if (authority.indexOf("@") !== -1) {
  72. const parts = authority.split("@");
  73. authority = parts[1];
  74. }
  75. // If the port is missing add one based on the scheme
  76. if (authority.indexOf(":") === -1) {
  77. let scheme = uri.scheme();
  78. if (scheme.length === 0) {
  79. scheme = window.location.protocol;
  80. scheme = scheme.substring(0, scheme.length - 1);
  81. }
  82. if (scheme === "http") {
  83. authority += ":80";
  84. } else if (scheme === "https") {
  85. authority += ":443";
  86. } else {
  87. return undefined;
  88. }
  89. }
  90. return authority;
  91. }
  92. /**
  93. * Tests whether a server is trusted or not. The server must have been added with the port if it is included in the url.
  94. *
  95. * @param {string} url The url to be tested against the trusted list
  96. *
  97. * @returns {boolean} Returns true if url is trusted, false otherwise.
  98. *
  99. * @example
  100. * // Add server
  101. * TrustedServers.add('my.server.com', 81);
  102. *
  103. * // Check if server is trusted
  104. * if (TrustedServers.contains('https://my.server.com:81/path/to/file.png')) {
  105. * // my.server.com:81 is trusted
  106. * }
  107. * if (TrustedServers.contains('https://my.server.com/path/to/file.png')) {
  108. * // my.server.com isn't trusted
  109. * }
  110. */
  111. TrustedServers.contains = function (url) {
  112. //>>includeStart('debug', pragmas.debug);
  113. if (!defined(url)) {
  114. throw new DeveloperError("url is required.");
  115. }
  116. //>>includeEnd('debug');
  117. const authority = getAuthority(url);
  118. if (defined(authority) && defined(_servers[authority])) {
  119. return true;
  120. }
  121. return false;
  122. };
  123. /**
  124. * Clears the registry
  125. *
  126. * @example
  127. * // Remove a trusted server
  128. * TrustedServers.clear();
  129. */
  130. TrustedServers.clear = function () {
  131. _servers = {};
  132. };
  133. export default TrustedServers;