diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/application_controller.rb | 13 | ||||
| -rw-r--r-- | app/controllers/otp_challenges_controller.rb | 54 | ||||
| -rw-r--r-- | app/controllers/sessions_controller.rb | 20 | ||||
| -rw-r--r-- | app/views/otp_challenges/new.html.erb | 13 |
4 files changed, 97 insertions, 3 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d8de9750..6d46d522 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb | |||
| @@ -4,6 +4,7 @@ class ApplicationController < ActionController::Base | |||
| 4 | protect_from_forgery | 4 | protect_from_forgery |
| 5 | 5 | ||
| 6 | before_action :set_locale | 6 | before_action :set_locale |
| 7 | before_action :enforce_otp_enrollment | ||
| 7 | 8 | ||
| 8 | helper_method :safe_return_to | 9 | helper_method :safe_return_to |
| 9 | 10 | ||
| @@ -30,4 +31,16 @@ class ApplicationController < ActionController::Base | |||
| 30 | rescue URI::InvalidURIError | 31 | rescue URI::InvalidURIError |
| 31 | default | 32 | default |
| 32 | end | 33 | end |
| 34 | |||
| 35 | # The hard gate for the slow transition: a user flagged otp_required | ||
| 36 | # who has not enrolled can reach only enrollment, their own user page, | ||
| 37 | # the login machinery, and the challenge -- everything else funnels | ||
| 38 | # into setup. Anonymous visitors are untouched (not logged_in?). | ||
| 39 | def enforce_otp_enrollment | ||
| 40 | return unless logged_in? | ||
| 41 | return unless current_user.otp_required? && !current_user.otp_enrolled? | ||
| 42 | return if %w[otp_enrollments otp_challenges sessions users].include?(controller_name) | ||
| 43 | flash[:error] = "Your account requires a second factor -- set it up to continue." | ||
| 44 | redirect_to edit_user_path(current_user) | ||
| 45 | end | ||
| 33 | end | 46 | end |
diff --git a/app/controllers/otp_challenges_controller.rb b/app/controllers/otp_challenges_controller.rb new file mode 100644 index 00000000..892503a8 --- /dev/null +++ b/app/controllers/otp_challenges_controller.rb | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | # The second half of a two-step login. A pending marker (set by | ||
| 2 | # sessions#create after a correct password) plus deadline and attempt | ||
| 3 | # counter live in the session; the real user_id is only written after a | ||
| 4 | # valid code, through a fresh session. | ||
| 5 | class OtpChallengesController < ApplicationController | ||
| 6 | |||
| 7 | layout 'admin' | ||
| 8 | |||
| 9 | MAX_ATTEMPTS = 5 | ||
| 10 | |||
| 11 | def new | ||
| 12 | redirect_to login_path unless pending_user | ||
| 13 | end | ||
| 14 | |||
| 15 | def create | ||
| 16 | user = pending_user | ||
| 17 | return redirect_to login_path unless user | ||
| 18 | |||
| 19 | session[:otp_attempts] = session[:otp_attempts].to_i + 1 | ||
| 20 | if session[:otp_attempts] > MAX_ATTEMPTS | ||
| 21 | clear_pending | ||
| 22 | flash[:error] = "Too many attempts -- log in again." | ||
| 23 | return redirect_to login_path | ||
| 24 | end | ||
| 25 | |||
| 26 | if user.verify_otp!(params[:code]) | ||
| 27 | return_to = session[:return_to] | ||
| 28 | reset_session | ||
| 29 | self.current_user = user | ||
| 30 | flash[:notice] = "Logged in successfully" | ||
| 31 | redirect_to safe_return_to(return_to, :default => admin_path) | ||
| 32 | else | ||
| 33 | flash.now[:error] = "That code did not match." | ||
| 34 | render :new | ||
| 35 | end | ||
| 36 | end | ||
| 37 | |||
| 38 | private | ||
| 39 | |||
| 40 | def pending_user | ||
| 41 | return nil if session[:pending_otp_user_id].blank? | ||
| 42 | if session[:otp_deadline].to_i < Time.now.to_i | ||
| 43 | clear_pending | ||
| 44 | return nil | ||
| 45 | end | ||
| 46 | @pending_user ||= User.find_by(:id => session[:pending_otp_user_id]) | ||
| 47 | end | ||
| 48 | |||
| 49 | def clear_pending | ||
| 50 | session.delete(:pending_otp_user_id) | ||
| 51 | session.delete(:otp_deadline) | ||
| 52 | session.delete(:otp_attempts) | ||
| 53 | end | ||
| 54 | end | ||
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 64bf951a..f0d5cf9b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb | |||
| @@ -20,9 +20,23 @@ class SessionsController < ApplicationController | |||
| 20 | # button. Uncomment if you understand the tradeoffs. | 20 | # button. Uncomment if you understand the tradeoffs. |
| 21 | reset_session | 21 | reset_session |
| 22 | 22 | ||
| 23 | self.current_user = user | 23 | if user.otp_enrolled? |
| 24 | redirect_to safe_return_to(return_to, :default => admin_path) | 24 | # Half-completed login: no user_id yet, only the pending marker. |
| 25 | flash[:notice] = "Logged in successfully" | 25 | session[:pending_otp_user_id] = user.id |
| 26 | session[:otp_deadline] = 2.minutes.from_now.to_i | ||
| 27 | session[:otp_attempts] = 0 | ||
| 28 | session[:return_to] = return_to | ||
| 29 | redirect_to new_otp_challenge_path | ||
| 30 | else | ||
| 31 | self.current_user = user | ||
| 32 | if user.otp_required? | ||
| 33 | flash[:error] = "Your account requires a second factor -- set it up now." | ||
| 34 | redirect_to edit_user_path(user) | ||
| 35 | else | ||
| 36 | flash[:notice] = "Logged in successfully" | ||
| 37 | redirect_to safe_return_to(return_to, :default => admin_path) | ||
| 38 | end | ||
| 39 | end | ||
| 26 | else | 40 | else |
| 27 | note_failed_signin | 41 | note_failed_signin |
| 28 | @login = params[:login] | 42 | @login = params[:login] |
diff --git a/app/views/otp_challenges/new.html.erb b/app/views/otp_challenges/new.html.erb new file mode 100644 index 00000000..a9c7a15c --- /dev/null +++ b/app/views/otp_challenges/new.html.erb | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | <h1>Second factor</h1> | ||
| 2 | |||
| 3 | <div id="page_editor"> | ||
| 4 | <div class="node_description">Code</div> | ||
| 5 | <div class="node_content"> | ||
| 6 | <%= form_tag otp_challenge_path, :method => :post do %> | ||
| 7 | <%= text_field_tag :code, nil, :autofocus => true, | ||
| 8 | :autocomplete => "one-time-code", :inputmode => "numeric" %> | ||
| 9 | <%= submit_tag "Log in" %> | ||
| 10 | <% end %> | ||
| 11 | <span class="field_hint">Enter the six-digit code from your authenticator app.</span> | ||
| 12 | </div> | ||
| 13 | </div> | ||
