Sha256: e3477c905c51ae8e00da0a33aad9729f0edf6e1470e54d17be87cc0d188a342d

Contents?: true

Size: 1.48 KB

Versions: 15

Compression:

Stored size: 1.48 KB

Contents

require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class KillbillAuthenticatable < Authenticatable
      # Invoked by warden to execute the strategy
      def authenticate!

        creds = params[:user] || {}
        kb_username = creds[:kb_username]
        kb_password = password
        # Find the associated user object
        resource = valid_password? && mapping.to.find_for_killbill_authentication(kb_username)
        return fail(:not_found_in_database) unless resource

        # Validate the credentials
        if validate(resource){ resource.valid_killbill_password?(kb_username, kb_password) }
          # Create the user if needed
          resource.after_killbill_authentication
          # Tell warden to halt the strategy and set the user in the appropriate scope
          success!(resource)
        end
      rescue Errno::ECONNREFUSED => _
        return fail(:killbill_not_available)
      end
    end
  end
end

Warden::Strategies.add(:killbill_authenticatable, Devise::Strategies::KillbillAuthenticatable)

Warden::Manager.after_set_user do |user, auth, opts|
  unless user.authenticated_with_killbill?
    scope = opts[:scope]
    auth.logout(scope)
    throw(:warden, :scope => scope, :reason => "Kill Bill session expired")
  end
end

Devise.add_module(:killbill_authenticatable,
                  :strategy => true,
                  :route => :session,
                  :controller => :sessions,
                  :model => 'kaui/killbill_authenticatable')

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
kaui-2.1.0 config/initializers/killbill_authenticatable.rb
kaui-2.0.4 config/initializers/killbill_authenticatable.rb
kaui-2.0.3 config/initializers/killbill_authenticatable.rb
kaui-2.0.2 config/initializers/killbill_authenticatable.rb
kaui-2.0.1 config/initializers/killbill_authenticatable.rb
kaui-2.0.0 config/initializers/killbill_authenticatable.rb
kaui-1.4.1 config/initializers/killbill_authenticatable.rb
kaui-1.4.0 config/initializers/killbill_authenticatable.rb
kaui-1.3.0 config/initializers/killbill_authenticatable.rb
kaui-1.2.0 config/initializers/killbill_authenticatable.rb
kaui-1.1.0 config/initializers/killbill_authenticatable.rb
kaui-1.0.0 config/initializers/killbill_authenticatable.rb
kaui-0.16.2 config/initializers/killbill_authenticatable.rb
kaui-0.16.1 config/initializers/killbill_authenticatable.rb
kaui-0.16.0 config/initializers/killbill_authenticatable.rb