forked from alexandregz/twofactor_gauthenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwofactor_gauthenticator.js
153 lines (125 loc) · 5.92 KB
/
twofactor_gauthenticator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
if (window.rcmail) {
rcmail.addEventListener('init', function(evt) {
// ripped from PHPGansta/GoogleAuthenticator.php
function createSecret(secretLength)
{
if(!secretLength) secretLength = 16;
LookupTable = new Array(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
'Y', 'Z', '2', '3', '4', '5', '6', '7' // 31
//'=' // padding char
);
secret = '';
for (i = 0; i < secretLength; i++) {
secret += LookupTable[Math.floor(Math.random()*LookupTable.length)];
}
return secret;
}
// populate all fields
function setup2FAfields() {
if($('#2FA_secret').get(0).value) return;
$('#twofactor_gauthenticator-form :input').each(function(){
if($(this).get(0).type == 'password') $(this).get(0).type = 'text';
});
// secret button
$('#2FA_create_secret').prop('id', '2FA_change_secret');
$('#2FA_change_secret').get(0).value = rcmail.gettext('hide_secret', 'twofactor_gauthenticator');
$('#2FA_change_secret').click(click2FA_change_secret);
$('#2FA_activate').prop('checked', true);
$('#2FA_show_recovery_codes').get(0).value = rcmail.gettext('hide_recovery_codes', 'twofactor_gauthenticator');
$('#2FA_qr_code').slideDown();
$('#2FA_secret').get(0).value = createSecret();
$("[name^='2FA_recovery_codes']").each(function() {
$(this).get(0).value = createSecret(10);
});
// add qr-code before msg_infor
var url_qr_code_values = 'otpauth://totp/' +$('#prefs-title').html().split(/ - /)[1]+ '?secret=' +$('#2FA_secret').get(0).value +'&issuer=RoundCube2FA%20'+window.location.hostname;
$('table tr:last').before('<tr><td>' +rcmail.gettext('qr_code', 'twofactor_gauthenticator')+ '</td><td><input type="button" class="button mainaction" id="2FA_change_qr_code" value="'
+rcmail.gettext('hide_qr_code', 'twofactor_gauthenticator')+ '"><div id="2FA_qr_code" style="display: visible; margin-top: 10px;"></div></td></tr>');
var qrcode = new QRCode(document.getElementById("2FA_qr_code"), {
text: url_qr_code_values,
width: 200,
height: 200,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.L // like charts.googleapis.com
});
$('#2FA_change_qr_code').click(click2FA_change_qr_code);
$('#2FA_qr_code').prop('title', ''); // enjoy the silence (qrcode.js uses text to set title)
// disable save button. It needs check code to enabled again
$('#2FA_setup_fields').prev().attr('disabled','disabled').attr('title', rcmail.gettext('check_code_to_activate', 'twofactor_gauthenticator'));
alert(rcmail.gettext('check_code_to_activate', 'twofactor_gauthenticator'));
}
$('#2FA_setup_fields').click(function(){
setup2FAfields();
});
// to show/hide secret
click2FA_change_secret = function(){
if($('#2FA_secret').get(0).type == 'text') {
$('#2FA_secret').get(0).type = 'password';
$('#2FA_change_secret').get(0).value = rcmail.gettext('show_secret', 'twofactor_gauthenticator');
}
else
{
$('#2FA_secret').get(0).type = 'text';
$('#2FA_change_secret').get(0).value = rcmail.gettext('hide_secret', 'twofactor_gauthenticator');
}
};
$('#2FA_change_secret').click(click2FA_change_secret);
// to show/hide recovery_codes
$('#2FA_show_recovery_codes').click(function(){
if($("[name^='2FA_recovery_codes']")[0].type == 'text') {
$("[name^='2FA_recovery_codes']").each(function() {
$(this).get(0).type = 'password';
});
$('#2FA_show_recovery_codes').get(0).value = rcmail.gettext('show_recovery_codes', 'twofactor_gauthenticator');
}
else {
$("[name^='2FA_recovery_codes']").each(function() {
$(this).get(0).type = 'text';
});
$('#2FA_show_recovery_codes').get(0).value = rcmail.gettext('hide_recovery_codes', 'twofactor_gauthenticator');
}
});
// to show/hide qr_code
click2FA_change_qr_code = function(){
if( $('#2FA_qr_code').is(':visible') ) {
$('#2FA_qr_code').slideUp();
$(this).get(0).value = rcmail.gettext('show_qr_code', 'twofactor_gauthenticator');
}
else {
$('#2FA_qr_code').slideDown();
$(this).get(0).value = rcmail.gettext('hide_qr_code', 'twofactor_gauthenticator');
}
}
$('#2FA_change_qr_code').click(click2FA_change_qr_code);
// create secret
$('#2FA_create_secret').click(function(){
$('#2FA_secret').get(0).value = createSecret();
});
// ajax
$('#2FA_check_code').click(function(){
url = "./?_action=plugin.twofactor_gauthenticator-checkcode&code=" +$('#2FA_code_to_check').val() + '&secret='+$('#2FA_secret').val();
$.post(url, function(data){
alert(data);
if(data == rcmail.gettext('code_ok', 'twofactor_gauthenticator'))
$('#2FA_setup_fields').prev().removeAttr('disabled');
});
});
// Define Variables
var tabtwofactorgauthenticator = $('<span>').attr('id', 'settingstabplugintwofactor_gauthenticator').addClass('tablink');
var button = $('<a>').attr('href', rcmail.env.comm_path + '&_action=plugin.twofactor_gauthenticator').html(rcmail.gettext('twofactor_gauthenticator', 'twofactor_gauthenticator')).appendTo(tabtwofactorgauthenticator);
button.bind('click', function(e){ return rcmail.command('plugin.twofactor_gauthenticator', this) });
// Button & Register commands
rcmail.add_element(tabtwofactorgauthenticator, 'tabs');
rcmail.register_command('plugin.twofactor_gauthenticator', function() { rcmail.goto_url('plugin.twofactor_gauthenticator') }, true);
rcmail.register_command('plugin.twofactor_gauthenticator-save', function() {
if(!$('#2FA_secret').get(0).value) {
$('#2FA_secret').get(0).value = createSecret();
}
rcmail.gui_objects.twofactor_gauthenticatorform.submit();
}, true);
});
}