-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
169 lines (140 loc) · 4.64 KB
/
login.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var login = (function (lightdm, $) {
// Initialize some variables
var username = null;
var password = null;
// Initialize the timer
password_interval_id = 0;
// Grab the fields
var $user = $('#user');
var $pass = $('#pass');
var $sess = $('#session');
// List the sessions
var setup_session_list = function () {
var $list = $sess;
var to_append = null;
$.each(lightdm.sessions, function (i) {
var key = lightdm.sessions[i].key;
var name = lightdm.sessions[i].name;
if(key == lightdm.default_session){
$list.append(
'<option selected="' +
key +
'">' +
key +
'</option>'
);
} else {
$list.append(
'<option value="' +
key +
'">' +
key +
'</option>'
);
}
});
};
// Authenticate
var auth = function() {
lightdm.cancel_timed_login();
window.start_authentication();
}
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
var updateSession = function() {
//So, we *could* start a ldm auth session here, now that a username has been selected.
//This seems incredibly wasteful though, because you might type in a username and never click submit
//instead, we just grab the session (if there is one) and update the select box
var username = $user.val() || null;
var session = lightdm.default_session;
if(username != null){
var url = 'http://ldap-api.collegiumv.org/' + username + '/desktopEnvironment/0';
var xhr = createCORSRequest('GET', url);
xhr.onload = function(){
session = xhr.responseText;
if (session == 'N'){
session = lightdm.default_session;
}
if(session != null){
$('#session').val(session);
}
}
xhr.onerror = function(){
$('#session').val(lightdm.default_session);
}
}
xhr.send();
//$('#error').append(session + '\n');
}
// Override the authentication so it grabs the username
window.start_authentication = function () {
username = $user.val() || null;
lightdm.start_authentication(username);
};
// Override the provide_secret so it grabs the password
window.provide_secret = function () {
password = $pass.val() || null;
//show_prompt(password);
if(password !== null) {
lightdm.provide_secret(password);
}
};
// Remove this. It's useless. I swear.
window.timed_login = function(user) {}
// Set the session properly and then start it
window.authentication_complete = function () {
if (lightdm.is_authenticated) {
show_prompt('Logged in');
session_list = document.getElementById('session');
session = lightdm.sessions[session_list.selectedIndex];
lightdm.login(lightdm.authentication_user, session.key);
} else {
lightdm.cancel_authentication();
$("#error").show();
}
};
// Show an error
window.show_error = function (e) {
console.log('Error: ' + e);
};
//Lightdm prompts for a password...this is how we know we can send the secret
window.show_prompt = function (e) {
if(password_interval_id > 0) clearInterval(password_interval_id);
password_interval_id = setInterval(function() {
window.provide_secret();} , 250);
};
var init = function () {
$(function () {
setup_session_list();
$('#error').hide();
$('#user').on('blur', function () {
updateSession();
});
// Bind auth to the form
$('form').on('submit', function (e) {
e.preventDefault();
auth();
});
});
};
return {
init: init
};
} (lightdm, jQuery));
//Start the task
login.init();