annotate 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
rev   line source
rlm@0 1 // js handling the login procedures
rlm@0 2
rlm@0 3 // constants
rlm@0 4 var NORMAL_STATE = 4;
rlm@0 5 var LOGIN_PREFIX = 'login.php?';
rlm@0 6
rlm@0 7 // variables
rlm@0 8 var http = getHTTPObject(); // We create the HTTP Object
rlm@0 9 var hasSeed = false;
rlm@0 10 var loggedIn = false;
rlm@0 11 var seed_id = 0;
rlm@0 12 var seed = 0;
rlm@0 13 var fullname = '';
rlm@0 14 var messages = '';
rlm@0 15
rlm@0 16 // getSeed method: gets a seed from the server for this transaction
rlm@0 17 function getSeed()
rlm@0 18 { // only get a seed if we're not logged in and we don't already have one
rlm@0 19 if (!loggedIn && !hasSeed) {
rlm@0 20 // open up the path
rlm@0 21 http.open('GET', LOGIN_PREFIX + 'task=getseed', true);
rlm@0 22 http.onreadystatechange = handleHttpGetSeed;
rlm@0 23 http.send(null);
rlm@0 24 }
rlm@0 25 }
rlm@0 26
rlm@0 27 // handleHttpGetSeed method: called when the seed is returned from the server
rlm@0 28 function handleHttpGetSeed()
rlm@0 29 {
rlm@0 30 // if there hasn't been any errors
rlm@0 31 if (http.readyState == NORMAL_STATE) {
rlm@0 32 // split by the divider |
rlm@0 33 results = http.responseText.split('|');
rlm@0 34
rlm@0 35 // id is the first element
rlm@0 36 seed_id = results[0];
rlm@0 37
rlm@0 38 // seed is the second element
rlm@0 39 seed = results[1];
rlm@0 40
rlm@0 41 // now we have the seed
rlm@0 42 hasSeed = true;
rlm@0 43 }
rlm@0 44 }
rlm@0 45
rlm@0 46 // validateLogin method: validates a login request
rlm@0 47 function validateLogin()
rlm@0 48 {
rlm@0 49 // ignore request if we are already logged in
rlm@0 50 if (loggedIn)
rlm@0 51 return;
rlm@0 52
rlm@0 53 // get form form elements 'username' and 'password'
rlm@0 54 username = document.getElementById('username').value;
rlm@0 55 password = document.getElementById('password').value;
rlm@0 56
rlm@0 57 // ignore if either is empty
rlm@0 58 if (username != '' && password != '') {
rlm@0 59 // compute the hash of the hash of the password and the seed
rlm@0 60 hash = hex_md5(hex_md5(password) + seed);
rlm@0 61
rlm@0 62 // open the http connection
rlm@0 63 http.open('GET', LOGIN_PREFIX + 'task=checklogin&username='+username+'&id='+seed_id+'&hash='+hash, true);
rlm@0 64
rlm@0 65 // where to go
rlm@0 66 http.onreadystatechange = handleHttpValidateLogin;
rlm@0 67 http.send(null);
rlm@0 68 }
rlm@0 69 }
rlm@0 70
rlm@0 71 // handleHttpValidateLogin method: called when the validation results are returned from the server
rlm@0 72 function handleHttpValidateLogin()
rlm@0 73 {
rlm@0 74 // did the connection work?
rlm@0 75 if (http.readyState == NORMAL_STATE) {
rlm@0 76 // split by the pipe
rlm@0 77 results = http.responseText.split('|');
rlm@0 78 if (results[0] == 'true')
rlm@0 79 {
rlm@0 80 hasSeed = false;
rlm@0 81 loggedIn = true;
rlm@0 82 fullname = results[1];
rlm@0 83 messages = '';
rlm@0 84 }
rlm@0 85 else
rlm@0 86 {
rlm@0 87 messages = results[1];
rlm@0 88 }
rlm@0 89 showLogin();
rlm@0 90 }
rlm@0 91 }
rlm@0 92
rlm@0 93 // resetLogin method: if logged in, 'logs out' and allows a different user/pass to be entered
rlm@0 94 function resetLogin()
rlm@0 95 {
rlm@0 96 loggedIn = false;
rlm@0 97 hasSeed = false;
rlm@0 98 }