<commit_before>module Pillowfort
  module Concerns
    module Controllers

      # This module is designed to be included in whichever controller acts
      # as the base class for your project. In most Rails projects, this will
      # be the ApplicationController.
      #
      module Base
        extend ActiveSupport::Concern

        #--------------------------------------------------
        # Configuration
        #--------------------------------------------------

        included do

          # callbacks
          before_filter :remove_response_headers!
          before_filter :authenticate_from_resource_token!

          # mixins
          include ActionController::HttpAuthentication::Basic::ControllerMethods

          # errors
          rescue_from Pillowfort::NotActivatedError,      with: :render_pillowfort_activation_error
          rescue_from Pillowfort::NotAuthenticatedError,  with: :render_pillowfort_authentication_error
          rescue_from Pillowfort::TokenStateError,        with: :render_pillowfort_token_state_error

          # helpers
          helper_method :pillowfort_realm
          helper_method :pillowfort_resource
          helper_method :pillowfort_session_token

        end


        #--------------------------------------------------
        # Private Methods
        #--------------------------------------------------
        private

        #========== AUTHENTICATION ========================

        # This method reads the email, token, and realm from
        # the request headers, determines the authenticable class,
        # and defers lookup to the authenticable class itself.
        #
        # If you wish to support multiple sessions per client
        # application, your client can provide a identifying value
        # in a custom http header named `X-Realm`.  If no such
        # header is provided, Pillowfort will use the default Rails
        # realm of `Application`.
        #
        def authenticate_from_resource_token!
          klass = Pillowfort.config.resource_class.to_s.classify.constantize

          authenticate_with_http_basic do |email, token|
            klass.authenticate_securely(email, token, pillowfort_realm) do |resource|
              @pillowfort_resource = resource
            end
          end
        end


        #========== CURRENT ===============================

        # This method returns the specified realm for this
        # request.
        #
        def pillowfort_realm
          @pillowfort_realm ||= begin
            (headers['HTTP_X_Realm'] || 'Application').to_s.underscore.strip
          end
        end

        # This method returns the current instance of the
        # authenticable resource.
        #
        def pillowfort_resource
          @pillowfort_resource
        end

        # This method returns the current session token of the
        # authenticable resource for the current realm.
        #
        def pillowfort_session_token
          pillowfort_resource &&
            pillowfort_resource.session_tokens.where(realm: pillowfort_realm).first
        end


        #========== HEADERS =================================

        # This is necessary, as it allows Cordova to properly delegate<commit_msg>Small change to how realm is retrieved to actually, you know, retrieve it.<commit_after>module Pillowfort
  module Concerns
    module Controllers

      # This module is designed to be included in whichever controller acts
      # as the base class for your project. In most Rails projects, this will
      # be the ApplicationController.
      #
      module Base
        extend ActiveSupport::Concern

        #--------------------------------------------------
        # Configuration
        #--------------------------------------------------

        included do

          # callbacks
          before_filter :remove_response_headers!
          before_filter :authenticate_from_resource_token!

          # mixins
          include ActionController::HttpAuthentication::Basic::ControllerMethods

          # errors
          rescue_from Pillowfort::NotActivatedError,      with: :render_pillowfort_activation_error
          rescue_from Pillowfort::NotAuthenticatedError,  with: :render_pillowfort_authentication_error
          rescue_from Pillowfort::TokenStateError,        with: :render_pillowfort_token_state_error

          # helpers
          helper_method :pillowfort_realm
          helper_method :pillowfort_resource
          helper_method :pillowfort_session_token

        end


        #--------------------------------------------------
        # Private Methods
        #--------------------------------------------------
        private

        #========== AUTHENTICATION ========================

        # This method reads the email, token, and realm from
        # the request headers, determines the authenticable class,
        # and defers lookup to the authenticable class itself.
        #
        # If you wish to support multiple sessions per client
        # application, your client can provide a identifying value
        # in a custom http header named `X-Realm`.  If no such
        # header is provided, Pillowfort will use the default Rails
        # realm of `Application`.
        #
        def authenticate_from_resource_token!
          klass = Pillowfort.config.resource_class.to_s.classify.constantize

          authenticate_with_http_basic do |email, token|
            klass.authenticate_securely(email, token, pillowfort_realm) do |resource|
              @pillowfort_resource = resource
            end
          end
        end


        #========== CURRENT ===============================

        # This method returns the specified realm for this
        # request.
        #
        def pillowfort_realm
          @pillowfort_realm ||= begin
            (request.headers['HTTP_X_REALM'] || 'Application').to_s.underscore.strip
          end
        end

        # This method returns the current instance of the
        # authenticable resource.
        #
        def pillowfort_resource
          @pillowfort_resource
        end

        # This method returns the current session token of the
        # authenticable resource for the current realm.
        #
        def pillowfort_session_token
          pillowfort_resource &&
            pillowfort_resource.session_tokens.where(realm: pillowfort_realm).first
        end


        #========== HEADERS =================================

        # This is necessary, as it allows Cordova to properly delegate
{'fraction_non_alphanumeric': 0.13866504485327658, 'fraction_numerical': 0.0, 'mean_word_length': 1.8562744246634824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3302741', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1487, 'n_words': 447}