Sha256: 9c1082795f204cd9aeda3dba9466db44c1102f3b83feaa30bcb1c00f93ad4558

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

module DeviseOtpAuthenticatable::Hooks
  module Sessions
    extend ActiveSupport::Concern
    include DeviseOtpAuthenticatable::Controllers::UrlHelpers

    included do
      alias_method_chain :create, :otp
    end

    #
    # replaces Devise::SessionsController#create
    #
    def create_with_otp

      resource = warden.authenticate!(auth_options)

      otp_refresh_credentials_for(resource)

      if otp_challenge_required_on?(resource)
        challenge = resource.generate_otp_challenge!
        warden.logout
        respond_with resource, :location => otp_credential_path_for(resource, {:challenge => challenge})

      elsif otp_mandatory_on?(resource) # if mandatory, log in user but send him to the must activate otp
        set_flash_message(:notice, :signed_in_but_otp) if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location => otp_token_path_for(resource)
      else

        set_flash_message(:notice, :signed_in) if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location => after_sign_in_path_for(resource)
      end
    end


    private

    #
    # resource should be challenged for otp
    #
    def otp_challenge_required_on?(resource)
      return false unless resource.respond_to?(:otp_enabled) && resource.respond_to?(:otp_auth_secret)
      resource.otp_enabled && !is_otp_trusted_device_for?(resource)
    end

    #
    # the resource -should- have otp turned on, but it isn't
    #
    def otp_mandatory_on?(resource)
      return true if resource.class.otp_mandatory
      return false unless resource.respond_to?(:otp_mandatory)

      resource.otp_mandatory && !resource.otp_enabled
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
devise-otp-0.2.3 lib/devise_otp_authenticatable/hooks/sessions.rb
devise-otp-0.2.2 lib/devise_otp_authenticatable/hooks/sessions.rb
devise-otp-0.2.0 lib/devise_otp_authenticatable/hooks/sessions.rb
devise-otp-0.1.1 lib/devise_otp_authenticatable/hooks/sessions.rb