Sha256: 1a486f7c53ff1bbb005dfe16ef1447c78a3a9f4973724aa0bcf169684e637e13

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module ProxES
  module Helpers
    module Authentication
      def current_user
        return nil unless env['rack.session'] && env['rack.session']['user_id']
        @users ||= Hash.new { |h, k| h[k] = User[k] }
        @users[env['rack.session']['user_id']]
      end

      def current_user=(user)
        env['rack.session']['user_id'] = user.id
      end

      def authenticate
        authenticated?
      end

      def authenticated?
        current_user.nil?
      end

      def authenticate!
        raise NotAuthenticated unless current_user
        true
      end

      def logout
        env['rack.session'].delete('user_id')
      end

      def check_basic
        auth = Rack::Auth::Basic::Request.new(env)
        return unless auth.provided?
        return unless auth.basic?

        identity = ::ProxES::Identity.find(username: auth.credentials[0])
        raise NotAuthenticated unless identity
        self.current_user = identity.user if identity.authenticate(auth.credentials[1])
      end
    end

    class NotAuthenticated < StandardError
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proxes-0.5.1 lib/proxes/helpers/authentication.rb
proxes-0.5.0 lib/proxes/helpers/authentication.rb