Sha256: 5f9ef3c5f2f53c354635a32cb76bf5f2d490cb0eed0ec98e9d3659664ead5169

Contents?: true

Size: 1.71 KB

Versions: 29

Compression:

Stored size: 1.71 KB

Contents

require File.dirname(__FILE__) + '/support_lite'
require 'warden' unless defined?(Warden)
load File.dirname(__FILE__) + '/markup_plugin/output_helpers.rb'
Dir[File.dirname(__FILE__) + '/warden_plugin/**/*.rb'].each {|file| load file }

module SinatraMore
  module WardenPlugin
    # This is the basic password strategy for authentication
    class PasswordStrategy < Warden::Strategies::Base
      cattr_accessor :user_class

      def valid?
        username || password
      end

      def authenticate!
        raise "Please either define a user class or set SinatraMore::WardenPlugin::PasswordStrategy.user_class" unless user_class
        u = user_class.authenticate(username, password)
        u.nil? ? fail!("Could not log in") : success!(u)
      end

      def username
        params['username'] || params['nickname'] || params['login'] || params['email']
      end

      def password
        params['password'] || params['pass']
      end
    end

    def self.registered(app)
      raise "WardenPlugin::Error - Install warden with 'sudo gem install warden' to use plugin!" unless Warden && Warden::Manager
      app.use Warden::Manager do |manager|
        manager.default_strategies :password
        manager.failure_app = app
      end
      app.helpers SinatraMore::OutputHelpers
      app.helpers SinatraMore::WardenHelpers

      Warden::Manager.before_failure { |env,opts| env['REQUEST_METHOD'] = "POST" }
      Warden::Manager.serialize_into_session { |user| user.nil? ? nil : user.id }
      Warden::Manager.serialize_from_session { |id| id.nil? ? nil : PasswordStrategy.user_class.find(id) }
      Warden::Strategies.add(:password, PasswordStrategy)
      PasswordStrategy.user_class = User if defined?(User)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
sinatra_more-0.3.4 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.3 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.2 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.1 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.0 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.2.9 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.2.8 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.2.7 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.2.5 lib/sinatra_more/warden_plugin.rb