-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Two-factor authentication feature in UI part. #1729
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
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9363eb9
Add necessary gems dependency for otp and qr-code.
ecnelises f0f1022
Added 2fa settings page.
ecnelises 32c9c8c
Move two factor auth stuff to a single controller.
ecnelises 38ab940
Fill missing locale items.
ecnelises 40efa62
Add otp prompt page when enabled 2fa and do login.
ecnelises 184d49d
Show recovery codes after user successfully enabled 2fa.
ecnelises 0207ff6
Add feature flag on 2FA, move otp login action method.
ecnelises b1e50dc
Add unit tests regarding 2fa feature in UI part.
ecnelises 319dc0c
Improve 2fa related i18n entry names.
ecnelises 2dc1379
Change 2fa or tfa in code to mfa/multifactor_auths.
ecnelises 9b6d0e1
Add drift and prior attempt record for otp verification.
ecnelises 1f30246
Store user name instead of id in 2fa login session.
ecnelises 2bf7bcf
Change 2fa prompting words from 'two factor' to 'multifactor'.
ecnelises 7cb8996
Tweak on mfa settings page UI.
ecnelises 68491f0
Move otp code verification into a single method.
ecnelises 82bae0c
Add tests regarding UI 2fa.
ecnelises 5e26d87
Tweaks on otp verification, test and migration.
ecnelises a3d7883
Change style and text of mfa-enabling entry.
ecnelises 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
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
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
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 @@ | ||
| class MultifactorAuthsController < ApplicationController | ||
| before_action :check_feature_flag | ||
| before_action :redirect_to_root, unless: :signed_in? | ||
| before_action :require_mfa_disabled, only: %i[new create] | ||
| before_action :require_mfa_enabled, only: :destroy | ||
| helper_method :issuer | ||
|
|
||
| def new | ||
| @seed = ROTP::Base32.random_base32 | ||
| session[:mfa_seed] = @seed | ||
| text = ROTP::TOTP.new(@seed, issuer: issuer).provisioning_uri(current_user.email) | ||
| @qrcode_svg = RQRCode::QRCode.new(text, level: :l).as_svg | ||
| end | ||
|
|
||
| def create | ||
| seed = session[:mfa_seed] | ||
| session.delete(:mfa_seed) | ||
| totp = ROTP::TOTP.new(seed, issuer: issuer) | ||
| if totp.verify(params[:otp]) | ||
| current_user.enable_mfa!(seed, :mfa_login_only) | ||
| current_user.update!(last_otp_at: Time.current) | ||
| flash[:success] = t('.success') | ||
| render :recovery | ||
| else | ||
| flash[:error] = t('multifactor_auths.incorrect_otp') | ||
| redirect_to edit_profile_url | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| if current_user.otp_verified?(params[:otp]) | ||
| flash[:success] = t('.success') | ||
| current_user.disable_mfa! | ||
| else | ||
| flash[:error] = t('multifactor_auths.incorrect_otp') | ||
| end | ||
| redirect_to edit_profile_url | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def issuer | ||
| request.host || 'rubygems.org' | ||
| end | ||
|
|
||
| def require_mfa_disabled | ||
| return unless current_user.mfa_enabled? | ||
| flash[:error] = t('multifactor_auths.require_mfa_disabled') | ||
| redirect_to edit_profile_path | ||
| end | ||
|
|
||
| def require_mfa_enabled | ||
| return if current_user.mfa_enabled? | ||
| flash[:error] = t('multifactor_auths.require_mfa_enabled') | ||
| redirect_to edit_profile_path | ||
| end | ||
|
|
||
| def check_feature_flag | ||
| redirect_to edit_profile_path unless mfa_enabled? | ||
| 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
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,24 @@ | ||
| <% @title = t('.title') %> | ||
|
|
||
| <div class="t-body l-overflow"> | ||
| <div class="l-half--l"> | ||
| <%= raw @qrcode_svg %> | ||
| </div> | ||
| <div> | ||
| <p><%= t('.scan_prompt') %></p> | ||
| <p> | ||
| <span id="mfa-account"><%= t('.account', :account => "#{issuer}:#{current_user.email}") %></span><br> | ||
| <span id="mfa-key"><%= t('.key', :key => @seed.chars.each_slice(4).map(&:join).join(' ')) %></span><br> | ||
| <span id="mfa-type"><%= t('.time_based') %></span> | ||
| </p> | ||
| </div> | ||
| </div> | ||
|
|
||
| <%= form_tag multifactor_auth_path, :method => :post do %> | ||
| <div class="text_field"> | ||
| <%= label_tag :otp, 'OTP code', :class => 'form__label' %> | ||
| <p class='form__field__instructions'><%= t '.otp_prompt' %></p> | ||
| <%= text_field_tag :otp, '', :class => 'form__input' %> | ||
| </div> | ||
| <%= submit_tag t('.enable'), :class => 'form__submit' %> | ||
| <% 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <% @title = t('.title') %> | ||
|
|
||
| <div class="t-body"> | ||
| <ul id="recovery-code-list"> | ||
| <% current_user.mfa_recovery_codes.each do |code| %> | ||
| <li><%= code %></li> | ||
| <% end %> | ||
| </ul> | ||
| <p><%= t '.note' %></p> | ||
| <%= link_to t('.continue'), edit_profile_path %> | ||
| </div> |
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,11 @@ | ||
| <% @title = t('multifactor_authentication') %> | ||
|
|
||
| <%= form_tag mfa_create_session_path, :method => :post do %> | ||
| <div class="text_field"> | ||
| <%= label_tag :otp, t('multifactor_auths.otp_code'), :class => 'form__label' %> | ||
| <%= text_field_tag :otp, '', :class => 'form__input', :autofocus => true %> | ||
| </div> | ||
| <div class="form_bottom"> | ||
| <%= submit_tag t('sign_in'), :data => {:disable_with => t('form_disable_with')}, :class => 'form__submit' %> | ||
| </div> | ||
| <% 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This if else logic could be simpler if
'sessions/otp_prompt'made request to a new method likemfa_create.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah. and in
mfa_createchecksession[:mfa_user]seems can achieve the same functionality