forked from Pomax/Usered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
executable file
·221 lines (196 loc) · 7.47 KB
/
user.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
/**
* Part of a framework for a simple user authentication.
*/
User = {
/**
* generates a random hex string
*/
randomString: function(len)
{
var hex = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
var string = "";
while(len-->0) { string += hex[parseInt(16*Math.random())]; }
return string; },
/**
* marks an input field as invalid/problematic
*/
markInvalid: function(input, reason) {
var classes = "";
if(input["class"]) { classes = input.getAttribute("class"); }
input.setAttribute("class", classes + " error");
input.title = reason;
return false; },
/**
* marks an input field as having passed validation
*/
markValid: function(input) {
if(input.getAttribute("class")) {
var stripped = input.getAttribute("class").replace("error", "");
input.setAttribute("class", stripped); }
input.title = "";
return true; },
/**
* user name validator
*/
validName: function(input)
{
var username = input.value;
if(username.trim()=="") { return this.markInvalid(input, "You forgot your user name."); }
if(username.indexOf("'")>-1) { return this.markInvalid(input, "Apostrophes are not allowed in user names."); }
if(username.length<4) { return this.markInvalid(input, "Sorry, user names must be more than 3 letters."); }
return this.markValid(input);
},
/**
* email address validator -- this uses the simplified email validation
* RegExp found on http://www.regular-expressions.info
*/
validEmail: function(input)
{
var valid = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(input.value);
if(!valid) { return this.markInvalid(input,"This is not a real email address..."); }
return this.markValid(input);
},
/**
* checks whether the twice typed password is the same
*/
passwordMatch: function(input1, input2)
{
var matched = (input1.value==input2.value);
if(!matched) { return this.markInvalid(input2, "The two passwords don't match"); }
return this.markValid(input2);
},
/**
* Checks whether there is a password set
*/
validPassword: function(input)
{
var password = input.value;
if(password.trim()=="") { return this.markInvalid(input, "You need to fill in a password"); }
return this.markValid(input);
},
/**
* Checks whether the password is strong enough.
*/
strongPassword: function(input)
{
var password = input.value;
if(!this.validPassword(input)) { return false; }
// you want to mofidy the following line to suit your personal preference in
// secure passwords. And remember that any policy you set has to work in an
// international setting. Passwords can contain any Unicode character, and
// are case sensitive. Don't rely on space-separated words, because several
// big languages don't use spaces. Don't demand "numbers and letters" because
// that just confuses your users. If you want to enforce strong passwords,
// calculate how easy it is to guess the password, and report how quickly
// you can figure out their password so that they pick a better one.
if(password.length<8) { return this.markInvalid(input, "Your password is too easy to guess, please pick something longer. Use an entire sentence. if you like."); }
return this.markValid(input);
},
/**
* Validate all values used for user registration, before submitting the form.
*
* NOTE: while this function does front-end validation, it is possible to bypass
* this function using a javascript console. So, in addition to this client-side
* validation the server will also be performing validation once it receives the data
*/
processRegistration: function()
{
var valid = true;
var form = document.getElementById('registration');
valid &= this.validName(form["username"]);
valid &= this.validEmail(form["email"]);
valid &= this.passwordMatch(form["password1"], form["password2"]);
valid &= this.strongPassword(form["password1"]);
if(valid) {
form["sha1"].value = Sha1.hash(form["password1"].value);
form["password1"].value = this.randomString(16);
form["password2"].value = form["password1"].value;
form.submit(); }
},
/**
* Validate all values used for user log in, before submitting the form.
*
* NOTE: while this function does front-end validation, it is possible to bypass
* this function using a javascript console. So, in addition to this client-side
* validation the server will also be performing validation once it receives the data
*/
processLogin: function()
{
var valid = true;
var form = document.getElementById('login');
valid &= this.validName(form["username"]);
valid &= this.validPassword(form["password1"]);
if(valid) {
form["sha1"].value = Sha1.hash(form["password1"].value);
form["password1"].value = this.randomString(16);
form.submit(); }
},
/**
* Validate all values used for email/password updating, before submitting the form.
*
* NOTE: while this function does front-end validation, it is possible to bypass
* this function using a javascript console. So, in addition to this client-side
* validation the server will also be performing validation once it receives the data
*/
processUpdate: function()
{
var valid = true;
var update = false;
var form = document.getElementById('update');
// email?
if(form["email"].value.trim()!="") {
valid &= this.validEmail(form["email"]);
if(valid) update = true; }
// password?
if(form["password1"].value.trim()!="") {
valid &= this.passwordMatch(form["password1"], form["password2"]);
valid &= this.strongPassword(form["password1"]);
if(valid) {
form["sha1"].value = Sha1.hash(form["password1"].value);
form["password1"].value = this.randomString(16);
form["password2"].value = form["password1"].value;
update = true; }}
if(valid && update) { form.submit(); }
},
// ------------------------------------------------------------
/**
* A static shorthand function for appendChild
*/
add: function(p, c) { p.appendChild(c); },
/**
* A more useful function for creating HTML elements.
*/
make: function(tag, properties) {
var tag = document.createElement(tag);
if(properties !== null) {
for(property in properties) {
tag[property] = properties[property]; }}
return tag; },
/**
* Inject a generic login form into the element passed as "parent"
*/
injectLogin: function(parent) {
// eliminate the need to type "this." everywhere in the function
var add = this.add;
var make = this.make;
var form = this.make("form", {id: "usered_login_form", action: ".", method: "POST"});
add(form, make("label", {"for": "usered_username", innerHTML: "user name"}));
add(form, make("input", {id: "usered_username", type: "text"}));
add(form, make("label", {"for": "usered_password", innerHTML: "password"}));
add(form, make("input", {id: "usered_password", type: "password"}));
add(form, make("input", {id: "usered_login_button", type: "submit", value: "log in"}));
add(parent, form);
},
/**
* Inject a generic logout form into the element passed as "parent"
*/
injectLogout: function(parent) {
// eliminate the need to type "this." everywhere in the function
var add = this.add;
var make = this.make;
var form = make("form", {id: "usered_logout_form", action: ".", method: "POST"});
add(form, make("input", {type: "hidden", name: "op", value: "logout"}));
add(form, make("input", {id: "usered_logout_button", type: "submit", value: "log out"}));
add(parent, form)
}
};