4923 |
10 Aug 18 |
nicklas |
1 |
|
4923 |
10 Aug 18 |
nicklas |
var OTP = function() |
4923 |
10 Aug 18 |
nicklas |
3 |
{ |
4923 |
10 Aug 18 |
nicklas |
var otp = {}; |
4923 |
10 Aug 18 |
nicklas |
5 |
|
4923 |
10 Aug 18 |
nicklas |
otp.initPage = function() |
4923 |
10 Aug 18 |
nicklas |
7 |
{ |
4923 |
10 Aug 18 |
nicklas |
// Focs OTP when switching to this tab |
4923 |
10 Aug 18 |
nicklas |
TabControl.addTabActivateListener('settings.otp-tab', otp.setFocus); |
4923 |
10 Aug 18 |
nicklas |
10 |
|
4923 |
10 Aug 18 |
nicklas |
// Accept digits and generate QR code when ENTER is pressed |
4923 |
10 Aug 18 |
nicklas |
Events.addEventHandler('otp', 'keypress', Events.integerOnly); |
4923 |
10 Aug 18 |
nicklas |
Events.doOnEnter('otp', otp.loadQrCode); |
4923 |
10 Aug 18 |
nicklas |
14 |
} |
4923 |
10 Aug 18 |
nicklas |
15 |
|
4923 |
10 Aug 18 |
nicklas |
otp.setFocus = function() |
4923 |
10 Aug 18 |
nicklas |
17 |
{ |
4923 |
10 Aug 18 |
nicklas |
Doc.element('otp').focus(); |
4923 |
10 Aug 18 |
nicklas |
19 |
} |
4923 |
10 Aug 18 |
nicklas |
20 |
|
4923 |
10 Aug 18 |
nicklas |
otp.loadQrCode = function() |
4923 |
10 Aug 18 |
nicklas |
22 |
{ |
4923 |
10 Aug 18 |
nicklas |
var otpValue = Doc.element('otp').value; |
4923 |
10 Aug 18 |
nicklas |
var url = Data.get('otp-data', 'home') + '/OTP.servlet?ID='+App.getSessionId(); |
4923 |
10 Aug 18 |
nicklas |
url += '&cmd=DisplayQRCode&otp='+otpValue; |
4923 |
10 Aug 18 |
nicklas |
26 |
|
4923 |
10 Aug 18 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
4923 |
10 Aug 18 |
nicklas |
request.open('GET', url, true); |
4923 |
10 Aug 18 |
nicklas |
Ajax.setReadyStateHandler(request, otp.qrCodeLoaded, otp.qrCodeLoaded); |
4923 |
10 Aug 18 |
nicklas |
request.send(null); |
4923 |
10 Aug 18 |
nicklas |
31 |
} |
4923 |
10 Aug 18 |
nicklas |
32 |
|
4923 |
10 Aug 18 |
nicklas |
33 |
|
4923 |
10 Aug 18 |
nicklas |
otp.getResponse = function(request) |
4923 |
10 Aug 18 |
nicklas |
35 |
{ |
4923 |
10 Aug 18 |
nicklas |
var response; |
4923 |
10 Aug 18 |
nicklas |
var error = false; |
4923 |
10 Aug 18 |
nicklas |
try |
4923 |
10 Aug 18 |
nicklas |
39 |
{ |
4923 |
10 Aug 18 |
nicklas |
response = JSON.parse(request.responseText); |
4923 |
10 Aug 18 |
nicklas |
if (response.status != 'ok') |
4923 |
10 Aug 18 |
nicklas |
42 |
{ |
4923 |
10 Aug 18 |
nicklas |
error = response.message || response.stacktrace || 'Unexpected error'; |
4923 |
10 Aug 18 |
nicklas |
44 |
} |
4923 |
10 Aug 18 |
nicklas |
45 |
} |
4923 |
10 Aug 18 |
nicklas |
catch (ex) |
4923 |
10 Aug 18 |
nicklas |
47 |
{ |
4923 |
10 Aug 18 |
nicklas |
error = ex; |
4923 |
10 Aug 18 |
nicklas |
49 |
} |
4923 |
10 Aug 18 |
nicklas |
50 |
|
4923 |
10 Aug 18 |
nicklas |
if (error) |
4923 |
10 Aug 18 |
nicklas |
52 |
{ |
4923 |
10 Aug 18 |
nicklas |
otp.showError(Strings.encodeTags(error)); |
4923 |
10 Aug 18 |
nicklas |
return; |
4923 |
10 Aug 18 |
nicklas |
55 |
} |
4923 |
10 Aug 18 |
nicklas |
56 |
|
4923 |
10 Aug 18 |
nicklas |
return response; |
4923 |
10 Aug 18 |
nicklas |
58 |
} |
4923 |
10 Aug 18 |
nicklas |
59 |
|
4923 |
10 Aug 18 |
nicklas |
otp.qrCodeLoaded = function(request) |
4923 |
10 Aug 18 |
nicklas |
61 |
{ |
4923 |
10 Aug 18 |
nicklas |
var response = otp.getResponse(request); |
4923 |
10 Aug 18 |
nicklas |
if (!response) return; |
4923 |
10 Aug 18 |
nicklas |
64 |
|
4923 |
10 Aug 18 |
nicklas |
Doc.element('qrcode').src = 'data:image/png;base64,'+response.image; |
4923 |
10 Aug 18 |
nicklas |
Doc.show('qrcode', 'inline'); |
4923 |
10 Aug 18 |
nicklas |
Doc.hide('no-qrcode'); |
4924 |
13 Aug 18 |
nicklas |
var msg = 'Scan the QR code with your device!<br><br>'; |
4924 |
13 Aug 18 |
nicklas |
msg += Strings.encodeTags(response.issuer + ' (' + response.username + ')'); |
4924 |
13 Aug 18 |
nicklas |
Doc.element('message').innerHTML = msg; |
4923 |
10 Aug 18 |
nicklas |
71 |
} |
4923 |
10 Aug 18 |
nicklas |
72 |
|
4923 |
10 Aug 18 |
nicklas |
otp.showError = function(message, focusElement) |
4923 |
10 Aug 18 |
nicklas |
74 |
{ |
4924 |
13 Aug 18 |
nicklas |
Doc.element('message').innerHTML = '<div class="error">'+Strings.encodeTags(message)+'</div>'; |
4923 |
10 Aug 18 |
nicklas |
Doc.element('otp').focus(); |
4923 |
10 Aug 18 |
nicklas |
Doc.element('otp').select(); |
4923 |
10 Aug 18 |
nicklas |
78 |
} |
4923 |
10 Aug 18 |
nicklas |
79 |
|
4923 |
10 Aug 18 |
nicklas |
return otp; |
4923 |
10 Aug 18 |
nicklas |
81 |
}(); |
4923 |
10 Aug 18 |
nicklas |
82 |
|
4923 |
10 Aug 18 |
nicklas |
Doc.onLoad(OTP.initPage); |
4923 |
10 Aug 18 |
nicklas |
84 |
|