Sha256: e056c406a8cb4873999b72a70de3628017cbcaa8a6e107da04f35e2301718ca6

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 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
        manager.serialize_into_session { |user| user.nil? ? nil : user.id }
        manager.serialize_from_session { |id| id.nil? ? nil : PasswordStrategy.user_class.find(id) }
      end
      app.helpers SinatraMore::OutputHelpers
      app.helpers SinatraMore::WardenHelpers
      Warden::Manager.before_failure { |env,opts| env['REQUEST_METHOD'] = "POST" }
      Warden::Strategies.add(:password, PasswordStrategy)
      PasswordStrategy.user_class = User if defined?(User)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sinatra_more-0.3.43 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.42 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.41 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.40 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.39 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.38 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.37 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.36 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.35 lib/sinatra_more/warden_plugin.rb
sinatra_more-0.3.34 lib/sinatra_more/warden_plugin.rb