123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- define([], function () {
- var DomainValidator = function () {
- function DomainValidator(configValue) {
- _classCallCheck(this, DomainValidator);
- this._validDomainListInitialized = false;
- this._validDomainList = null;
- this._validDomains = '';
- this.init(configValue);
- }
- DomainValidator.prototype.init = function init(configValue) {
- var _this = this;
- this._validDomainListInitialized = true;
- if (configValue && configValue !== 'myDefault') {
- this._validDomains = configValue;
- this._validDomainList = [];
- configValue.toLowerCase().split(',').forEach(function (host) {
- var hostAndPort = host.trim().split(':');
- var hostname = hostAndPort[0];
- var allowAllSubDomains = false,
- allowFirstSubDomain = false;
- if (hostname.indexOf('*.') === 0) {
- allowAllSubDomains = true;
- allowFirstSubDomain = true;
- hostname = hostname.substring(2);
- } else if (hostname.indexOf('.') === 0) {
- allowFirstSubDomain = true;
- hostname = hostname.substring(1);
- }
- _this._validDomainList.push({
- hostname: hostname,
- port: hostAndPort[1],
- allowFirstSubDomain: allowFirstSubDomain,
- allowAllSubDomains: allowAllSubDomains
- });
- });
- }
- };
- DomainValidator.prototype.isAllowedDomain = function isAllowedDomain(url) {
- if (!url || !this._validDomainListInitialized) {
- return false;
- }
- if (!this._validDomainList) {
-
- return true;
- }
- var findHost = url.toLowerCase() + '/';
- var hostname = void 0,
- port = void 0,
- hasPort = false;
- if (findHost.indexOf('://') !== -1) {
- findHost = findHost.substring(findHost.indexOf('://') + 3);
- findHost = findHost.substring(0, findHost.indexOf('/'));
- if (findHost.indexOf('@') !== -1) {
-
- findHost = findHost.substring(findHost.indexOf('@') + 1);
- }
- findHost = findHost.split(':');
- hostname = findHost[0];
- port = findHost[1];
- } else {
- hostname = window.location.hostname;
- port = window.location.port;
- }
- hasPort = !!port;
- return !!this._validDomainList.find(function (validDomainEntry) {
-
- if (validDomainEntry.port && (!hasPort || validDomainEntry.port !== port)) {
- return false;
- }
- if (validDomainEntry.hostname === hostname) {
- return true;
- } else if (validDomainEntry.allowFirstSubDomain) {
- var baseHost = '.' + validDomainEntry.hostname;
- if (hostname.substring(hostname.length - baseHost.length, hostname.length) !== baseHost) {
- return false;
- }
- if (validDomainEntry.allowAllSubDomains) {
-
- return true;
- }
- var subdomain = hostname.substring(0, hostname.length - baseHost.length);
- return subdomain.indexOf('.') === -1;
- }
- return false;
- });
- };
- DomainValidator.prototype.getAllowedDomains = function getAllowedDomains() {
- return this._validDomains;
- };
- DomainValidator.prototype.isAllowedProtocol = function isAllowedProtocol(url) {
- if (!url) {
- return false;
- }
- url = url.toLowerCase();
-
-
-
-
-
- if (url.indexOf('https://') === 0 || url.indexOf('//') === 0) {
- return true;
- }
- return false;
- };
- return DomainValidator;
- }();
- return DomainValidator;
- });
|