-
Notifications
You must be signed in to change notification settings - Fork 166
LG-11454 (1): Add controllers for WebAuthn management #9742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
app/controllers/api/internal/two_factor_authentication/webauthn_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| module Api | ||
| module Internal | ||
| module TwoFactorAuthentication | ||
| class WebauthnController < ApplicationController | ||
| include CsrfTokenConcern | ||
| include ReauthenticationRequiredConcern | ||
|
|
||
| before_action :render_unauthorized, unless: :recently_authenticated_2fa? | ||
|
|
||
| after_action :add_csrf_token_header_to_response | ||
|
|
||
| respond_to :json | ||
|
|
||
| def update | ||
| result = ::TwoFactorAuthentication::WebauthnUpdateForm.new( | ||
| user: current_user, | ||
| configuration_id: params[:id], | ||
| ).submit(name: params[:name]) | ||
|
|
||
| analytics.webauthn_update_name_submitted(**result.to_h) | ||
|
|
||
| if result.success? | ||
| render json: { success: true } | ||
| else | ||
| render json: { success: false, error: result.first_error_message }, status: :bad_request | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| result = ::TwoFactorAuthentication::WebauthnDeleteForm.new( | ||
| user: current_user, | ||
| configuration_id: params[:id], | ||
| ).submit | ||
|
|
||
| analytics.webauthn_delete_submitted(**result.to_h) | ||
|
|
||
| if result.success? | ||
| render json: { success: true } | ||
| else | ||
| render json: { success: false, error: result.first_error_message }, status: :bad_request | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def render_unauthorized | ||
| render json: { error: 'Unauthorized' }, status: :unauthorized | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| module Users | ||
| class WebauthnController < ApplicationController | ||
| include ReauthenticationRequiredConcern | ||
|
|
||
| before_action :confirm_two_factor_authenticated | ||
| before_action :confirm_recently_authenticated_2fa | ||
| before_action :set_form | ||
| before_action :validate_configuration_exists | ||
|
|
||
| def edit; end | ||
|
|
||
| def update | ||
| result = form.submit(name: params.dig(:form, :name)) | ||
|
|
||
| analytics.webauthn_update_name_submitted(**result.to_h) | ||
|
|
||
| if result.success? | ||
| flash[:success] = t('two_factor_authentication.webauthn_platform.renamed') | ||
| redirect_to account_path | ||
| else | ||
| flash.now[:error] = result.first_error_message | ||
| render :edit | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| result = form.submit | ||
|
|
||
| analytics.webauthn_delete_submitted(**result.to_h) | ||
|
|
||
| if result.success? | ||
| flash[:success] = t('two_factor_authentication.webauthn_platform.deleted') | ||
| redirect_to account_path | ||
| else | ||
| flash[:error] = result.first_error_message | ||
| redirect_to edit_webauthn_path(id: params[:id]) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def form | ||
| @form ||= form_class.new(user: current_user, configuration_id: params[:id]) | ||
| end | ||
|
|
||
| alias_method :set_form, :form | ||
|
|
||
| def form_class | ||
| case action_name | ||
| when 'edit', 'update' | ||
| TwoFactorAuthentication::WebauthnUpdateForm | ||
| when 'destroy' | ||
| TwoFactorAuthentication::WebauthnDeleteForm | ||
| end | ||
| end | ||
|
|
||
| def validate_configuration_exists | ||
| render_not_found if form.configuration.blank? | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/forms/two_factor_authentication/webauthn_delete_form.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| module TwoFactorAuthentication | ||
| class WebauthnDeleteForm | ||
| include ActiveModel::Model | ||
| include ActionView::Helpers::TranslationHelper | ||
|
|
||
| attr_reader :user, :configuration_id | ||
|
|
||
| validate :validate_configuration_exists | ||
| validate :validate_has_multiple_mfa | ||
|
|
||
| def initialize(user:, configuration_id:) | ||
| @user = user | ||
| @configuration_id = configuration_id | ||
| end | ||
|
|
||
| def submit | ||
| success = valid? | ||
|
|
||
| configuration.destroy if success | ||
|
|
||
| FormResponse.new( | ||
| success:, | ||
| errors:, | ||
| extra: extra_analytics_attributes, | ||
| serialize_error_details_only: true, | ||
| ) | ||
| end | ||
|
|
||
| def configuration | ||
| @configuration ||= user.webauthn_configurations.find_by(id: configuration_id) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate_configuration_exists | ||
| return if configuration.present? | ||
| errors.add( | ||
| :configuration_id, | ||
| :configuration_not_found, | ||
| message: t('errors.manage_authenticator.internal_error'), | ||
| ) | ||
| end | ||
|
|
||
| def validate_has_multiple_mfa | ||
| return if !configuration || MfaPolicy.new(user).multiple_factors_enabled? | ||
| errors.add( | ||
| :configuration_id, | ||
| :only_method, | ||
| message: t('errors.manage_authenticator.remove_only_method_error'), | ||
| ) | ||
| end | ||
|
|
||
| def extra_analytics_attributes | ||
| { configuration_id: } | ||
| end | ||
| end | ||
| end |
68 changes: 68 additions & 0 deletions
68
app/forms/two_factor_authentication/webauthn_update_form.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| module TwoFactorAuthentication | ||
| class WebauthnUpdateForm | ||
| include ActiveModel::Model | ||
| include ActionView::Helpers::TranslationHelper | ||
|
|
||
| attr_reader :user, :configuration_id | ||
|
|
||
| validate :validate_configuration_exists | ||
| validate :validate_unique_name | ||
|
|
||
| def initialize(user:, configuration_id:) | ||
| @user = user | ||
| @configuration_id = configuration_id | ||
| end | ||
|
|
||
| def submit(name:) | ||
| @name = name | ||
|
|
||
| success = valid? | ||
| if valid? | ||
| configuration.name = name | ||
| success = configuration.valid? | ||
| errors.merge!(configuration.errors) | ||
| configuration.save if success | ||
| end | ||
|
|
||
| FormResponse.new( | ||
| success:, | ||
| errors:, | ||
| extra: extra_analytics_attributes, | ||
| serialize_error_details_only: true, | ||
| ) | ||
| end | ||
|
|
||
| def name | ||
| return @name if defined?(@name) | ||
| @name = configuration&.name | ||
| end | ||
|
|
||
| def configuration | ||
| @configuration ||= user.webauthn_configurations.find_by(id: configuration_id) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate_configuration_exists | ||
| return if configuration.present? | ||
| errors.add( | ||
| :configuration_id, | ||
| :configuration_not_found, | ||
| message: t('errors.manage_authenticator.internal_error'), | ||
| ) | ||
| end | ||
|
|
||
| def validate_unique_name | ||
| return unless user.webauthn_configurations.where.not(id: configuration_id).find_by(name:) | ||
| errors.add( | ||
| :name, | ||
| :duplicate, | ||
| message: t('errors.manage_authenticator.unique_name_error'), | ||
| ) | ||
| end | ||
|
|
||
| def extra_analytics_attributes | ||
| { configuration_id: } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <% self.title = t('two_factor_authentication.webauthn_platform.edit_heading') %> | ||
|
|
||
| <%= render PageHeadingComponent.new.with_content(t('two_factor_authentication.webauthn_platform.edit_heading')) %> | ||
|
|
||
| <%= simple_form_for( | ||
| @form, | ||
| as: :form, | ||
| method: :put, | ||
| html: { autocomplete: 'off' }, | ||
| url: webauthn_path(id: @form.configuration.id), | ||
| ) do |f| %> | ||
| <%= render ValidatedFieldComponent.new( | ||
| form: f, | ||
| name: :name, | ||
| label: t('two_factor_authentication.webauthn_platform.nickname'), | ||
| ) %> | ||
|
|
||
| <%= f.submit( | ||
| t('two_factor_authentication.webauthn_platform.change_nickname'), | ||
| class: 'display-block margin-top-5', | ||
| ) %> | ||
| <% end %> | ||
|
|
||
| <%= render ButtonComponent.new( | ||
| action: ->(**tag_options, &block) do | ||
| button_to( | ||
| webauthn_path(id: @form.configuration.id), | ||
| form: { aria: { label: t('two_factor_authentication.webauthn_platform.delete') } }, | ||
| **tag_options, | ||
| &block | ||
| ) | ||
| end, | ||
| method: :delete, | ||
| big: true, | ||
| wide: true, | ||
| danger: true, | ||
| class: 'display-block margin-top-2', | ||
| ).with_content(t('two_factor_authentication.webauthn_platform.delete')) %> | ||
|
|
||
| <%= render 'shared/cancel', link: account_path %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.