Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Replace 2FA script with stimulus controller

Oliver Güntherdef3396d95f5
diff --git a/app/views/users/_simple_form.html.erb b/app/views/users/_simple_form.html.erb
index 56de57cc072..c0efc3cb5f6 100644
--- a/app/views/users/_simple_form.html.erb
+++ b/app/views/users/_simple_form.html.erb
@@ -27,10 +27,6 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
-<% content_for :header_tags do %>
- <meta name="required_script" content="two_factor_authentication" />
-<% end %>
-
<section class="form--section">
<%= render partial: 'users/form/basic_attributes', locals: { f: f } %>
diff --git a/frontend/src/app/core/augmenting/dynamic-scripts/two_factor_authentication.ts b/frontend/src/app/core/augmenting/dynamic-scripts/two_factor_authentication.ts
deleted file mode 100644
index 27bfff66819..00000000000
--- a/frontend/src/app/core/augmenting/dynamic-scripts/two_factor_authentication.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-import QrCreator from 'qr-creator';
-
-jQuery(($) => {
- $('#submit_otp').submit(() => {
- $('.ajax_form').find('input, radio').attr('disabled', 'disabled');
- });
-
- $('#toggle_resend_form').click(() => {
- $('#resend_otp_container').toggle();
- return false;
- });
-
- $('.qr-code-element').each(function () {
- QrCreator.render({
- text: this.dataset.value as string,
- radius: 0,
- ecLevel: 'H',
- fill: '#222222',
- background: '#FFFFFF',
- size: 250,
- }, this);
- });
-
- $('.ajax_form').submit(function () {
- $('#submit_otp').find('input').attr('disabled', 'disabled');
- const form = $(this);
- const submit_button = form.find('input[type=submit]');
- $.ajax({
- url: form.attr('action'),
- type: 'post',
- data: form.serialize(),
- beforeSend() {
- submit_button.attr('disabled', 'disabled');
- submit_button.toggleClass('submitting');
- $('.flash.notice').toggle();
- },
- complete(response) {
- submit_button.removeAttr('disabled');
- $('#submit_otp').find('input').removeAttr('disabled');
- $('.flash.notice a').html(response.responseText);
- $('form#resend_otp, #toggle_resend_form, .flash.notice').toggle();
- submit_button.toggleClass('submitting');
- },
- });
- return false;
- });
-
- $('#print_2fa_backup_codes').click(() => {
- window.print();
- });
-
- if ($('#download_2fa_backup_codes').length) {
- let text = '';
- $('.two-factor-authentication--backup-codes li').each(function () {
- text += `${this.textContent}\n`;
- });
- const element = $('#download_2fa_backup_codes');
- element.attr('href', `data:text/plain;charset=utf-8,${encodeURIComponent(text)}`);
- element.attr('download', 'backup-codes.txt');
- }
-});
diff --git a/frontend/src/stimulus/controllers/dynamic/two-factor-authentication.controller.ts b/frontend/src/stimulus/controllers/dynamic/two-factor-authentication.controller.ts
new file mode 100644
index 00000000000..fd1b2fe5267
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/two-factor-authentication.controller.ts
@@ -0,0 +1,65 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 2023 the OpenProject GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 3.
+ *
+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+ * Copyright (C) 2006-2013 Jean-Philippe Lang
+ * Copyright (C) 2010-2013 the ChiliProject Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * See COPYRIGHT and LICENSE files for more details.
+ * ++
+ */
+
+import { Controller } from '@hotwired/stimulus';
+import QrCreator from 'qr-creator';
+
+export default class TwoFactorAuthenticationController extends Controller {
+ static targets = [
+ 'resendOptions',
+ 'qrCodeElement',
+ ];
+
+ declare readonly resendOptionsTarget:HTMLElement;
+
+ qrCodeElementTargetConnected(target:HTMLElement) {
+ QrCreator.render(
+ {
+ text: target.dataset.value as string,
+ radius: 0,
+ ecLevel: 'H',
+ fill: '#222222',
+ background: '#FFFFFF',
+ size: 250,
+ },
+ target,
+ );
+ }
+
+ print(evt:MouseEvent) {
+ evt.preventDefault();
+ window.print();
+ }
+
+ toggleResendOptions(evt:MouseEvent) {
+ evt.preventDefault();
+ this.resendOptionsTarget.hidden = !this.resendOptionsTarget.hidden;
+ }
+}
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/_header_tags.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/_header_tags.html.erb
deleted file mode 100644
index 8bdcba2eb8a..00000000000
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/_header_tags.html.erb
+++ /dev/null
@@ -1,3 +0,0 @@
-<% content_for :header_tags do %>
- <meta name="required_script" content="two_factor_authentication" />
-<% end %>
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/enter_backup_code.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/enter_backup_code.html.erb
index 4570b3fe490..53deeccebc6 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/enter_backup_code.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/enter_backup_code.html.erb
@@ -1,5 +1,3 @@
-<%= render partial: 'two_factor_authentication/header_tags' %>
-
<% html_title t(:field_otp) %>
<% breadcrumb_paths(t(:field_otp)) %>
<div id="login-form" class="form -bordered">
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/request_otp.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/request_otp.html.erb
index c71e8e70969..afef8a19b63 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/request_otp.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/authentication/request_otp.html.erb
@@ -2,64 +2,79 @@
<% has_other_devices = @active_devices.count > 1 %>
<% has_backup_codes = @authenticated_user.otp_backup_codes.exists? %>
-<%= render partial: 'two_factor_authentication/header_tags' %>
-
<% html_title t(:field_otp) %>
<% breadcrumb_paths(t(:field_otp)) %>
-<div id="login-form" class="form -bordered">
- <%= styled_form_tag({ action: :confirm_otp }, { :autocomplete => "off", :id => 'submit_otp' }) do %>
+<div id="login-form"
+ class="form -bordered"
+ data-controller="two-factor-authentication"
+ data-application-target="dynamic"
+>
+ <%= styled_form_tag(
+ { action: :confirm_otp },
+ id: 'submit_otp',
+ autocomplete: "off") do %>
<h2><%= t 'two_factor_authentication.label_two_factor_authentication' %></h2>
- <% identifier = @used_device.request_2fa_identifier(@strategy.channel) %>
- <p><%= raw I18n.t('two_factor_authentication.devices.request_2fa', device_name: identifier) %></p>
- <hr class="form--separator">
+ <% identifier = @used_device.request_2fa_identifier(@strategy.channel) %>
+ <p><%= raw I18n.t('two_factor_authentication.devices.request_2fa', device_name: identifier) %></p>
+ <hr class="form--separator">
<%= back_url_hidden_field_tag %>
<div class="form--field -wide-label">
<%= styled_label_tag 'otp', t(:field_otp) %>
<div class="form--field-container">
- <%= styled_text_field_tag 'otp', nil, autocomplete: 'off', size: 6, maxlength: 6, tabindex: 1, autofocus: true %>
+ <%= styled_text_field_tag 'otp', nil, autocomplete: 'off', size: 6, maxlength: 6, tabindex: 1, autofocus: true %>
</div>
</div>
<% if remember_2fa_enabled? %>
- <div class="form--field -wide-label">
- <label class="form--label-with-check-box -no-ellipsis">
- <div class="form--check-box-container">
- <%= styled_check_box_tag 'remember_me', true, false %>
- </div>
- <%= t 'two_factor_authentication.remember.dont_ask_again', days: remember_2fa_days %>
- </label>
- </div>
- <% end %>
- <input type="submit" name="login" value="<%=t(:button_login)%>" class="button -highlight" tabindex="2" />
- <% if resend_supported || has_other_devices || has_backup_codes %>
- <div class="login-options-container">
- <div class="login-links">
- <%= link_to(t(:text_otp_not_receive), '#', id: 'toggle_resend_form', tabindex: 3) %>
+ <div class="form--field -wide-label">
+ <label class="form--label-with-check-box -no-ellipsis">
+ <div class="form--check-box-container">
+ <%= styled_check_box_tag 'remember_me', true, false %>
</div>
+ <%= t 'two_factor_authentication.remember.dont_ask_again', days: remember_2fa_days %>
+ </label>
+ </div>
+ <% end %>
+ <input type="submit" name="login" value="<%= t(:button_login) %>" class="button -highlight" tabindex="2"/>
+ <% if resend_supported || has_other_devices || has_backup_codes %>
+ <div class="login-options-container">
+ <div class="login-links">
+ <%= link_to(
+ t(:text_otp_not_receive),
+ '#',
+ id: 'toggle_resend_form',
+ tabindex: 3,
+ data: { action: 'two-factor-authentication#toggleResendOptions' }
+ ) %>
</div>
- <% end %>
+ </div>
+ <% end %>
<% end %>
- <div id="resend_otp_container" style="display: none">
+ <div
+ id="resend_otp_container"
+ data-two-factor-authentication-target="resendOptions"
+ hidden
+ >
<% if resend_supported %>
- <%= styled_form_tag({action: :retry }, autocomplete: 'off', id: 'resend_otp') do %>
+ <%= styled_form_tag({ action: :retry }, autocomplete: 'off', id: 'resend_otp') do %>
<%= hidden_field_tag 'use_device', @service.device.id %>
- <hr />
+ <hr/>
<div class="resend-header"><%= t(:text_send_otp_again) %></div>
<div class="radios-wrapper">
<% default_channel = @service.device.channel %>
<% supported_channels = @used_device.class.supported_channels & @strategy.class.supported_channels %>
<% supported_channels.each do |channel| %>
- <%= radio_button_tag 'use_channel', channel, default_channel == channel %>
- <label for="channel_<%= channel %>" ><%= t("button_otp_by_#{channel}") %></label>
+ <%= radio_button_tag 'use_channel', channel, default_channel == channel %>
+ <label for="channel_<%= channel %>"><%= t("button_otp_by_#{channel}") %></label>
<% end %>
</div>
- <input type="submit" value="<%= t(:button_resend_otp_form) %>" id="submit_resend_form" class="button -highlight" />
+ <input type="submit" value="<%= t(:button_resend_otp_form) %>" id="submit_resend_form" class="button -highlight"/>
<% end %>
<% end %>
<% if has_other_devices %>
<hr/>
<div class="resend-header"><%= t('two_factor_authentication.login.other_device') %></div>
- <ul>
+ <ul>
<% @active_devices.each do |device| %>
<% next if device.id == @used_device.id %>
<li>
@@ -69,7 +84,7 @@
<% end %>
</li>
<% end %>
- </ul>
+ </ul>
<% end %>
<% if has_backup_codes %>
<ul>
@@ -78,6 +93,6 @@
</a>
</li>
</ul>
- <% end %>
+ <% end %>
</div>
</div>
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/my/backup_codes/show.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/my/backup_codes/show.html.erb
index f0d4a40e7cb..216f4679126 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/my/backup_codes/show.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/my/backup_codes/show.html.erb
@@ -1,4 +1,3 @@
-<%= render partial: '/two_factor_authentication/header_tags' %>
<% html_title(t(:label_my_account), t('two_factor_authentication.backup_codes.plural')) -%>
<h1>
@@ -20,20 +19,31 @@
<div class="two-factor-authentication--backup-codes">
<ul>
<% @backup_codes.each do |code| %>
- <li><%= code %></li>
+ <li><%= code %></li>
<% end %>
</ul>
</div>
<hr class="hide-when-print"/>
-<div class="two-factor-authentication--backup-actions">
- <a id="download_2fa_backup_codes" class="button hide-when-print">
+<div
+ data-controller="two-factor-authentication"
+ data-application-target="dynamic"
+>
+ <%= content_tag :a,
+ id: 'download_2fa_backup_codes',
+ class: "button hide-when-print",
+ download: 'backup-codes.txt',
+ href: "data:text/plain;charset=utf-8,#{CGI.escape(@backup_codes.join("\n"))}" do %>
<%= op_icon 'button--icon icon-save' %>
<span class="button--text"><%= t(:button_download) %></span>
- </a>
- <button id="print_2fa_backup_codes" class="button hide-when-print">
- <%= op_icon 'button--icon icon-print' %>
- <span class="button--text"><%= t(:button_print) %></span>
- </button>
+ <% end %>
+ <button
+ id="print_2fa_backup_codes"
+ class="button hide-when-print"
+ data-action="two-factor-authentication#print"
+ >
+ <%= op_icon 'button--icon icon-print' %>
+ <span class="button--text"><%= t(:button_print) %></span>
+ </button>
</div>
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/my/two_factor_devices/index.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/my/two_factor_devices/index.html.erb
index c20a2c44329..b713eba3722 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/my/two_factor_devices/index.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/my/two_factor_devices/index.html.erb
@@ -1,4 +1,3 @@
-<%= render partial: '/two_factor_authentication/header_tags' %>
<% html_title(t(:label_my_account), t('two_factor_authentication.label_two_factor_authentication')) -%>
<% if @has_remember_token_for_user %>
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/confirm.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/confirm.html.erb
index 84fdcd247cc..72c1d31c833 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/confirm.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/confirm.html.erb
@@ -1,4 +1,3 @@
-<%= render partial: '/two_factor_authentication/header_tags' %>
<% html_title(t(:label_my_account), t('two_factor_authentication.devices.confirm')) -%>
<% breadcrumb_paths link_to(t('two_factor_authentication.label_two_factor_authentication'), index_path),
t('two_factor_authentication.devices.confirm') -%>
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb
index d9ff5316a24..d10a1152022 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new.html.erb
@@ -1,5 +1,3 @@
-<%= render partial: '/two_factor_authentication/header_tags' %>
-
<% html_title(t(:label_my_account), t('two_factor_authentication.devices.add_new')) -%>
<% breadcrumb_paths link_to(t('two_factor_authentication.label_two_factor_authentication'), index_path),
t('two_factor_authentication.devices.add_new') -%>
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new_type.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new_type.html.erb
index d6f54bb62f5..0251cffe860 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new_type.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/new_type.html.erb
@@ -1,5 +1,3 @@
-<%= render partial: 'two_factor_authentication/header_tags' %>
-
<% html_title(t(:label_my_account), t('two_factor_authentication.devices.add_new')) -%>
<% breadcrumb_paths link_to(t('two_factor_authentication.label_two_factor_authentication'), index_path),
t('two_factor_authentication.devices.add_new') -%>
diff --git a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/totp/_form.html.erb b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/totp/_form.html.erb
index 7b9c7ec5e55..3aaaab27318 100644
--- a/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/totp/_form.html.erb
+++ b/modules/two_factor_authentication/app/views/two_factor_authentication/two_factor_devices/totp/_form.html.erb
@@ -13,8 +13,14 @@
<%= f.hidden_field :otp_secret, value: @device.otp_secret %>
</fieldset>
-<div class="mobile-otp-qr-form">
- <div class="qr-code-element" data-value="<%= device.provisioning_url %>">
+<div class="mobile-otp-qr-form"
+ data-controller="two-factor-authentication"
+ data-application-target="dynamic"
+>
+ <div class="qr-code-element"
+ data-value="<%= device.provisioning_url %>"
+ data-two-factor-authentication-target="qrCodeElement"
+ >
</div>
<div class="mobile-otp-qr-manually">
<p>
diff --git a/modules/two_factor_authentication/app/views/users/_two_factor_authentication.html.erb b/modules/two_factor_authentication/app/views/users/_two_factor_authentication.html.erb
index 16846e9937d..cb252bd2dc4 100644
--- a/modules/two_factor_authentication/app/views/users/_two_factor_authentication.html.erb
+++ b/modules/two_factor_authentication/app/views/users/_two_factor_authentication.html.erb
@@ -1,5 +1,3 @@
-<%= render partial: '/two_factor_authentication/header_tags' %>
-
<%=
if current_user.id == @user.id
render partial: 'two_factor_authentication_self'