This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
realms.js
97 lines (83 loc) · 2.79 KB
/
realms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var Realms = function(Gombot, SiteConfigs, Uri) {
var realms = [];
function buildRealms(siteConfigs) {
realms = siteConfigs.realms;
}
// fqOrigin cannot contain wildcards, realmOrigin may
// TODO: make more general to account for wildcards in realmOrigins
function fullyQualifiedOriginMatchesRealmOrigin(fqOrigin, realmOrigin) {
return fqOrigin === realmOrigin;
}
function getRealmWrapperForOrigin(fqOrigin) {
var length = realms.length;
for (var i=0;i<length;i++) {
if (isOriginMemberOfRealm(fqOrigin,realms[i].origins)) {
return realms[i];
}
}
return null;
}
// Determines a realm (array of origins, maybe with wildcards)
// for a fully qualified origin. This will consult the realm map and if it finds
// nothing, it will create the default realm for the origin (the origin wrapped in an array).
// fqOrigin must be fully qualified (no wildcards)
function getRealmForOrigin(fqOrigin) {
var realmWrapper = getRealmWrapperForOrigin(fqOrigin);
if (realmWrapper) return realmWrapper.origins;
return [ fqOrigin ];
}
// extracts an origin string from a uri string
function getOriginForUri(uriString) {
var uri = new Uri(uriString),
origin = uri.protocol()+"://"+uri.host(),
port = uri.port();
if (port) {
origin += ":"+port;
}
return origin;
}
function getRealmForUri(uriString) {
return getRealmForOrigin(getOriginForUri(uriString));
}
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getTitleFromUri(uri) {
var origin = getOriginForUri(uri);
var realmWrapper = getRealmWrapperForOrigin(origin);
if (realmWrapper && realmWrapper.title) return realmWrapper.title;
if (Gombot.TldService.isIpAddress(origin)) {
return Gombot.TldService.getFullHostnameWithPort(origin);
}
else {
return capitaliseFirstLetter(Gombot.TldService.getDomain(origin).split(".")[0]);
}
}
// a realm is defined by an array of origins, maybe some with wildcards
// fqOrigin must be fully qualified (no wildcards)
function isOriginMemberOfRealm(fqOrigin, origins) {
var i;
for (i=0;i<origins.length;i++) {
if (fullyQualifiedOriginMatchesRealmOrigin(fqOrigin,origins[i])) {
return true;
}
}
return false;
}
function isUriMemberOfRealm(uri, origins) {
return isOriginMemberOfRealm(getOriginForUri(uri), origins);
}
buildRealms(SiteConfigs);
return {
buildRealms: buildRealms,
getRealmForOrigin: getRealmForOrigin,
getOriginForUri: getOriginForUri,
getRealmForUri: getRealmForUri,
isUriMemberOfRealm: isUriMemberOfRealm,
getTitleFromUri: getTitleFromUri
};
}
if (typeof module !== "undefined" && module.exports) {
module.exports = Realms;
}