Sha256: 18058ae73ca20f573cff4ae34ba47756a9d4267d06c16bc7fb53924836af6670

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

module DeviseMultiFactor
  module Controllers
    module Helpers
      extend ActiveSupport::Concern

      included do
        before_action :handle_two_factor_authentication
      end

      private

      def two_factor_authenticate!
        Devise.mappings.keys.flatten.any? do |scope|
          if signed_in?(scope) and warden.session(scope)[DeviseMultiFactor::NEED_AUTHENTICATION]
            handle_failed_second_factor(scope)
          end
        end
      end

      def handle_two_factor_authentication
        unless devise_controller?
          two_factor_authenticate!
        end
      end

      def handle_failed_second_factor(scope)
        if request.format.present?
          if request.format.html?
            session["#{scope}_return_to"] = request.original_fullpath if request.get?
            redirect_to two_factor_authentication_path_for(scope)
          elsif request.format.json?
            session["#{scope}_return_to"] = root_path(format: :html)
            render json: { redirect_to: two_factor_authentication_path_for(scope) }, status: :unauthorized
          end
        else
          head :unauthorized
        end
      end

      def two_factor_authentication_path_for(resource_or_scope = nil)
        scope = Devise::Mapping.find_scope!(resource_or_scope)
        change_path = "#{scope}_two_factor_authentication_path"
        send(change_path)
      end
    end
  end
end

module Devise
  module Controllers
    module Helpers
      def is_fully_authenticated?
        !session["warden.user.user.session"].try(:[], DeviseMultiFactor::NEED_AUTHENTICATION)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
devise-multi-factor-3.2.4 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.2.3 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.2.2 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.2.1 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.2.0 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.1.8 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.1.7 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.1.6 lib/devise_multi_factor/controllers/helpers.rb
devise-multi-factor-3.1.5 lib/devise_multi_factor/controllers/helpers.rb