2259 |
28 Feb 14 |
nicklas |
1 |
|
2259 |
28 Feb 14 |
nicklas |
var YubiKey = function() |
2259 |
28 Feb 14 |
nicklas |
3 |
{ |
2259 |
28 Feb 14 |
nicklas |
var yubikey = {}; |
2259 |
28 Feb 14 |
nicklas |
5 |
|
2259 |
28 Feb 14 |
nicklas |
yubikey.initPage = function() |
2259 |
28 Feb 14 |
nicklas |
7 |
{ |
2259 |
28 Feb 14 |
nicklas |
// Disable YubiKey fields on extended properties tab |
2259 |
28 Feb 14 |
nicklas |
Doc.element('ep.yubiKeySerial').disabled = true; |
2259 |
28 Feb 14 |
nicklas |
Doc.element('ep.yubiKeyId').disabled = true; |
2259 |
28 Feb 14 |
nicklas |
11 |
|
2259 |
28 Feb 14 |
nicklas |
Events.addEventHandler('yubiKeySerial', 'blur', yubikey.copySerial); |
2259 |
28 Feb 14 |
nicklas |
Events.doOnEnter('yubiKeyOtp', yubikey.verifyOtp); |
2259 |
28 Feb 14 |
nicklas |
Buttons.addClickHandler('btnYubiKeyRemove', yubikey.removeYubiKey); |
2488 |
05 Jun 14 |
nicklas |
15 |
|
2488 |
05 Jun 14 |
nicklas |
TabControl.addTabActivateListener('settings.yubikey-tab', yubikey.autoFocus); |
2259 |
28 Feb 14 |
nicklas |
17 |
} |
2259 |
28 Feb 14 |
nicklas |
18 |
|
2488 |
05 Jun 14 |
nicklas |
// If the user has a YubiKey set focus to the serial number field, otherwise the YubiKey OTP field |
2488 |
05 Jun 14 |
nicklas |
yubikey.autoFocus = function() |
2488 |
05 Jun 14 |
nicklas |
21 |
{ |
2488 |
05 Jun 14 |
nicklas |
if (Doc.element('ep.yubiKeyId').value) |
2488 |
05 Jun 14 |
nicklas |
23 |
{ |
2488 |
05 Jun 14 |
nicklas |
Doc.element('yubiKeySerial').focus(); |
2488 |
05 Jun 14 |
nicklas |
25 |
} |
2488 |
05 Jun 14 |
nicklas |
else |
2488 |
05 Jun 14 |
nicklas |
27 |
{ |
2488 |
05 Jun 14 |
nicklas |
Doc.element('yubiKeyOtp').focus(); |
2488 |
05 Jun 14 |
nicklas |
29 |
} |
2488 |
05 Jun 14 |
nicklas |
30 |
} |
2488 |
05 Jun 14 |
nicklas |
31 |
|
2259 |
28 Feb 14 |
nicklas |
/* Copy serial number to extended properties tab */ |
2259 |
28 Feb 14 |
nicklas |
yubikey.copySerial = function() |
2259 |
28 Feb 14 |
nicklas |
34 |
{ |
2259 |
28 Feb 14 |
nicklas |
Doc.element('ep.yubiKeySerial').value = Doc.element('yubiKeySerial').value; |
2259 |
28 Feb 14 |
nicklas |
36 |
} |
2259 |
28 Feb 14 |
nicklas |
37 |
|
2259 |
28 Feb 14 |
nicklas |
/* Set serial number in all relevant places */ |
2259 |
28 Feb 14 |
nicklas |
yubikey.setSerial = function(serial) |
2259 |
28 Feb 14 |
nicklas |
40 |
{ |
2259 |
28 Feb 14 |
nicklas |
Doc.element('yubiKeySerial').value = serial; |
2259 |
28 Feb 14 |
nicklas |
Doc.element('ep.yubiKeySerial').value = serial; |
2259 |
28 Feb 14 |
nicklas |
43 |
} |
2259 |
28 Feb 14 |
nicklas |
44 |
|
2259 |
28 Feb 14 |
nicklas |
/* Set YubiKey ID in all relevant places */ |
2259 |
28 Feb 14 |
nicklas |
yubikey.setYubiKeyId = function(id) |
2259 |
28 Feb 14 |
nicklas |
47 |
{ |
2259 |
28 Feb 14 |
nicklas |
Doc.element('yubiKeyId').value = id; |
2259 |
28 Feb 14 |
nicklas |
Doc.element('ep.yubiKeyId').value = id; |
2259 |
28 Feb 14 |
nicklas |
Doc.element('yubiKeyId2').innerHTML = id ? id : '<i>No YubiKey</i>'; |
2259 |
28 Feb 14 |
nicklas |
51 |
} |
2259 |
28 Feb 14 |
nicklas |
52 |
|
2259 |
28 Feb 14 |
nicklas |
53 |
/* |
2259 |
28 Feb 14 |
nicklas |
Remove all YubiKey values for this user. |
2259 |
28 Feb 14 |
nicklas |
Switch to the non-YubiKey form |
2259 |
28 Feb 14 |
nicklas |
56 |
*/ |
2259 |
28 Feb 14 |
nicklas |
yubikey.removeYubiKey = function() |
2259 |
28 Feb 14 |
nicklas |
58 |
{ |
2259 |
28 Feb 14 |
nicklas |
yubikey.setSerial(''); |
2259 |
28 Feb 14 |
nicklas |
yubikey.setYubiKeyId(''); |
2259 |
28 Feb 14 |
nicklas |
61 |
|
2259 |
28 Feb 14 |
nicklas |
Doc.hide('has-yubikey'); |
2259 |
28 Feb 14 |
nicklas |
Doc.show('no-yubikey'); |
2259 |
28 Feb 14 |
nicklas |
Doc.element('yubiKeyOtp').value = ''; |
2259 |
28 Feb 14 |
nicklas |
Doc.element('yubiKeyOtp').focus(); |
2259 |
28 Feb 14 |
nicklas |
66 |
} |
2259 |
28 Feb 14 |
nicklas |
67 |
|
2259 |
28 Feb 14 |
nicklas |
68 |
/** |
2259 |
28 Feb 14 |
nicklas |
Verify that the one-time-password is ok. |
2259 |
28 Feb 14 |
nicklas |
70 |
*/ |
2259 |
28 Feb 14 |
nicklas |
yubikey.verifyOtp = function() |
2259 |
28 Feb 14 |
nicklas |
72 |
{ |
2259 |
28 Feb 14 |
nicklas |
var otp = Doc.element('yubiKeyOtp').value; |
2259 |
28 Feb 14 |
nicklas |
Doc.hide('yubikey-error'); |
2259 |
28 Feb 14 |
nicklas |
75 |
|
2259 |
28 Feb 14 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
2259 |
28 Feb 14 |
nicklas |
var response; |
2259 |
28 Feb 14 |
nicklas |
var error; |
2259 |
28 Feb 14 |
nicklas |
try |
2259 |
28 Feb 14 |
nicklas |
80 |
{ |
2259 |
28 Feb 14 |
nicklas |
Doc.show('yubikey-verifying'); |
2259 |
28 Feb 14 |
nicklas |
var home = Data.get('yubikey-data', 'home'); |
2259 |
28 Feb 14 |
nicklas |
var url = home + '/YubiKey.servlet?ID='+App.getSessionId(); |
2259 |
28 Feb 14 |
nicklas |
url += '&cmd=Verify&otp='+encodeURIComponent(otp); |
2259 |
28 Feb 14 |
nicklas |
request.open("GET", url, false); |
2259 |
28 Feb 14 |
nicklas |
request.send(null); |
2259 |
28 Feb 14 |
nicklas |
response = JSON.parse(request.responseText); |
2259 |
28 Feb 14 |
nicklas |
88 |
|
2259 |
28 Feb 14 |
nicklas |
if (response.status != 'ok') |
2259 |
28 Feb 14 |
nicklas |
90 |
{ |
2259 |
28 Feb 14 |
nicklas |
error = response.message; |
2259 |
28 Feb 14 |
nicklas |
92 |
} |
2259 |
28 Feb 14 |
nicklas |
93 |
} |
2259 |
28 Feb 14 |
nicklas |
catch (e) |
2259 |
28 Feb 14 |
nicklas |
95 |
{ |
2259 |
28 Feb 14 |
nicklas |
error = e; |
2259 |
28 Feb 14 |
nicklas |
97 |
} |
2259 |
28 Feb 14 |
nicklas |
finally |
2259 |
28 Feb 14 |
nicklas |
99 |
{ |
2259 |
28 Feb 14 |
nicklas |
Doc.hide('yubikey-verifying'); |
2259 |
28 Feb 14 |
nicklas |
101 |
} |
2259 |
28 Feb 14 |
nicklas |
102 |
|
2259 |
28 Feb 14 |
nicklas |
if (error) |
2259 |
28 Feb 14 |
nicklas |
104 |
{ |
2259 |
28 Feb 14 |
nicklas |
Doc.element('yubikey-error').innerHTML = Strings.encodeTags(error.toString ? error.toString() : error); |
2259 |
28 Feb 14 |
nicklas |
Doc.show('yubikey-error'); |
2259 |
28 Feb 14 |
nicklas |
return; |
2259 |
28 Feb 14 |
nicklas |
108 |
} |
2259 |
28 Feb 14 |
nicklas |
109 |
|
2259 |
28 Feb 14 |
nicklas |
var publicId = response.publicId; |
2259 |
28 Feb 14 |
nicklas |
111 |
|
2259 |
28 Feb 14 |
nicklas |
// Update form elements and switch to the has-YubiKey form |
2259 |
28 Feb 14 |
nicklas |
yubikey.setYubiKeyId(publicId); |
2259 |
28 Feb 14 |
nicklas |
Doc.show('has-yubikey'); |
2259 |
28 Feb 14 |
nicklas |
Doc.hide('no-yubikey'); |
2488 |
05 Jun 14 |
nicklas |
Doc.element('yubiKeySerial').focus(); |
2259 |
28 Feb 14 |
nicklas |
117 |
} |
2259 |
28 Feb 14 |
nicklas |
118 |
|
2259 |
28 Feb 14 |
nicklas |
return yubikey; |
2259 |
28 Feb 14 |
nicklas |
120 |
}(); |
2259 |
28 Feb 14 |
nicklas |
121 |
|
2259 |
28 Feb 14 |
nicklas |
Doc.onLoad(YubiKey.initPage); |
2259 |
28 Feb 14 |
nicklas |
123 |
|