author | Dan |
Wed, 29 Jul 2009 12:09:35 -0400 | |
changeset 24 | ba1a31f16afe |
parent 23 | 4b868c7c241e |
child 28 | b9a3da602841 |
permissions | -rw-r--r-- |
0 | 1 |
// sample OTP: |
2 |
// ttttvvvvvvcurikvhjcvnlnbecbkubjvuittbifhndhn |
|
3 |
// charset: cbdefghijklnrtuv |
|
4 |
||
5 |
var yk_interval = false; |
|
6 |
||
7 |
var YK_SEC_NORMAL_USERNAME = 1; |
|
8 |
var YK_SEC_NORMAL_PASSWORD = 2; |
|
9 |
var YK_SEC_ELEV_USERNAME = 4; |
|
10 |
var YK_SEC_ELEV_PASSWORD = 8; |
|
11 |
||
12 |
var yubikey_otp_current = false; |
|
13 |
||
14 |
function yk_mb_init(fieldid, statid) |
|
15 |
{ |
|
16 |
load_component(['messagebox', 'fadefilter', 'flyin', 'jquery', 'jquery-ui', 'l10n']); |
|
17 |
var mp = miniPrompt(yk_mb_construct); |
|
18 |
if ( typeof(fieldid) == 'function' ) |
|
19 |
{ |
|
20 |
var input = mp.getElementsByTagName('input')[0]; |
|
21 |
input.submit_func = fieldid; |
|
22 |
} |
|
23 |
else if ( fieldid && statid ) |
|
24 |
{ |
|
25 |
var input = mp.getElementsByTagName('input')[0]; |
|
26 |
input.yk_field_id = fieldid; |
|
27 |
input.yk_status_id = statid; |
|
28 |
} |
|
29 |
} |
|
30 |
||
31 |
function yk_mb_construct(mp) |
|
32 |
{ |
|
33 |
mp.innerHTML = ''; |
|
34 |
mp.style.textAlign = 'center'; |
|
35 |
mp.innerHTML = '<h3>' + $lang.get('yubiauth_msg_please_touch_key') + '</h3>'; |
|
6 | 36 |
var progress = document.createElement('div'); |
37 |
$(progress).addClass('yubikey_bar'); |
|
38 |
var progimg = document.createElement('img'); |
|
39 |
progimg.src = cdnPath + '/images/spacer.gif'; |
|
40 |
progress.appendChild(progimg); |
|
41 |
mp.appendChild(progress); |
|
0 | 42 |
var ta = document.createElement('input'); |
43 |
ta.submitted = false; |
|
44 |
$(ta) |
|
45 |
.css('background-color', 'transparent') |
|
46 |
.css('border-width', '0px') |
|
47 |
.css('color', '#fff') |
|
48 |
.css('font-size', '1px') |
|
49 |
.css('padding', '0') |
|
50 |
.attr('size', '1') |
|
51 |
.keyup(function(e) |
|
52 |
{ |
|
53 |
if ( e.keyCode == 27 ) |
|
54 |
{ |
|
55 |
window.clearInterval(yk_interval); |
|
56 |
miniPromptDestroy(this); |
|
57 |
} |
|
23
4b868c7c241e
JS: Change to submit behavior: allowed keycode 13 (enter) for submit
Dan
parents:
18
diff
changeset
|
58 |
else if ( this.value.length == 44 && !this.submitted ) |
4b868c7c241e
JS: Change to submit behavior: allowed keycode 13 (enter) for submit
Dan
parents:
18
diff
changeset
|
59 |
{ |
4b868c7c241e
JS: Change to submit behavior: allowed keycode 13 (enter) for submit
Dan
parents:
18
diff
changeset
|
60 |
this.submitted = true; |
4b868c7c241e
JS: Change to submit behavior: allowed keycode 13 (enter) for submit
Dan
parents:
18
diff
changeset
|
61 |
yk_handle_submit(this); |
4b868c7c241e
JS: Change to submit behavior: allowed keycode 13 (enter) for submit
Dan
parents:
18
diff
changeset
|
62 |
} |
4b868c7c241e
JS: Change to submit behavior: allowed keycode 13 (enter) for submit
Dan
parents:
18
diff
changeset
|
63 |
else if ( e.keyCode == 13 && this.value.length != 44 ) |
0 | 64 |
{ |
65 |
this.submitted = true; |
|
66 |
yk_handle_submit(this); |
|
67 |
} |
|
6 | 68 |
else |
69 |
{ |
|
70 |
$('div.yubikey_bar > img', this.parentNode) |
|
71 |
.css('width', String(this.value.length * 2) + 'px') |
|
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
72 |
.css('background-position', String((this.value.length > 44 ? 44 : this.value.length) - 44) + 'px -88px'); |
6 | 73 |
} |
0 | 74 |
e.preventDefault(); |
75 |
e.stopPropagation(); |
|
76 |
}); |
|
77 |
mp.appendChild(ta); |
|
78 |
setTimeout(function() |
|
79 |
{ |
|
80 |
window.yk_interval = setInterval(function() |
|
81 |
{ |
|
82 |
ta.focus(); |
|
83 |
}, 50); |
|
84 |
}, 750); |
|
85 |
var info = document.createElement('p'); |
|
6 | 86 |
$(info) |
87 |
.html($lang.get('yubiauth_msg_close_instructions')) |
|
88 |
.css('margin-top', '0'); |
|
0 | 89 |
mp.appendChild(info); |
90 |
} |
|
91 |
||
92 |
function yk_handle_submit(ta) |
|
93 |
{ |
|
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
94 |
if ( ta.value.length > 44 || !ta.value.match(/^[cbdefghijklnrtuv]+$/) ) |
0 | 95 |
{ |
96 |
setTimeout(function() |
|
97 |
{ |
|
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
98 |
var parent = ta.parentNode; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
99 |
var tabackup = { |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
100 |
field_id: ta.yk_field_id, |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
101 |
status_id: ta.yk_status_id, |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
102 |
submit_func: ta.submit_func |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
103 |
}; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
104 |
yk_mb_construct(parent); |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
105 |
var input = parent.getElementsByTagName('input')[0]; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
106 |
if ( tabackup.field_id ) |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
107 |
input.yk_field_id = tabackup.field_id; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
108 |
if ( tabackup.status_id ) |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
109 |
input.yk_status_id = tabackup.status_id; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
110 |
if ( tabackup.submit_func ) |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
111 |
input.submit_func = tabackup.submit_func; |
0 | 112 |
}, 1000); |
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
113 |
$('h3', ta.parentNode).text($lang.get(ta.value.length > 44 ? 'yubiauth_msg_too_long' : 'yubiauth_msg_invalid_chars')); |
6 | 114 |
$('div.yubikey_bar > img', this.parentNode).addClass('yubikey_bar_error'); |
0 | 115 |
return false; |
116 |
} |
|
117 |
||
118 |
window.clearInterval(yk_interval); |
|
119 |
||
120 |
if ( ta.yk_field_id && ta.yk_status_id ) |
|
121 |
{ |
|
122 |
var field = document.getElementById(ta.yk_field_id); |
|
123 |
var status = document.getElementById(ta.yk_status_id); |
|
124 |
if ( $(status).hasClass('empty') || $(status).hasClass('rmpending') ) |
|
125 |
{ |
|
126 |
$(status).next('a') |
|
127 |
.text($lang.get('yubiauth_ctl_btn_change_key')) |
|
128 |
.addClass('abutton_green') |
|
129 |
.after(' <a class="abutton abutton_red yubikey_enroll" href="#yk_clear" onclick="yk_clear(\'' + ta.yk_field_id + '\', \'' + ta.yk_status_id + '\'); return false;">' |
|
130 |
+ $lang.get('yubiauth_ctl_btn_clear') + |
|
131 |
'</a>'); |
|
132 |
} |
|
133 |
$(status).removeClass('empty').removeClass('enrolled').removeClass('rmpending').addClass('savepending').html($lang.get('yubiauth_ctl_status_enrolled_pending')); |
|
134 |
field.value = ta.value; |
|
135 |
miniPromptDestroy(ta); |
|
136 |
return true; |
|
137 |
} |
|
138 |
else if ( ta.submit_func ) |
|
139 |
{ |
|
140 |
ta.submit_func(ta); |
|
141 |
} |
|
142 |
else |
|
143 |
{ |
|
144 |
miniPromptDestroy(ta); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
function yk_login_validate_reqs(ta) |
|
149 |
{ |
|
6 | 150 |
$(ta.parentNode).remove('p'); |
0 | 151 |
yubikey_otp_current = ta.value; |
152 |
||
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
153 |
miniPromptDestroy(ta, true); |
0 | 154 |
|
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
155 |
if ( logindata ) |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
156 |
{ |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
157 |
if ( logindata.mb_object ) |
0 | 158 |
{ |
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
159 |
// login window is open |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
160 |
if ( user_level == USER_LEVEL_GUEST ) |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
161 |
{ |
18
dd8c53454f31
Yubikey flags are no longer fetched from server at login time, instead provided with load
Dan
parents:
17
diff
changeset
|
162 |
var show_username = window.yk_user_flags & YK_SEC_NORMAL_USERNAME; |
dd8c53454f31
Yubikey flags are no longer fetched from server at login time, instead provided with load
Dan
parents:
17
diff
changeset
|
163 |
var show_password = window.yk_user_flags & YK_SEC_NORMAL_PASSWORD; |
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
164 |
} |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
165 |
else |
0 | 166 |
{ |
18
dd8c53454f31
Yubikey flags are no longer fetched from server at login time, instead provided with load
Dan
parents:
17
diff
changeset
|
167 |
var show_username = window.yk_user_flags & YK_SEC_ELEV_USERNAME; |
dd8c53454f31
Yubikey flags are no longer fetched from server at login time, instead provided with load
Dan
parents:
17
diff
changeset
|
168 |
var show_password = window.yk_user_flags & YK_SEC_ELEV_PASSWORD; |
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
169 |
} |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
170 |
if ( !show_username ) |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
171 |
$('#ajax_login_field_username').parent('td').hide().prev().hide(); |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
172 |
if ( !show_password ) |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
173 |
$('#ajax_login_field_password').parent('td').hide().prev().hide(); |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
174 |
|
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
175 |
var can_submit = true; |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
176 |
if ( show_username && !$('#ajax_login_field_username').attr('value') ) |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
177 |
{ |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
178 |
$('#ajax_login_field_password').focus(); |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
179 |
can_submit = false; |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
180 |
} |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
181 |
if ( show_password && !$('#ajax_login_field_password').attr('value') ) |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
182 |
{ |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
183 |
if ( can_submit ) |
0 | 184 |
{ |
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
185 |
$('#ajax_login_field_password').focus(); |
0 | 186 |
} |
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
187 |
can_submit = false; |
0 | 188 |
} |
17
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
189 |
|
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
190 |
if ( can_submit ) |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
191 |
{ |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
192 |
$('#messageBoxButtons input:button:first').click(); |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
193 |
} |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
194 |
} |
e04c0f64e972
SECURITY (critical): If username provided, any Yubikey could be used to log in.
Dan
parents:
12
diff
changeset
|
195 |
} |
0 | 196 |
} |
197 |
||
198 |
function yk_clear(field_id, status_id) |
|
199 |
{ |
|
200 |
var field = document.getElementById(field_id); |
|
201 |
var status = document.getElementById(status_id); |
|
202 |
||
203 |
var was_pending = $(field).hasClass('wasempty'); |
|
204 |
||
205 |
$(field).attr('value', ''); |
|
206 |
$(status) |
|
207 |
.removeClass('savepending') |
|
208 |
.removeClass('enrolled') |
|
209 |
.addClass( was_pending ? 'empty' : 'rmpending' ) |
|
210 |
.text( was_pending ? $lang.get('yubiauth_ctl_status_empty') : $lang.get('yubiauth_ctl_status_remove_pending') ) |
|
211 |
.next('a') |
|
212 |
.text($lang.get('yubiauth_ctl_btn_enroll')) |
|
213 |
.removeClass('abutton_green') |
|
214 |
.next('a') |
|
215 |
.remove(); |
|
216 |
} |
|
217 |
||
218 |
addOnloadHook(function() |
|
219 |
{ |
|
24 | 220 |
if ( is_iPhone ) |
221 |
// kinda can't plug a yubikey into an iPhone |
|
222 |
// ... yet? |
|
223 |
return; |
|
224 |
||
0 | 225 |
attachHook('login_build_form', 'yk_login_dlg_hook(table);'); |
226 |
attachHook('login_build_userinfo', 'if ( window.yubikey_otp_current ) userinfo.yubikey_otp = window.yubikey_otp_current;'); |
|
12 | 227 |
if ( title == namespace_list.Special + 'Preferences/Yubikey' ) |
228 |
{ |
|
229 |
load_component(['jquery', 'jquery-ui', 'expander']); |
|
230 |
} |
|
0 | 231 |
}); |
232 |
||
233 |
function yk_login_dlg_hook(table) |
|
234 |
{ |
|
235 |
window.yubikey_otp_current = false; |
|
236 |
var tr = document.createElement('tr'); |
|
237 |
var td = document.createElement('td'); |
|
238 |
$(td) |
|
239 |
.attr('colspan', '2') |
|
240 |
.css('text-align', 'center') |
|
241 |
.css('font-size', 'smaller') |
|
242 |
.css('font-weight', 'bold') |
|
243 |
.html('<a href="#" onclick="yk_mb_init(yk_login_validate_reqs); return false;" style="color: #6fa202">' + $lang.get('yubiauth_btn_enter_otp') + '</a>'); |
|
244 |
$('a', td).blur(function(e) |
|
245 |
{ |
|
246 |
$('#messageBoxButtons input:button:first').focus(); |
|
247 |
$('#ajax_login_field_captcha').focus(); |
|
248 |
}); |
|
10 | 249 |
if ( window.yk_reg_require_otp || window.yk_user_enabled ) |
9
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
250 |
{ |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
251 |
setTimeout(function() |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
252 |
{ |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
253 |
yk_mb_init(yk_login_validate_reqs); |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
254 |
}, 750); |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
255 |
} |
0 | 256 |
tr.appendChild(td); |
257 |
table.appendChild(tr); |
|
258 |
} |
|
259 |