summaryrefslogtreecommitdiff
path: root/app/controllers/elevations_controller.rb
blob: 229f33d8ab19f50c739d6fc4f69e159c72fac611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Step-up authentication for janitorial work. Mirrors the two-step login's
# attempt cap: verify_otp! guards replay but not rate, and an attacker with a
# stolen cookie would otherwise have unlimited tries at a six-digit code.
class ElevationsController < ApplicationController
  include RoleRequired

  layout 'admin'

  before_action :login_required
  before_action :require_admin

  MAX_ATTEMPTS = 5

  def new
    redirect_to admin_path if elevated?
  end

  def create
    return render(:new) unless current_user.otp_enrolled?

    session[:elevation_attempts] = session[:elevation_attempts].to_i + 1
    if session[:elevation_attempts] > MAX_ATTEMPTS
      logout_killing_session!
      flash[:error] = t("flash.otp.too_many_attempts")
      return redirect_to(login_path)
    end

    if current_user.verify_otp!(params[:code])
      session.delete(:elevation_attempts)
      elevate!
      redirect_to safe_return_to(session.delete(:elevation_return_to),
                                 :default => users_path)
    else
      flash.now[:error] = t("flash.otp.code_mismatch")
      render :new
    end
  end

  def destroy
    drop_elevation!
    flash[:notice] = t("flash.elevation.dropped")
    redirect_to admin_path
  end

  def renew
    if renew_elevation!
      flash[:notice] = t("flash.elevation.renewed",
                         :minutes => AuthenticatedSystem::ELEVATION_MAX_AGE.in_minutes.to_i,
                         :left => elevation_extensions_left)
    else
      flash[:error] = t("flash.elevation.cannot_renew")
    end
    redirect_back(:fallback_location => admin_path, :allow_other_host => false)
  end
end