-
Notifications
You must be signed in to change notification settings - Fork 1
/
sid_unifi.js
131 lines (109 loc) · 3.22 KB
/
sid_unifi.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*jshint esversion: 6 */
var parameters = {
// set your captive portal login url here. Ex: "https://wifi.socialidnow.com/portals/cbt-ubnt-unifi-lab"
sidPortal: null,
// html element to display errors
errorsContainerName: "error_message"
}
var SidUnifi = function(parameters) {
var ErrorViewer = function(containerName) {
var container;
if (containerName) {
container = document.getElementById(containerName);
}
function showError(message) {
if (container) {
container.innerHTML = message;
}
}
return {
showError: showError
}
}
function getQueryParams(parameterName) {
var result = null,
tmp = [];
var items = location.search.substr(1).split("&");
for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
}
function serialize(params) {
return Object.entries(params).
filter(function([key, val]) { return val; }).
map(function([key, val]) { return key + '=' + encodeURIComponent(val); }).
join('&');
}
function buildLoginPath() {
var params = {
ap: getQueryParams("ap"),
id: getQueryParams("id"),
ssid: getQueryParams("ssid"),
url: getQueryParams("url"),
t: getQueryParams("t"),
login_url: 'http://' + window.location.host + window.location.pathname + 'signin.html'
};
return "/auth?" + serialize(params);
}
function redirectToPortal() {
if (!parameters.sidPortal) {
errorViewer.showError("Please set your sidPortal parameter in sid_unifi.js file");
return;
}
var path = buildLoginPath();
window.location.href = parameters.sidPortal + path;
}
function performRadiusLogin() {
var data = getQueryParams("data");
// validate parameters
if (!parameters.sidPortal) {
errorViewer.showError("Please set your sidPortal parameter in sid_unifi.js file");
return;
}
if (!data) {
errorViewer.showError("Missing required parameter: data");
return;
}
try {
data = JSON.parse(atob(data));
} catch (err) {
errorViewer.showError("Invalid required parameter: data");
return;
}
// create xhr request
var payload = {
by: 'radius',
username: data.username,
password: data.password,
};
var redirect = parameters.sidPortal + "#/fail";
var xhr = new XMLHttpRequest();
// configure xhr request
xhr.open("POST", "login", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.onload = function(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var resp = JSON.parse(xhr.responseText);
if (resp.data.length > 0) {
redirect = data.continue_url; //success
}
window.location = redirect;
}
}
};
// perform xhr request
try {
var resp = xhr.send(JSON.stringify(payload));
} catch (err) {
window.location = redirect;
}
}
var errorViewer = new ErrorViewer(parameters.errorsContainerName);
return {
redirectToPortal: redirectToPortal,
performRadiusLogin: performRadiusLogin
}
}