You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

Add WebAuthn as an alternative 2FA method (#14466) * feat: add possibility of adding WebAuthn security keys to use as 2FA This adds a basic UI for enabling WebAuthn 2FA. We did a little refactor to the Settings page for editing the 2FA methods – now it will list the methods that are available to the user (TOTP and WebAuthn) and from there they'll be able to add or remove any of them. Also, it's worth mentioning that for enabling WebAuthn it's required to have TOTP enabled, so the first time that you go to the 2FA Settings page, you'll be asked to set it up. This work was inspired by the one donde by Github in their platform, and despite it could be approached in different ways, we decided to go with this one given that we feel that this gives a great UX. Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: add request for WebAuthn as second factor at login if enabled This commits adds the feature for using WebAuthn as a second factor for login when enabled. If users have WebAuthn enabled, now a page requesting for the use of a WebAuthn credential for log in will appear, although a link redirecting to the old page for logging in using a two-factor code will also be present. Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: add possibility of deleting WebAuthn Credentials Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: disable WebAuthn when an Admin disables 2FA for a user Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: remove ability to disable TOTP leaving only WebAuthn as 2FA Following examples form other platforms like Github, we decided to make Webauthn 2FA secondary to 2FA with TOTP, so that we removed the possibility of removing TOTP authentication only, leaving users with just WEbAuthn as 2FA. Instead, users will have to click on 'Disable 2FA' in order to remove second factor auth. The reason for WebAuthn being secondary to TOPT is that in that way, users will still be able to log in using their code from their phone's application if they don't have their security keys with them – or maybe even lost them. * We had to change a little the flow for setting up TOTP, given that now it's possible to setting up again if you already had TOTP, in order to let users modify their authenticator app – given that now it's not possible for them to disable TOTP and set it up again with another authenticator app. So, basically, now instead of storing the new `otp_secret` in the user, we store it in the session until the process of set up is finished. This was because, as it was before, when users clicked on 'Edit' in the new two-factor methods lists page, but then went back without finishing the flow, their `otp_secret` had been changed therefore invalidating their previous authenticator app, making them unable to log in again using TOTP. Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * refactor: fix eslint errors The PR build was failing given that linting returning some errors. This commit attempts to fix them. * refactor: normalize i18n translations The build was failing given that i18n translations files were not normalized. This commits fixes that. * refactor: avoid having the webauthn gem locked to a specific version * refactor: use symbols for routes without '/' * refactor: avoid sending webauthn disabled email when 2FA is disabled When an admins disable 2FA for users, we were sending two mails to them, one notifying that 2FA was disabled and the other to notify that WebAuthn was disabled. As the second one is redundant since the first email includes it, we can remove it and send just one email to users. * refactor: avoid creating new env variable for webauthn_origin config * refactor: improve flash error messages for webauthn pages Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com>
3 years ago
  1. # frozen_string_literal: true
  2. ENV['RAILS_ENV'] ||= 'test'
  3. require File.expand_path('../config/environment', __dir__)
  4. abort('The Rails environment is running in production mode!') if Rails.env.production?
  5. require 'spec_helper'
  6. require 'rspec/rails'
  7. require 'webmock/rspec'
  8. require 'paperclip/matchers'
  9. require 'capybara/rspec'
  10. require 'chewy/rspec'
  11. Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
  12. ActiveRecord::Migration.maintain_test_schema!
  13. WebMock.disable_net_connect!(allow: Chewy.settings[:host])
  14. Sidekiq::Testing.inline!
  15. Sidekiq.logger = nil
  16. Devise::Test::ControllerHelpers.module_eval do
  17. alias_method :original_sign_in, :sign_in
  18. def sign_in(resource, _deprecated = nil, scope: nil)
  19. original_sign_in(resource, scope: scope)
  20. SessionActivation.deactivate warden.cookies.signed['_session_id']
  21. warden.cookies.signed['_session_id'] = {
  22. value: resource.activate_session(warden.request),
  23. expires: 1.year.from_now,
  24. httponly: true,
  25. }
  26. end
  27. end
  28. RSpec.configure do |config|
  29. config.fixture_path = "#{Rails.root}/spec/fixtures"
  30. config.use_transactional_fixtures = true
  31. config.order = 'random'
  32. config.infer_spec_type_from_file_location!
  33. config.filter_rails_from_backtrace!
  34. config.include Devise::Test::ControllerHelpers, type: :controller
  35. config.include Devise::Test::ControllerHelpers, type: :view
  36. config.include Paperclip::Shoulda::Matchers
  37. config.include ActiveSupport::Testing::TimeHelpers
  38. config.include Chewy::Rspec::Helpers
  39. config.include Redisable
  40. config.before :each, type: :feature do
  41. https = ENV['LOCAL_HTTPS'] == 'true'
  42. Capybara.app_host = "http#{https ? 's' : ''}://#{ENV.fetch('LOCAL_DOMAIN')}"
  43. end
  44. config.before :each, type: :controller do
  45. stub_jsonld_contexts!
  46. end
  47. config.before :each, type: :service do
  48. stub_jsonld_contexts!
  49. end
  50. config.after :each do
  51. Rails.cache.clear
  52. redis.del(redis.keys)
  53. end
  54. end
  55. RSpec::Sidekiq.configure do |config|
  56. config.warn_when_jobs_not_processed_by_sidekiq = false
  57. end
  58. RSpec::Matchers.define_negated_matcher :not_change, :change
  59. def request_fixture(name)
  60. Rails.root.join('spec', 'fixtures', 'requests', name).read
  61. end
  62. def attachment_fixture(name)
  63. Rails.root.join('spec', 'fixtures', 'files', name).open
  64. end
  65. def stub_jsonld_contexts!
  66. stub_request(:get, 'https://www.w3.org/ns/activitystreams').to_return(request_fixture('json-ld.activitystreams.txt'))
  67. stub_request(:get, 'https://w3id.org/identity/v1').to_return(request_fixture('json-ld.identity.txt'))
  68. stub_request(:get, 'https://w3id.org/security/v1').to_return(request_fixture('json-ld.security.txt'))
  69. end