<commit_before>module Sinatra
  module Bouncer
    COOKIE_KEY = "sinatra.bouncer.remember"

    use Rack::Session::Cookie
    use Rack::Flash
    use Rack::Cookies
    use Warden::Manager do |manager|
      manager.failure_app = lambda { |env|
        env['x-rack.flash'][:error] = "You need to be authenticated to access this page"
        [302, { 'Location' => '/login' }, ['']] 
      }
      manager.default_strategies :remember_me
      manager.default_serializers :session

      manager.serializers.update(:session) do
        def serialize(user); user.id; end
        def deserialize(id); User.get(id); end
      end

      manager.strategies.add(:password) do
        def valid?
          params['user'] && params['user']['login'] && params['user']['password']
        end

        def authenticate!
          user = User.authenticate(params['user']['login'], params['user']['password'])
          if user.nil?
            errors.add(:authenticate, "invalid login/password")
          elsif !user.confirmed
            errors.add(:authenticate, "email not confirmed")
          else
            if params['user']['remember_me']
              user.remember_me!
              env['rack.cookies'][COOKIE_KEY] = { :value => user.remember_token, :expires => Time.now + 7 * 24 * 3600, :path => '/' }
            end
            success!(user)
          end
        end
      end

      manager.strategies.add(:remember_me) do
        def valid?
          !!env['rack.cookies'][COOKIE_KEY]
        end

        def authenticate!
          token = env['rack.cookies'][COOKIE_KEY]
          return nil unless token
          user = User.first(:remember_token => token)
          if user.nil?
            env['rack.cookies'].delete(COOKIE_KEY)
          else
            user.remember_me!
            env['rack.cookies'][COOKIE_KEY] = { :value => user.remember_token, :expires => Time.now + 7 * 24 * 3600, :path => '/' }
            success!(user)
          end
        end
      end
    end

    Warden::Manager.before_logout do |user, proxy, opts|
      user.forget_me! if user
      proxy.env['rack.cookies'].delete(COOKIE_KEY)
    end

    module Helpers
      def authenticated?
        env['warden'].authenticated?
      end
      alias_method :logged_in?, :authenticated?

      def confirmation_link(user)
        "http://#{env['HTTP_HOST']}/confirm/#{user.confirm_token}"<commit_msg>get tests to pass after updating to warden 0.9<commit_after>module Sinatra
  module Bouncer
    COOKIE_KEY = "sinatra.bouncer.remember"

    use Rack::Session::Cookie
    use Rack::Flash
    use Rack::Cookies
    use Warden::Manager do |manager|
      manager.failure_app = lambda { |env|
        env['x-rack.flash'][:error] = "You need to be authenticated to access this page"
        [302, { 'Location' => '/login' }, ['']] 
      }
      manager.default_strategies :remember_me

      manager.strategies.add(:password) do
        def valid?
          params['user'] && params['user']['login'] && params['user']['password']
        end

        def authenticate!
          user = User.authenticate(params['user']['login'], params['user']['password'])
          if user.nil?
            errors.add(:authenticate, "invalid login/password")
          elsif !user.confirmed
            errors.add(:authenticate, "email not confirmed")
          else
            if params['user']['remember_me']
              user.remember_me!
              env['rack.cookies'][COOKIE_KEY] = { :value => user.remember_token, :expires => Time.now + 7 * 24 * 3600, :path => '/' }
            end
            success!(user)
          end
        end
      end

      manager.strategies.add(:remember_me) do
        def valid?
          !!env['rack.cookies'][COOKIE_KEY]
        end

        def authenticate!
          token = env['rack.cookies'][COOKIE_KEY]
          return nil unless token
          user = User.first(:remember_token => token)
          if user.nil?
            env['rack.cookies'].delete(COOKIE_KEY)
          else
            user.remember_me!
            env['rack.cookies'][COOKIE_KEY] = { :value => user.remember_token, :expires => Time.now + 7 * 24 * 3600, :path => '/' }
            success!(user)
          end
        end
      end
    end

    class Warden::SessionSerializer
      def serialize(user); user.id; end
      def deserialize(id); User.get(id); end
    end

    Warden::Manager.before_logout do |user, proxy, opts|
      user.forget_me! if user
      proxy.env['rack.cookies'].delete(COOKIE_KEY)
    end

    module Helpers
      def authenticated?
        env['warden'].authenticated?
      end
      alias_method :logged_in?, :authenticated?

      def confirmation_link(user)
        "http://#{env['HTTP_HOST']}/confirm/#{user.confirm_token}"
{'fraction_non_alphanumeric': 0.1312026913372582, 'fraction_numerical': 0.007569386038687973, 'mean_word_length': 2.181939799331104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5411332', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1403, 'n_words': 299}