-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.js
executable file
·227 lines (193 loc) · 6.42 KB
/
authentication.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Functions to handle user authentication, login, logout, etc
* @author Chad Auld
*/
function initAuthentication(){
try {
var user = getAuthenticatedUser();
if (user) {
Jaxer.clientData.isAuthenticated = true;
}
else {
Jaxer.clientData.isAuthenticated = false;
}
}
catch (e) {
Jaxer.Log.error("Error trying to get authenticated user: " + e);
}
}
function changeAuthentication(isAuthenticated){
if (YAHOO.lang.isFunction(window.onAuthenticateChange)) {
onAuthenticateChange(isAuthenticated);
}
}
changeAuthentication.runat = "client";
/**
* Returns authenticated user object and/or null if user not authenticated
*/
function getAuthenticatedUser(){
var username = Jaxer.session.username;
if (typeof username === 'undefined') {
return null;
}
else {
var userModel = getModel("User");
var user = userModel.findByUsername(username);
if (typeof user === 'undefined') {
return null;
}
}
return user;
}
getAuthenticatedUser.proxy = true;
/**
* Sets Jaxer session variable with authenticated user information
* @param {Object} user
*/
function makeAuthenticated(user){
Jaxer.session.username = user.username;
}
/**
* Verifies users credentials against the database. Used
* by the login function.
* @param {Object} username
* @param {Object} password
*/
function checkCredentials(username, password){
var userModel = getModel("User");
var user = userModel.find({
where: {
username: username,
password: password
},
limit: 1
});
if (typeof user[0] !== 'object' || typeof user[0].username === 'undefined') {
return null;
}
makeAuthenticated(user[0]);
return user[0].username;
}
checkCredentials.proxy = true;
/**
* Destroy Jaxer session variables dealing with
* authenticated user. Used in the logout process.
*/
function removeCredentials(){
delete Jaxer.session.username;
}
removeCredentials.proxy = true;
/**
* Log authenticated user out of the system, change their status,
* and destory their related session information.
*/
function logout(){
var chatroomPage = YAHOO.util.Dom.getElementsByClassName('chatroom-page');
YAHOO.util.Dom.setStyle(chatroomPage, 'display', 'none');
var loginPage = YAHOO.util.Dom.getElementsByClassName('login-page');
YAHOO.util.Dom.setStyle(loginPage, 'display', 'block');
YAHOO.util.Dom.get('username').focus();
changeAuthentication(false);
removeCredentials();
}
logout.runat = "client";
/**
* Checks user credentials, sets up the authenticated session,
* and modifes visual display on successful login.
*/
function login(){
var username = YAHOO.lang.trim(YAHOO.util.Dom.get('username').value);
var password = YAHOO.lang.trim(YAHOO.util.Dom.get('password').value);
//Validation checks
var usernameCheck = validateUsername(username);
var passwordCheck = validatePassword(password);
if (usernameCheck !== '') {
YAHOO.util.Dom.get('loginMessage').innerHTML = usernameCheck;
return false;
}
else
if (passwordCheck !== '') {
YAHOO.util.Dom.get('loginMessage').innerHTML = passwordCheck;
return false;
}
var encryptedPassword = encryptPassword(password);
var dbUsername = checkCredentials(username, encryptedPassword);
if (dbUsername) {
var loginPage = YAHOO.util.Dom.getElementsByClassName('login-page');
YAHOO.util.Dom.setStyle(loginPage, 'display', 'none');
changeAuthentication(true);
//Cleanup the password and any old error messages so they not present on signout
YAHOO.util.Dom.get('loginMessage').innerHTML = '';
YAHOO.util.Dom.get('password').value = '';
jChat.postLoginInit();
}
else {
YAHOO.util.Dom.get('loginMessage').innerHTML = 'Invalid login! Please try again.';
return false;
}
}
login.runat = "client";
/**
* Use to create new accounts for the application and verify
* the data meets predefined formatting conditions.
*/
function doCreateAccount(){
var username = YAHOO.lang.trim(YAHOO.util.Dom.get('new_username').value);
var password = YAHOO.lang.trim(YAHOO.util.Dom.get('new_password').value);
var confirmPassword = YAHOO.lang.trim(YAHOO.util.Dom.get('confirm_password').value);
//Validation checks
var usernameCheck = validateUsername(username);
var passwordCheck = validatePassword(password);
if (usernameCheck !== '') {
YAHOO.util.Dom.get('newAccountMessage').innerHTML = usernameCheck;
return false;
}
else
if (usernameExists(username) === true) {
YAHOO.util.Dom.get('newAccountMessage').innerHTML = "Username already in use! Please try another.";
return false;
}
else
if (passwordCheck !== '') {
YAHOO.util.Dom.get('newAccountMessage').innerHTML = passwordCheck;
return false;
}
else
if (password !== confirmPassword) {
YAHOO.util.Dom.get('newAccountMessage').innerHTML = "Passwords do not match! Please try again.";
return false;
}
var newUserResult = addNewUser(username, password);
if (newUserResult) {
//Cleanup the fields so they are not present on signout/create during the initial session
YAHOO.util.Dom.get('new_username').value = '';
YAHOO.util.Dom.get('new_password').value = '';
YAHOO.util.Dom.get('confirm_password').value = '';
YAHOO.util.Dom.get('loginMessage').innerHTML = 'Account created successfully! Please login.';
YAHOO.util.Dom.setStyle('registration-form', 'display', 'none');
YAHOO.util.Dom.setStyle('login-form', 'display', 'block');
}
else {
alert('Unable to create new user. Please try again!');
return false;
}
}
doCreateAccount.runat = "client";
/**
* Validates the entered username
* @param {Object} candidate
*/
function validateUsername(candidate){
var pattern = /^[\w\-]+$/;
return pattern.test(candidate) ? "" : "Username must be alphanumeric or -";
}
validateUsername.runat = "both";
/**
* Validates that the entered password
* @param {Object} candidate
*/
function validatePassword(candidate){
var pattern = /^[\w\-\$]{6,}$/;
return pattern.test(candidate) ? "" : "Password must be 6 or more alphanumeric or - or $ characters";
}
validatePassword.runat = "both";