webauthn_credentials_controller.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. module Settings
  3. module TwoFactorAuthentication
  4. class WebauthnCredentialsController < BaseController
  5. skip_before_action :check_self_destruct!
  6. skip_before_action :require_functional!
  7. before_action :redirect_invalid_otp, unless: -> { current_user.otp_enabled? }
  8. before_action :redirect_invalid_webauthn, only: [:index, :destroy], unless: -> { current_user.webauthn_enabled? }
  9. def index; end
  10. def new; end
  11. def options
  12. current_user.update(webauthn_id: WebAuthn.generate_user_id) unless current_user.webauthn_id
  13. options_for_create = WebAuthn::Credential.options_for_create(
  14. user: {
  15. name: current_user.account.username,
  16. display_name: current_user.account.username,
  17. id: current_user.webauthn_id,
  18. },
  19. exclude: current_user.webauthn_credentials.pluck(:external_id),
  20. authenticator_selection: { user_verification: 'discouraged' }
  21. )
  22. session[:webauthn_challenge] = options_for_create.challenge
  23. render json: options_for_create, status: 200
  24. end
  25. def create
  26. webauthn_credential = WebAuthn::Credential.from_create(params[:credential])
  27. if webauthn_credential.verify(session[:webauthn_challenge])
  28. user_credential = current_user.webauthn_credentials.build(
  29. external_id: webauthn_credential.id,
  30. public_key: webauthn_credential.public_key,
  31. nickname: params[:nickname],
  32. sign_count: webauthn_credential.sign_count
  33. )
  34. if user_credential.save
  35. flash[:success] = I18n.t('webauthn_credentials.create.success')
  36. status = :ok
  37. if current_user.webauthn_credentials.size == 1
  38. UserMailer.webauthn_enabled(current_user).deliver_later!
  39. else
  40. UserMailer.webauthn_credential_added(current_user, user_credential).deliver_later!
  41. end
  42. else
  43. flash[:error] = I18n.t('webauthn_credentials.create.error')
  44. status = :unprocessable_entity
  45. end
  46. else
  47. flash[:error] = t('webauthn_credentials.create.error')
  48. status = :unauthorized
  49. end
  50. render json: { redirect_path: settings_two_factor_authentication_methods_path }, status: status
  51. end
  52. def destroy
  53. credential = current_user.webauthn_credentials.find_by(id: params[:id])
  54. if credential
  55. credential.destroy
  56. if credential.destroyed?
  57. flash[:success] = I18n.t('webauthn_credentials.destroy.success')
  58. if current_user.webauthn_credentials.empty?
  59. UserMailer.webauthn_disabled(current_user).deliver_later!
  60. else
  61. UserMailer.webauthn_credential_deleted(current_user, credential).deliver_later!
  62. end
  63. else
  64. flash[:error] = I18n.t('webauthn_credentials.destroy.error')
  65. end
  66. else
  67. flash[:error] = I18n.t('webauthn_credentials.destroy.error')
  68. end
  69. redirect_to settings_two_factor_authentication_methods_path
  70. end
  71. private
  72. def redirect_invalid_otp
  73. flash[:error] = t('webauthn_credentials.otp_required')
  74. redirect_to settings_two_factor_authentication_methods_path
  75. end
  76. def redirect_invalid_webauthn
  77. flash[:error] = t('webauthn_credentials.not_enabled')
  78. redirect_to settings_two_factor_authentication_methods_path
  79. end
  80. end
  81. end
  82. end