Skip to content

Add a variant which sets up 2FA with devise #77

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

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

eoinkelly
Copy link
Contributor

@eoinkelly eoinkelly commented Apr 11, 2020

Current status

WIP. This branch can be used as inspiration for a devise MFA feature. The design doc included in this PR is particularly useful for understanding the feature.

Background

These changes were initially sketched in ackama/rails-template-demo-devise-2fa#11 and refined in some real client apps. Hat tip to @joshmcarthur who greatly improved my first pass at this. Fixes #61

TODO

  • Be consistent in whether we describe this as 2FA or MFA
  • Decide whether this variant should create a seed user for easy testing of sign-in and sign-out (decided against doing this)
  • Fix all TODO items in the code
  • Generate tests for all code added by this variant

@eoinkelly eoinkelly changed the base branch from master to main July 25, 2020 22:22
@eoinkelly
Copy link
Contributor Author

The basics are in this now but we need to add:

  • Screens for managing OTP for your user
  • A screen for backup codes (not sure how hard this is to do)

My goal here is that this should generate everything you need except CSS styling for the feature

@trevh-ack trevh-ack self-requested a review August 5, 2020 21:38
@trevh-ack trevh-ack self-assigned this Aug 5, 2020
@eoinkelly eoinkelly removed the request for review from trevh-ack May 28, 2021 02:43
@eoinkelly
Copy link
Contributor Author

I now think this is big enough to be a whole separate template so I'm going to close this.

@eoinkelly eoinkelly closed this May 28, 2021
@eoinkelly eoinkelly reopened this Mar 4, 2022
@eoinkelly
Copy link
Contributor Author

This work has to be paused until devise-two-factor supports Rails 7. I'm trying to help make that happen on devise-two-factor/devise-two-factor#206

@eoinkelly
Copy link
Contributor Author

eoinkelly commented Jul 9, 2022

See ackama/rails-template-demo-devise-2fa#1 for progress on this feature.

Edit: this is outdated, latest changes are in this PR now

@eoinkelly eoinkelly changed the title Add a variant which sets up 2FA (Two Factor Auth) with devise [DRAFT] Add a variant which sets up 2FA with devise [WIP] Jul 9, 2022
@eoinkelly eoinkelly added the WIP Work in progress label Oct 14, 2022
@eoinkelly eoinkelly changed the title Add a variant which sets up 2FA with devise [WIP] Add a variant which sets up 2FA with devise Oct 14, 2022
@robotdana
Copy link
Contributor

copy improvements from another project which doesn't need js and is instead an actual 2-step process with separate pages

@eoinkelly
Copy link
Contributor Author

I have copied the files from ackama/rails-template-demo-devise-2fa#11 which incorporates changes made on client projects. This PR is still very WIP.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 36 out of 36 changed files in this pull request and generated 2 comments.

private

def otp_param
params.require(:otp_attempt).gsub(/\A[^\d+]\z/, "")
Copy link
Preview

Copilot AI Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OTP parameter sanitization regex appears incorrect for typical OTP codes composed solely of digits. Consider replacing it with a method like .delete("^0-9") to reliably remove unwanted characters.

Suggested change
params.require(:otp_attempt).gsub(/\A[^\d+]\z/, "")
params.require(:otp_attempt).delete("^0-9")

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a real bug. My regex had anchors for start and end of string (\A and \z respectively) which prevents it from matching all non digit characters. A correct regex would be /[^\d+]/ but the suggestion from copilot is also correct and honestly a bit more readable.

# irb session to demonstrate
irb(main):001> x = "abc123%*"
=> "abc123%*"

# my broken regex
irb(main):002> x.gsub(/\A[^\d+]\z/, "")
=> "abc123%*"

# copilot suggestion
irb(main):003> x.delete("^0-9")
=> "123"

# my fixed regex
irb(main):004> x.gsub(/[^\d+]/, "")
=> "123"

@@ -45,6 +45,10 @@ def apply_variant_devise?
@yaml_config.fetch("apply_variant_devise")
end

def apply_variant_devise_mfa?
@yaml_config.fetch("apply_variant_devise")
Copy link
Preview

Copilot AI Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The apply_variant_devise_mfa? method is fetching the 'apply_variant_devise' key instead of 'apply_variant_devise_mfa', which may lead to incorrect behavior. Please update the key to ensure it reflects the intended variant.

Suggested change
@yaml_config.fetch("apply_variant_devise")
@yaml_config.fetch("apply_variant_devise_mfa")

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

@eoinkelly eoinkelly Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a real bug and the suggested fix does fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Add a 2FA (Two Factor Auth) with Devise variant
4 participants