| 1 | /* ___________________________________________________________________________ |
|---|
| 2 | * _install section login |
|---|
| 3 | * |
|---|
| 4 | * used in login and generate hash view! |
|---|
| 5 | * |
|---|
| 6 | * some routines exist from {{ PyLucid_media_url }}/shared_sha_tools.js |
|---|
| 7 | * and {{ PyLucid_media_url }}/sha.js |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | /* uncomment to activate the debug window */ |
|---|
| 11 | //debug_msg = true; |
|---|
| 12 | |
|---|
| 13 | function shared_init() { |
|---|
| 14 | /* init stuff for both install views: install/generate hash */ |
|---|
| 15 | |
|---|
| 16 | /* The login form was hide via CSS. After JS loaded fine, we unhide it. */ |
|---|
| 17 | unhide_by_id("password_form"); // from: shared_sha_tools.js |
|---|
| 18 | |
|---|
| 19 | set_focus("plaintext_pass"); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | function login_init() { |
|---|
| 24 | shared_init(); |
|---|
| 25 | |
|---|
| 26 | /* After generating the install hash, the form should be submit */ |
|---|
| 27 | submit_form = true; |
|---|
| 28 | |
|---|
| 29 | debug("salt value from server:" + salt); |
|---|
| 30 | if (salt.length != SALT_LEN) { |
|---|
| 31 | alert("salt from Server fail!"); |
|---|
| 32 | return false; |
|---|
| 33 | } |
|---|
| 34 | check_ok = true; // initial set to false in shared_sha_tools.js |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | function generate_hash_init() { |
|---|
| 39 | shared_init(); |
|---|
| 40 | |
|---|
| 41 | /* The form should not submit, after generating the install hash */ |
|---|
| 42 | submit_form = false; |
|---|
| 43 | |
|---|
| 44 | salt = generate_salt(); |
|---|
| 45 | debug("generated JS salt value:" + salt); |
|---|
| 46 | if (salt==false) { |
|---|
| 47 | return false; |
|---|
| 48 | } |
|---|
| 49 | check_ok = true; // initial set to false in shared_sha_tools.js |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | function make_hash(submit) { |
|---|
| 54 | /* |
|---|
| 55 | processing the form data for the both install views: login & generate hash |
|---|
| 56 | The only difference here is: |
|---|
| 57 | * The given salt value: |
|---|
| 58 | * login view: |
|---|
| 59 | The salt value was insert into the html page from the server |
|---|
| 60 | * generate hash view: |
|---|
| 61 | The salt value is a random JS number generated in init() |
|---|
| 62 | * only the login view must submit the form. |
|---|
| 63 | */ |
|---|
| 64 | if (check_ok != true) { |
|---|
| 65 | alert("Internal error. (check_ok not 'true')"); |
|---|
| 66 | return false; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | in_pass = get_plaintext_pass("plaintext_pass"); |
|---|
| 70 | if (in_pass==false) { |
|---|
| 71 | return false; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | sha = hex_sha1(salt + in_pass); |
|---|
| 75 | hash = "sha1$" + salt + "$" + sha; |
|---|
| 76 | |
|---|
| 77 | set_value("hash", hash); |
|---|
| 78 | change_color("hash", "#90EE90"); |
|---|
| 79 | |
|---|
| 80 | set_value("plaintext_pass", ""); |
|---|
| 81 | change_color("plaintext_pass", "#808080"); |
|---|
| 82 | |
|---|
| 83 | debug_confirm(); |
|---|
| 84 | if (submit_form==true) { |
|---|
| 85 | /* Only in the login view, the form must be submit */ |
|---|
| 86 | document.login.submit(); |
|---|
| 87 | } else { |
|---|
| 88 | /* The generate hash should never send the form back to the server! */ |
|---|
| 89 | return false; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | function generate_salt() { |
|---|
| 95 | /* return a random string: six digit number */ |
|---|
| 96 | salt = Math.random(); |
|---|
| 97 | // convert to a string: |
|---|
| 98 | salt = "" + salt; |
|---|
| 99 | // get 5 decimal places after the comma: |
|---|
| 100 | salt = salt.substring(2, 7); |
|---|
| 101 | if (salt.length != SALT_LEN) { |
|---|
| 102 | alert("generating a random salt fail!"); |
|---|
| 103 | return false; |
|---|
| 104 | } |
|---|
| 105 | return salt |
|---|
| 106 | } |
|---|