author | Dan |
Tue, 17 Mar 2009 00:25:50 -0400 | |
changeset 11 | 794e7b1a97e7 |
parent 10 | 748fa1b80031 |
child 12 | 6faa838e4433 |
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 |
} |
|
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
58 |
else if ( this.value.length >= 44 && !this.submitted ) |
0 | 59 |
{ |
60 |
this.submitted = true; |
|
61 |
yk_handle_submit(this); |
|
62 |
} |
|
6 | 63 |
else |
64 |
{ |
|
65 |
$('div.yubikey_bar > img', this.parentNode) |
|
66 |
.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
|
67 |
.css('background-position', String((this.value.length > 44 ? 44 : this.value.length) - 44) + 'px -88px'); |
6 | 68 |
} |
0 | 69 |
e.preventDefault(); |
70 |
e.stopPropagation(); |
|
71 |
}); |
|
72 |
mp.appendChild(ta); |
|
73 |
setTimeout(function() |
|
74 |
{ |
|
75 |
window.yk_interval = setInterval(function() |
|
76 |
{ |
|
77 |
ta.focus(); |
|
78 |
}, 50); |
|
79 |
}, 750); |
|
80 |
var info = document.createElement('p'); |
|
6 | 81 |
$(info) |
82 |
.html($lang.get('yubiauth_msg_close_instructions')) |
|
83 |
.css('margin-top', '0'); |
|
0 | 84 |
mp.appendChild(info); |
85 |
} |
|
86 |
||
87 |
function yk_handle_submit(ta) |
|
88 |
{ |
|
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
89 |
if ( ta.value.length > 44 || !ta.value.match(/^[cbdefghijklnrtuv]+$/) ) |
0 | 90 |
{ |
91 |
setTimeout(function() |
|
92 |
{ |
|
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
93 |
var parent = ta.parentNode; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
94 |
var tabackup = { |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
95 |
field_id: ta.yk_field_id, |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
96 |
status_id: ta.yk_status_id, |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
97 |
submit_func: ta.submit_func |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
98 |
}; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
99 |
yk_mb_construct(parent); |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
100 |
var input = parent.getElementsByTagName('input')[0]; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
101 |
if ( tabackup.field_id ) |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
102 |
input.yk_field_id = tabackup.field_id; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
103 |
if ( tabackup.status_id ) |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
104 |
input.yk_status_id = tabackup.status_id; |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
105 |
if ( tabackup.submit_func ) |
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
106 |
input.submit_func = tabackup.submit_func; |
0 | 107 |
}, 1000); |
7
c8fc1493eacd
Fixed a few issues with input postprocessing in client API
Dan
parents:
6
diff
changeset
|
108 |
$('h3', ta.parentNode).text($lang.get(ta.value.length > 44 ? 'yubiauth_msg_too_long' : 'yubiauth_msg_invalid_chars')); |
6 | 109 |
$('div.yubikey_bar > img', this.parentNode).addClass('yubikey_bar_error'); |
0 | 110 |
return false; |
111 |
} |
|
112 |
||
113 |
window.clearInterval(yk_interval); |
|
114 |
||
115 |
if ( ta.yk_field_id && ta.yk_status_id ) |
|
116 |
{ |
|
117 |
var field = document.getElementById(ta.yk_field_id); |
|
118 |
var status = document.getElementById(ta.yk_status_id); |
|
119 |
if ( $(status).hasClass('empty') || $(status).hasClass('rmpending') ) |
|
120 |
{ |
|
121 |
$(status).next('a') |
|
122 |
.text($lang.get('yubiauth_ctl_btn_change_key')) |
|
123 |
.addClass('abutton_green') |
|
124 |
.after(' <a class="abutton abutton_red yubikey_enroll" href="#yk_clear" onclick="yk_clear(\'' + ta.yk_field_id + '\', \'' + ta.yk_status_id + '\'); return false;">' |
|
125 |
+ $lang.get('yubiauth_ctl_btn_clear') + |
|
126 |
'</a>'); |
|
127 |
} |
|
128 |
$(status).removeClass('empty').removeClass('enrolled').removeClass('rmpending').addClass('savepending').html($lang.get('yubiauth_ctl_status_enrolled_pending')); |
|
129 |
field.value = ta.value; |
|
130 |
miniPromptDestroy(ta); |
|
131 |
return true; |
|
132 |
} |
|
133 |
else if ( ta.submit_func ) |
|
134 |
{ |
|
135 |
ta.submit_func(ta); |
|
136 |
} |
|
137 |
else |
|
138 |
{ |
|
139 |
miniPromptDestroy(ta); |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
function yk_login_validate_reqs(ta) |
|
144 |
{ |
|
6 | 145 |
$(ta.parentNode).remove('p'); |
0 | 146 |
yubikey_otp_current = ta.value; |
147 |
||
6 | 148 |
$('h3', ta.parentNode).text($lang.get('yubiauth_msg_validating_otp')); |
0 | 149 |
|
150 |
ajaxPost(makeUrlNS('Special', 'Yubikey'), 'get_flags=' + ta.value.substr(0, 12), function(ajax) |
|
151 |
{ |
|
152 |
if ( ajax.readyState == 4 && ajax.status == 200 ) |
|
153 |
{ |
|
154 |
miniPromptDestroy(ta); |
|
155 |
if ( !check_json_response(ajax.responseText) ) |
|
156 |
{ |
|
157 |
handle_invalid_json(ajax.responseText); |
|
158 |
return false; |
|
159 |
} |
|
6 | 160 |
$('h3', ta.parentNode).text($lang.get('yubiauth_msg_otp_valid')); |
0 | 161 |
var response = parseJSON(ajax.responseText); |
162 |
if ( response.mode == 'error' ) |
|
163 |
{ |
|
164 |
alert('Yubikey server-side processing error: \n' + response.error); |
|
165 |
return false; |
|
166 |
} |
|
167 |
if ( logindata ) |
|
168 |
{ |
|
169 |
if ( logindata.mb_object ) |
|
170 |
{ |
|
171 |
// login window is open |
|
172 |
if ( user_level == USER_LEVEL_GUEST ) |
|
173 |
{ |
|
174 |
var show_username = response.flags & YK_SEC_NORMAL_USERNAME; |
|
175 |
var show_password = response.flags & YK_SEC_NORMAL_PASSWORD; |
|
176 |
} |
|
177 |
else |
|
178 |
{ |
|
179 |
var show_username = response.flags & YK_SEC_ELEV_USERNAME; |
|
180 |
var show_password = response.flags & YK_SEC_ELEV_PASSWORD; |
|
181 |
} |
|
182 |
if ( !show_username ) |
|
183 |
$('#ajax_login_field_username').parent('td').hide().prev().hide(); |
|
184 |
if ( !show_password ) |
|
185 |
$('#ajax_login_field_password').parent('td').hide().prev().hide(); |
|
186 |
||
187 |
var can_submit = true; |
|
188 |
if ( show_username && !$('#ajax_login_field_username').attr('value') ) |
|
189 |
{ |
|
190 |
$('#ajax_login_field_password').focus(); |
|
191 |
can_submit = false; |
|
192 |
} |
|
193 |
if ( show_password && !$('#ajax_login_field_password').attr('value') ) |
|
194 |
{ |
|
195 |
if ( can_submit ) |
|
196 |
{ |
|
197 |
$('#ajax_login_field_password').focus(); |
|
198 |
} |
|
199 |
can_submit = false; |
|
200 |
} |
|
201 |
||
202 |
if ( can_submit ) |
|
203 |
{ |
|
204 |
$('#messageBoxButtons input:button:first').click(); |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
}); |
|
210 |
} |
|
211 |
||
212 |
function yk_clear(field_id, status_id) |
|
213 |
{ |
|
214 |
var field = document.getElementById(field_id); |
|
215 |
var status = document.getElementById(status_id); |
|
216 |
||
217 |
var was_pending = $(field).hasClass('wasempty'); |
|
218 |
||
219 |
$(field).attr('value', ''); |
|
220 |
$(status) |
|
221 |
.removeClass('savepending') |
|
222 |
.removeClass('enrolled') |
|
223 |
.addClass( was_pending ? 'empty' : 'rmpending' ) |
|
224 |
.text( was_pending ? $lang.get('yubiauth_ctl_status_empty') : $lang.get('yubiauth_ctl_status_remove_pending') ) |
|
225 |
.next('a') |
|
226 |
.text($lang.get('yubiauth_ctl_btn_enroll')) |
|
227 |
.removeClass('abutton_green') |
|
228 |
.next('a') |
|
229 |
.remove(); |
|
230 |
} |
|
231 |
||
232 |
addOnloadHook(function() |
|
233 |
{ |
|
234 |
attachHook('login_build_form', 'yk_login_dlg_hook(table);'); |
|
235 |
attachHook('login_build_userinfo', 'if ( window.yubikey_otp_current ) userinfo.yubikey_otp = window.yubikey_otp_current;'); |
|
236 |
}); |
|
237 |
||
238 |
function yk_login_dlg_hook(table) |
|
239 |
{ |
|
240 |
window.yubikey_otp_current = false; |
|
241 |
var tr = document.createElement('tr'); |
|
242 |
var td = document.createElement('td'); |
|
243 |
$(td) |
|
244 |
.attr('colspan', '2') |
|
245 |
.css('text-align', 'center') |
|
246 |
.css('font-size', 'smaller') |
|
247 |
.css('font-weight', 'bold') |
|
248 |
.html('<a href="#" onclick="yk_mb_init(yk_login_validate_reqs); return false;" style="color: #6fa202">' + $lang.get('yubiauth_btn_enter_otp') + '</a>'); |
|
249 |
$('a', td).blur(function(e) |
|
250 |
{ |
|
251 |
$('#messageBoxButtons input:button:first').focus(); |
|
252 |
$('#ajax_login_field_captcha').focus(); |
|
253 |
}); |
|
10 | 254 |
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
|
255 |
{ |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
256 |
setTimeout(function() |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
257 |
{ |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
258 |
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
|
259 |
}, 750); |
65965da01c41
If yubikey_reg_require_otp is 1, opening login window now auto-opens Yubikey prompt
Dan
parents:
7
diff
changeset
|
260 |
} |
0 | 261 |
tr.appendChild(td); |
262 |
table.appendChild(tr); |
|
263 |
} |
|
264 |