4849 |
13 Jun 18 |
nicklas |
'use strict'; |
4849 |
13 Jun 18 |
nicklas |
2 |
|
4849 |
13 Jun 18 |
nicklas |
var SetupOTP = function() |
4849 |
13 Jun 18 |
nicklas |
4 |
{ |
4849 |
13 Jun 18 |
nicklas |
var setup = {}; |
4849 |
13 Jun 18 |
nicklas |
6 |
|
4849 |
13 Jun 18 |
nicklas |
setup.initPage = function() |
4849 |
13 Jun 18 |
nicklas |
8 |
{ |
4850 |
14 Jun 18 |
nicklas |
Events.addEventHandler('username', 'change', setup.getQrCode); |
4850 |
14 Jun 18 |
nicklas |
10 |
|
4850 |
14 Jun 18 |
nicklas |
Events.addEventHandler('username', 'keypress', setup.focusOnEnter); |
4850 |
14 Jun 18 |
nicklas |
Events.addEventHandler('password', 'keypress', setup.focusOnEnter); |
4857 |
18 Jun 18 |
nicklas |
Events.addEventHandler('newpw', 'keypress', setup.focusOnEnter); |
4857 |
18 Jun 18 |
nicklas |
Events.addEventHandler('newpw2', 'keypress', setup.focusOnEnter); |
4851 |
14 Jun 18 |
nicklas |
Events.doOnEnter('otp', setup.save); |
4850 |
14 Jun 18 |
nicklas |
16 |
|
4857 |
18 Jun 18 |
nicklas |
Events.addEventHandler('change-pwd', 'change', setup.enableChangePassword); |
4857 |
18 Jun 18 |
nicklas |
18 |
|
4849 |
13 Jun 18 |
nicklas |
Buttons.addClickHandler('btnCancel', App.closeWindow); |
4851 |
14 Jun 18 |
nicklas |
Buttons.addClickHandler('btnClose', App.closeWindow); |
4851 |
14 Jun 18 |
nicklas |
Buttons.addClickHandler('btnSave', setup.save); |
4854 |
15 Jun 18 |
nicklas |
22 |
|
4854 |
15 Jun 18 |
nicklas |
var username = Doc.element('username').value; |
4854 |
15 Jun 18 |
nicklas |
24 |
|
4854 |
15 Jun 18 |
nicklas |
if (username) |
4854 |
15 Jun 18 |
nicklas |
26 |
{ |
4854 |
15 Jun 18 |
nicklas |
Events.sendChangeEvent('username'); |
4854 |
15 Jun 18 |
nicklas |
Doc.element('password').focus(); |
4854 |
15 Jun 18 |
nicklas |
29 |
} |
4854 |
15 Jun 18 |
nicklas |
else |
4854 |
15 Jun 18 |
nicklas |
31 |
{ |
4854 |
15 Jun 18 |
nicklas |
Doc.element('username').focus(); |
4854 |
15 Jun 18 |
nicklas |
33 |
} |
4849 |
13 Jun 18 |
nicklas |
34 |
} |
4850 |
14 Jun 18 |
nicklas |
35 |
|
4850 |
14 Jun 18 |
nicklas |
setup.getQrCode = function() |
4850 |
14 Jun 18 |
nicklas |
37 |
{ |
4850 |
14 Jun 18 |
nicklas |
var username = Doc.element('username').value; |
4850 |
14 Jun 18 |
nicklas |
if (username == '') |
4850 |
14 Jun 18 |
nicklas |
40 |
{ |
4857 |
18 Jun 18 |
nicklas |
Doc.show('no-qrcode', 'inline-block'); |
4850 |
14 Jun 18 |
nicklas |
Doc.hide('qrcode'); |
4850 |
14 Jun 18 |
nicklas |
return; |
4850 |
14 Jun 18 |
nicklas |
44 |
} |
4850 |
14 Jun 18 |
nicklas |
45 |
|
4850 |
14 Jun 18 |
nicklas |
var url = 'OTP.servlet?ID='+App.getSessionId(); |
4850 |
14 Jun 18 |
nicklas |
url += '&cmd=CreateQRCode&username='+encodeURIComponent(username); |
4850 |
14 Jun 18 |
nicklas |
48 |
|
4850 |
14 Jun 18 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
4850 |
14 Jun 18 |
nicklas |
request.open('GET', url, true); |
4850 |
14 Jun 18 |
nicklas |
Ajax.setReadyStateHandler(request, setup.qrCodeLoaded, setup.qrCodeLoaded); |
4850 |
14 Jun 18 |
nicklas |
request.send(null); |
4850 |
14 Jun 18 |
nicklas |
53 |
} |
4850 |
14 Jun 18 |
nicklas |
54 |
|
4851 |
14 Jun 18 |
nicklas |
setup.getResponse = function(request) |
4850 |
14 Jun 18 |
nicklas |
56 |
{ |
4850 |
14 Jun 18 |
nicklas |
var response; |
4850 |
14 Jun 18 |
nicklas |
var error = false; |
4850 |
14 Jun 18 |
nicklas |
try |
4850 |
14 Jun 18 |
nicklas |
60 |
{ |
4850 |
14 Jun 18 |
nicklas |
response = JSON.parse(request.responseText); |
4850 |
14 Jun 18 |
nicklas |
if (response.status != 'ok') |
4850 |
14 Jun 18 |
nicklas |
63 |
{ |
4850 |
14 Jun 18 |
nicklas |
error = response.message || response.stacktrace || 'Unexpected error'; |
4850 |
14 Jun 18 |
nicklas |
65 |
} |
4850 |
14 Jun 18 |
nicklas |
66 |
} |
4850 |
14 Jun 18 |
nicklas |
catch (ex) |
4850 |
14 Jun 18 |
nicklas |
68 |
{ |
4850 |
14 Jun 18 |
nicklas |
error = ex; |
4850 |
14 Jun 18 |
nicklas |
70 |
} |
4850 |
14 Jun 18 |
nicklas |
71 |
|
4850 |
14 Jun 18 |
nicklas |
if (error) |
4850 |
14 Jun 18 |
nicklas |
73 |
{ |
4851 |
14 Jun 18 |
nicklas |
setup.showError(Strings.encodeTags(error)); |
4850 |
14 Jun 18 |
nicklas |
return; |
4850 |
14 Jun 18 |
nicklas |
76 |
} |
4851 |
14 Jun 18 |
nicklas |
77 |
|
4851 |
14 Jun 18 |
nicklas |
return response; |
4851 |
14 Jun 18 |
nicklas |
79 |
} |
4851 |
14 Jun 18 |
nicklas |
80 |
|
4851 |
14 Jun 18 |
nicklas |
setup.qrCodeLoaded = function(request) |
4851 |
14 Jun 18 |
nicklas |
82 |
{ |
4851 |
14 Jun 18 |
nicklas |
var response = setup.getResponse(request); |
4851 |
14 Jun 18 |
nicklas |
if (!response) return; |
4851 |
14 Jun 18 |
nicklas |
85 |
|
4850 |
14 Jun 18 |
nicklas |
Doc.element('qrcode').src = 'data:image/png;base64,'+response.image; |
4850 |
14 Jun 18 |
nicklas |
Doc.show('qrcode', 'inline'); |
4850 |
14 Jun 18 |
nicklas |
Doc.hide('no-qrcode'); |
4850 |
14 Jun 18 |
nicklas |
89 |
} |
4850 |
14 Jun 18 |
nicklas |
90 |
|
4857 |
18 Jun 18 |
nicklas |
setup.enableChangePassword = function() |
4857 |
18 Jun 18 |
nicklas |
92 |
{ |
4857 |
18 Jun 18 |
nicklas |
Doc.hide('change-password'); |
4857 |
18 Jun 18 |
nicklas |
Doc.show('new-password-1'); |
4857 |
18 Jun 18 |
nicklas |
Doc.show('new-password-2'); |
4857 |
18 Jun 18 |
nicklas |
Doc.element('newpw').focus(); |
4857 |
18 Jun 18 |
nicklas |
97 |
} |
4857 |
18 Jun 18 |
nicklas |
98 |
|
4851 |
14 Jun 18 |
nicklas |
setup.showError = function(message, focusElement) |
4851 |
14 Jun 18 |
nicklas |
100 |
{ |
4851 |
14 Jun 18 |
nicklas |
Doc.element('message').innerHTML = '<div class="error">'+message+'</div>'; |
4851 |
14 Jun 18 |
nicklas |
if (focusElement) Doc.element(focusElement).focus(); |
4851 |
14 Jun 18 |
nicklas |
103 |
} |
4849 |
13 Jun 18 |
nicklas |
104 |
|
4850 |
14 Jun 18 |
nicklas |
setup.focusOnEnter = function(event) |
4850 |
14 Jun 18 |
nicklas |
106 |
{ |
4850 |
14 Jun 18 |
nicklas |
if (event.keyCode == 13) |
4850 |
14 Jun 18 |
nicklas |
108 |
{ |
4850 |
14 Jun 18 |
nicklas |
var nextFocus = Doc.element(Data.get(event.currentTarget, 'next-focus')); |
4850 |
14 Jun 18 |
nicklas |
if (nextFocus && nextFocus.focus) nextFocus.focus(); |
4850 |
14 Jun 18 |
nicklas |
111 |
} |
4850 |
14 Jun 18 |
nicklas |
112 |
} |
4850 |
14 Jun 18 |
nicklas |
113 |
|
4851 |
14 Jun 18 |
nicklas |
setup.save = function() |
4851 |
14 Jun 18 |
nicklas |
115 |
{ |
4851 |
14 Jun 18 |
nicklas |
var username = Doc.element('username').value; |
4851 |
14 Jun 18 |
nicklas |
var password = Doc.element('password').value; |
4851 |
14 Jun 18 |
nicklas |
var otp = Doc.element('otp').value; |
4857 |
18 Jun 18 |
nicklas |
var newPw = Doc.element('newpw').value; |
4857 |
18 Jun 18 |
nicklas |
var newPw2 = Doc.element('newpw2').value; |
4857 |
18 Jun 18 |
nicklas |
121 |
|
4851 |
14 Jun 18 |
nicklas |
if (username == '') |
4851 |
14 Jun 18 |
nicklas |
123 |
{ |
4851 |
14 Jun 18 |
nicklas |
setup.showError('A username must be specified', 'username'); |
4851 |
14 Jun 18 |
nicklas |
return; |
4851 |
14 Jun 18 |
nicklas |
126 |
} |
4851 |
14 Jun 18 |
nicklas |
else if (password == '') |
4851 |
14 Jun 18 |
nicklas |
128 |
{ |
4851 |
14 Jun 18 |
nicklas |
setup.showError('A password must be specified', 'password'); |
4851 |
14 Jun 18 |
nicklas |
return; |
4851 |
14 Jun 18 |
nicklas |
131 |
} |
4851 |
14 Jun 18 |
nicklas |
else if (otp == '') |
4851 |
14 Jun 18 |
nicklas |
133 |
{ |
4851 |
14 Jun 18 |
nicklas |
setup.showError('A one-time passcode must be specified', 'otp'); |
4851 |
14 Jun 18 |
nicklas |
return; |
4851 |
14 Jun 18 |
nicklas |
136 |
} |
4857 |
18 Jun 18 |
nicklas |
else if (newPw != newPw2) |
4857 |
18 Jun 18 |
nicklas |
138 |
{ |
4857 |
18 Jun 18 |
nicklas |
setup.showError('The new passwords do not match', 'newpw2'); |
4857 |
18 Jun 18 |
nicklas |
return; |
4857 |
18 Jun 18 |
nicklas |
141 |
} |
4851 |
14 Jun 18 |
nicklas |
142 |
|
4851 |
14 Jun 18 |
nicklas |
var url = 'OTP.servlet?ID='+App.getSessionId(); |
4851 |
14 Jun 18 |
nicklas |
url += '&cmd=SaveOTPSetup'; |
4851 |
14 Jun 18 |
nicklas |
145 |
|
4851 |
14 Jun 18 |
nicklas |
var postData = {}; |
4851 |
14 Jun 18 |
nicklas |
postData.username = username; |
4851 |
14 Jun 18 |
nicklas |
postData.password = password; |
4851 |
14 Jun 18 |
nicklas |
postData.otp = otp; |
4857 |
18 Jun 18 |
nicklas |
if (newPw) |
4857 |
18 Jun 18 |
nicklas |
151 |
{ |
4857 |
18 Jun 18 |
nicklas |
postData.newPassword = newPw; |
4857 |
18 Jun 18 |
nicklas |
153 |
} |
4851 |
14 Jun 18 |
nicklas |
154 |
|
4851 |
14 Jun 18 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
4851 |
14 Jun 18 |
nicklas |
request.open('POST', url, true); |
4851 |
14 Jun 18 |
nicklas |
request.setRequestHeader("Content-Type", "application/json; charset=UTF-8"); |
4851 |
14 Jun 18 |
nicklas |
Ajax.setReadyStateHandler(request, setup.setupCompleted, setup.setupCompleted); |
4851 |
14 Jun 18 |
nicklas |
request.send(JSON.stringify(postData)); |
4851 |
14 Jun 18 |
nicklas |
160 |
} |
4851 |
14 Jun 18 |
nicklas |
161 |
|
4851 |
14 Jun 18 |
nicklas |
setup.setupCompleted = function(request) |
4851 |
14 Jun 18 |
nicklas |
163 |
{ |
4851 |
14 Jun 18 |
nicklas |
var response = setup.getResponse(request); |
4851 |
14 Jun 18 |
nicklas |
if (!response) return; |
4851 |
14 Jun 18 |
nicklas |
166 |
|
4857 |
18 Jun 18 |
nicklas |
Doc.element('message').innerHTML = '<div class="success bg-filled-100">'+Strings.encodeTags(response.message)+'</div>'; |
4851 |
14 Jun 18 |
nicklas |
168 |
|
4851 |
14 Jun 18 |
nicklas |
Doc.hide('setup-buttons'); |
4851 |
14 Jun 18 |
nicklas |
Doc.hide('setup-form'); |
4851 |
14 Jun 18 |
nicklas |
Doc.show('close-button'); |
4851 |
14 Jun 18 |
nicklas |
172 |
} |
4851 |
14 Jun 18 |
nicklas |
173 |
|
4849 |
13 Jun 18 |
nicklas |
return setup; |
4849 |
13 Jun 18 |
nicklas |
175 |
}(); |
4849 |
13 Jun 18 |
nicklas |
176 |
|
4849 |
13 Jun 18 |
nicklas |
Doc.onLoad(SetupOTP.initPage); |
4849 |
13 Jun 18 |
nicklas |
178 |
|