root/trunk/pylucid_project/media/PyLucid/shared_sha_tools.js

Revision 1498, 4.9 KB (checked in by JensDiemer, 2 years ago)

* Updates for ticket:163 - auth/login: password form with visibility:hidden
* Bugfixes for ticket:165 - _install: generate the SHA hash doesn't work with IE
* Merge some JS code together

Line 
1/* ___________________________________________________________________________
2 *  Some needed stuff round the SHA-JS-Login
3 */
4
5check_ok = false;
6debug_msg = false;
7
8// length of a SHA1 hexdigest string:
9HASH_LEN = 40;
10// length of the salt and challenge value string:
11SALT_LEN = 5;
12
13
14if (!document.getElementById) {
15    alert("Error: Your Browser is not supported!");
16}
17
18if (navigator.cookieEnabled) {
19    if (navigator.cookieEnabled != true) {
20        alert("You must enable Cookies in your Browser!");
21    }
22}
23
24/* ___________________________________________________________________________
25 *  needfull generic functions:
26 */
27
28function get_value(object_id) {
29    // get a form value
30    try {
31        return document.getElementById(object_id).value;
32    } catch (e) {
33        alert("get_value('"+object_id+"') error! " + e);
34    }
35}
36
37function set_value(object_id, value) {
38    // set a form value
39    try {
40        obj = document.getElementById(object_id).value = value;
41    } catch (e) {
42        alert("set_value('"+object_id+"', '"+value+"') error! " + e);
43    }
44}
45
46function set_focus(object_id) {
47    try {
48        debug("set focus on id:" + object_id);
49        document.getElementById(object_id).focus();
50    } catch (e) {
51        alert("set_focus('"+object_id+"') error:" + e);
52    }
53}
54
55function hide_by_id(object_id) {
56    try {
57        debug("hide_by_id: "+object_id);
58        obj = document.getElementById(object_id);
59        obj.style.display = 'none';
60    } catch (e) {
61        alert("hide_by_id('"+object_id+"') error:" + e);
62    }
63}
64function unhide_by_id(object_id) {
65    try {
66        debug("unhide_by_id: "+object_id);
67        obj = document.getElementById(object_id);
68        obj.style.display = 'block';
69    } catch (e) {
70        alert("unhide_by_id('"+object_id+"') error:" + e);
71    }
72}
73
74function change_color(object_id, color_name) {
75    try {
76        debug("change_color: "+object_id);
77        obj = document.getElementById(object_id);
78        obj.style.backgroundColor = color_name;
79    } catch (e) {
80        alert("change_color('"+object_id+"', '"+color_name+"') error:" + e);
81    }
82}
83
84/* ___________________________________________________________________________
85 *  special functions:
86 */
87
88function check_ascii_only(data) {
89    for (var i = 1; i <= data.length; i++) {
90       unicode_charcode = data.charCodeAt(i);
91       if (unicode_charcode > 127) {
92            alert("Only ASCII letters are allowed!");
93            return false;
94       }
95    }
96    return true;
97 }
98
99function get_plaintext_pass(object_id) {
100    try {
101        in_pass = get_value(object_id);
102        debug("in_pass:" + in_pass);
103        if (in_pass.length<8) {
104            alert("Password min len 8! - current len:" + in_pass.length);
105            set_focus(object_id)
106            return false;
107        }
108
109        if (check_ascii_only(in_pass) == false) {
110            set_focus(object_id)
111            return false;
112        }
113        return in_pass;
114    } catch (e) {
115        alert("get_plaintext_pass() error:" + e);
116        return false;
117    }
118}
119
120function make_SHA(txt) {
121    try {
122        debug("make_SHA(" + txt + "):");
123        SHA_hexdigest = hex_sha1(txt); // from: sha.js
124        len = SHA_hexdigest.length;
125        if (len != HASH_LEN) {
126            alert("make_SHA() error! wrong length:" + len);
127            return false;
128        }
129        debug(SHA_hexdigest);
130        return SHA_hexdigest;
131    } catch (e) {
132        alert("make_SHA() error:" + e);
133        return false;
134    }
135}
136
137
138/* ___________________________________________________________________________
139 *  debugging:
140 */
141
142function init_debug() {
143    /* Create a debug window, if it's not exists */
144    try {
145        if (debug_msg != true) { return; }
146        try {
147            if (debug_window) {
148                return;
149            }
150        } catch (e) {}
151        debug_window = window.open("", "Debug", "dependent=yes, resizable=yes, scrollbars=yes, width=350, height=400, top=1, left=" + window.outerWidth);
152
153        debug_win = debug_window.document;
154        debug_win.writeln("<style>* { font-size: 0.85em; }</style>");
155
156        var now = new Date();
157        now = now.toLocaleString();
158
159        debug_win.writeln("<h1>JS Debug - "+now+":</h1>");
160        debug_win.writeln("---[DEBUG START]---");
161        debug_win.writeln("cookie:" + document.cookie +"<br />");
162    } catch (e) {
163        alert("init_debug() error:" + e);
164    }
165}
166
167
168function debug(msg) {
169    try {
170        if (debug_msg != true) { return; }
171        init_debug();
172        debug_win.writeln(msg + "<br />");
173        debug_window.focus();
174        // scroll to the last lines
175        debug_window.scrollBy(0, 1000);
176    } catch (e) {
177        alert("debug('"+msg+"') error:" + e);
178    }
179}
180
181
182function debug_confirm() {
183    try {
184       if (debug_msg != true) { return; }
185       debug_window.focus();
186       debug_win.writeln("---[DEBUG END]---");
187       alert("OK for closing the debug window.");
188       debug_window.close();
189       debug_window = false;
190    } catch (e) {
191        alert("debug_confirm() error:" + e);
192    }
193}
Note: See TracBrowser for help on using the browser.