<commit_before>module ElderWand
  module Rails
    module Helpers
      extend ActiveSupport::Concern

      included do
        rescue_from ElderWand::Error, with: :elder_wand_render_elder_tree_error
        rescue_from ElderWand::Errors::ElderWandError, with: :elder_wand_render_elder_wand_error
      end

      # resource_owner_from_credentials do
      #   user = User.find_for_database_authentication(username: params[:username])
      #   if user && user.valid_password?(params[:password])
      #     user
      #   else
      #     fail ElderWand::Errors::InvalidPasswordError
      #   end
      # end

      # def current_resource_owner
      #   @current_resource_owner ||= User.find(elder_wand_token.resource_owner_id) if elder_wand_token
      # end

      # def authenticate_user!
      #   user = instance_eval(&Doorkeeper.configuration.resource_owner_from_credentials)
      #   create_elder_wand_token!(params[:code], user.id, ElderWand.configuration.scopes)
      # end

      def authorize_client_app!(*scopes)
        @elder_wand_scopes = scopes.presence || ElderWand.configuration.default_scopes
        if !valid_elder_tree_client?
          fail ElderWand::Errors::InvalidClientError
        end
      end

      def authorize_resource_owner!(*scopes)
        @elder_wand_scopes = scopes.presence || ElderWand.configuration.default_scopes
        if !valid_elder_tree_token?
          fail ElderWand::Errors::InvalidAccessTokenError.new(elder_wand_token)
        end
      end

      def create_elder_wand_token!(code, resource_owner_id, scopes)
        options = {
          resource_owner_id: resource_owner_id,
          scopes: scopes
        }
        @elder_wand_token = client.token_from_auth_code(code, options)
      end

      # @param [ElderWand::AccessToken]
      def elder_wand_token
        @elder_wand_token
      end

      private

      def elder_wand_client
        @elder_wand_client ||= ElderWand::Client.new(
          params[:client_id],
          params[:client_secret],
          site: Elderwand.configuration.provider_url
        )
      end

      def valid_elder_tree_token?
        @elder_wand_token = elder_wand_get_token_info
        @elder_wand_token && @elder_wand_token.acceptable?(@elder_wand_scopes)
      end

      def valid_elder_tree_client?
        @client_app = elder_wand_get_client_app_info
        @client_app && @client_app.includes_scope?(@elder_wand_scopes)
      end

      def elder_wand_get_token_info
        token = elder_wand_token_from_params || elder_wand_token_from_bearer_auth
        elder_wand_client.get_token_info(token)
      end

      def elder_wand_get_client_app_info
        elder_wand_client.get_client_info(params[:client_id], params[:client_secret])
      end

      def elder_wand_token_from_params<commit_msg>add notes to helper module<commit_after>module ElderWand
  module Rails
    module Helpers
      extend ActiveSupport::Concern

      included do
        rescue_from ElderWand::Error, with: :elder_wand_render_elder_tree_error
        rescue_from ElderWand::Errors::ElderWandError, with: :elder_wand_render_elder_wand_error
      end

      # resource_owner_from_credentials do
      #   user = User.find_for_database_authentication(username: params[:username])
      #   if user && user.valid_password?(params[:password])
      #     user
      #   else
      #     fail ElderWand::Errors::InvalidPasswordError
      #   end
      # end

      # def current_resource_owner
      #   @current_resource_owner ||= User.find(elder_wand_token.resource_owner_id) if elder_wand_token
      # end

      def elder_wand_authenticate_resource_owner!
        user = instance_eval(&ElderWand.configuration.resource_owner_from_credentials)
        create_elder_wand_token!(params[:code], user.id, ElderWand.configuration.scopes)
      end

      # TODO: maybe in the future we should have one method that takes care
      # of authorizing resource_owners and clients but the I feel the current
      # implementation is more explicit.
      # def elder_wand_authorize!(*scopes)
      #   @elder_wand_scopes = scopes.presence || ElderWand.configuration.default_scopes
      #   if valid_elder_tree_token?
      #     true
      #   elsif valid_elder_tree_client?
      #     true
      #   else
      #     raise_elder_wand_error
      #   end
      # end

      def elder_wand_authorize_client_app!(*scopes)
        @elder_wand_scopes = scopes.presence || ElderWand.configuration.default_scopes
        if !valid_elder_tree_client?
          fail ElderWand::Errors::InvalidClientError
        end
      end

      def elder_wand_authorize_access_token!(*scopes)
      end

      def elder_wand_authorize_resource_owner!(*scopes)
        @elder_wand_scopes = scopes.presence || ElderWand.configuration.default_scopes
        if !valid_elder_tree_token?
          fail ElderWand::Errors::InvalidAccessTokenError.new(elder_wand_token)
        end
      end

      def create_elder_wand_token!(code, resource_owner_id, scopes)
        options = {
          resource_owner_id: resource_owner_id,
          scopes: scopes
        }
        @elder_wand_token = client.token_from_auth_code(code, options)
      end

      # @param [ElderWand::AccessToken]
      def elder_wand_token
        @elder_wand_token
      end

      private

      def elder_wand_client
        @elder_wand_client ||= ElderWand::Client.new(
          params[:client_id],
          params[:client_secret],
          site: Elderwand.configuration.provider_url
        )
      end

      def valid_elder_tree_token?
        @elder_wand_token ||= elder_wand_get_token_info
        @elder_wand_token && @elder_wand_token.acceptable?(@elder_wand_scopes)
      end

      def valid_elder_tree_client?
        @client_app ||= elder_wand_get_client_app_info
        @client_app && @client_app.includes_scope?(@elder_wand_scopes)
      end

      def elder_wand_get_token_info
        token = elder_wand_token_from_params || elder_wand_token_from_bearer_auth
        elder_wand_client.get_token_info(token)
      end

      def elder_wand_get_client_app_info
        elder_wand_client.get_client_info(params[:client_id], params[:client_secret])
      end

      def elder_wand_token_from_params
{'fraction_non_alphanumeric': 0.069164265129683, 'fraction_numerical': 0.0, 'mean_word_length': 2.9487989886219976, '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': '6026448', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1985, 'n_words': 303}