Sha256: b50de0957e3a783b04e94851530ba64fcc0fead9ad9f2519a0b65bdf67d71b7a

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 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 Sinatra
  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 Sinatra::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.default_serializers :session, :cookie
        manager.failure_app = app
        manager.serializers.update(:session) do
          def serialize(user); user.nil? ? nil : user.id; end
          def deserialize(id); id.nil? ? nil : PasswordStrategy.user_class.find(id); end
        end
      end
      app.helpers Sinatra::MarkupPlugin::OutputHelpers
      app.helpers 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

  register WardenPlugin
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
darkhelmet-sinatra_more-0.3.36 lib/sinatra/warden_plugin.rb
darkhelmet-sinatra_more-0.3.35 lib/sinatra/warden_plugin.rb
darkhelmet-sinatra_more-0.3.34 lib/sinatra/warden_plugin.rb
darkhelmet-sinatra_more-0.3.33 lib/sinatra/warden_plugin.rb