Mercurial > rlmcintyre
diff BoosterPack/logintets/login_controller.js @ 0:0d795f02a8bb tip
initial committ. what was I thinking?
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 27 Sep 2010 16:57:26 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/BoosterPack/logintets/login_controller.js Mon Sep 27 16:57:26 2010 -0400 1.3 @@ -0,0 +1,98 @@ 1.4 +// js handling the login procedures 1.5 + 1.6 +// constants 1.7 +var NORMAL_STATE = 4; 1.8 +var LOGIN_PREFIX = 'login.php?'; 1.9 + 1.10 +// variables 1.11 +var http = getHTTPObject(); // We create the HTTP Object 1.12 +var hasSeed = false; 1.13 +var loggedIn = false; 1.14 +var seed_id = 0; 1.15 +var seed = 0; 1.16 +var fullname = ''; 1.17 +var messages = ''; 1.18 + 1.19 +// getSeed method: gets a seed from the server for this transaction 1.20 +function getSeed() 1.21 +{ // only get a seed if we're not logged in and we don't already have one 1.22 + if (!loggedIn && !hasSeed) { 1.23 + // open up the path 1.24 + http.open('GET', LOGIN_PREFIX + 'task=getseed', true); 1.25 + http.onreadystatechange = handleHttpGetSeed; 1.26 + http.send(null); 1.27 + } 1.28 +} 1.29 + 1.30 +// handleHttpGetSeed method: called when the seed is returned from the server 1.31 +function handleHttpGetSeed() 1.32 +{ 1.33 + // if there hasn't been any errors 1.34 + if (http.readyState == NORMAL_STATE) { 1.35 + // split by the divider | 1.36 + results = http.responseText.split('|'); 1.37 + 1.38 + // id is the first element 1.39 + seed_id = results[0]; 1.40 + 1.41 + // seed is the second element 1.42 + seed = results[1]; 1.43 + 1.44 + // now we have the seed 1.45 + hasSeed = true; 1.46 + } 1.47 +} 1.48 + 1.49 +// validateLogin method: validates a login request 1.50 +function validateLogin() 1.51 +{ 1.52 + // ignore request if we are already logged in 1.53 + if (loggedIn) 1.54 + return; 1.55 + 1.56 + // get form form elements 'username' and 'password' 1.57 + username = document.getElementById('username').value; 1.58 + password = document.getElementById('password').value; 1.59 + 1.60 + // ignore if either is empty 1.61 + if (username != '' && password != '') { 1.62 + // compute the hash of the hash of the password and the seed 1.63 + hash = hex_md5(hex_md5(password) + seed); 1.64 + 1.65 + // open the http connection 1.66 + http.open('GET', LOGIN_PREFIX + 'task=checklogin&username='+username+'&id='+seed_id+'&hash='+hash, true); 1.67 + 1.68 + // where to go 1.69 + http.onreadystatechange = handleHttpValidateLogin; 1.70 + http.send(null); 1.71 + } 1.72 +} 1.73 + 1.74 +// handleHttpValidateLogin method: called when the validation results are returned from the server 1.75 +function handleHttpValidateLogin() 1.76 +{ 1.77 + // did the connection work? 1.78 + if (http.readyState == NORMAL_STATE) { 1.79 + // split by the pipe 1.80 + results = http.responseText.split('|'); 1.81 + if (results[0] == 'true') 1.82 + { 1.83 + hasSeed = false; 1.84 + loggedIn = true; 1.85 + fullname = results[1]; 1.86 + messages = ''; 1.87 + } 1.88 + else 1.89 + { 1.90 + messages = results[1]; 1.91 + } 1.92 + showLogin(); 1.93 + } 1.94 +} 1.95 + 1.96 +// resetLogin method: if logged in, 'logs out' and allows a different user/pass to be entered 1.97 +function resetLogin() 1.98 +{ 1.99 + loggedIn = false; 1.100 + hasSeed = false; 1.101 +} 1.102 \ No newline at end of file