Sha256: e5149ddffe69e7597a3a1f154a69d0bc543a5c97548433ba80c59d7a23de18ec

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 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']
        @user ||= User[env['rack.session']['user_id']]
      end

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

      def authenticate
        authenticated?
      end

      def authenticated?
        !env['rack.session']['user_id'].nil?
      end

      def authenticate!
        raise NotAuthenticated unless env['rack.session']['user_id']
        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

8 entries across 8 versions & 1 rubygems

Version Path
proxes-0.3.6 lib/proxes/helpers/authentication.rb
proxes-0.3.5 lib/proxes/helpers/authentication.rb
proxes-0.3.3 lib/proxes/helpers/authentication.rb
proxes-0.3.2 lib/proxes/helpers/authentication.rb
proxes-0.3.1 lib/proxes/helpers/authentication.rb
proxes-0.3.0 lib/proxes/helpers/authentication.rb
proxes-0.2.0 lib/proxes/helpers/authentication.rb
proxes-0.1.0 lib/proxes/helpers/authentication.rb