<commit_before>require 'rt_oauth_client'
require 'rt_oauth_client/bearer_token'
require 'rt_oauth_client/param_token'
require 'rt_oauth_client/authorizer'
module RtOauthClient
  module Protector
    extend ActiveSupport::Concern
    included do
      include RtOauthClient::Authorizer
      include RtOauthClient::ParamToken
      include RtOauthClient::BearerToken
    end
    
    helper_method :protected_user

    protected
    
    def protect!
      fetch_user_from_oauth2 # load user from request headers
      
      unless protected_user
        if respond_to?(:protect_failure)
          protect_failure
        else
          head 403
        end
      end
    end
    alias_method :protect_with_user!, :protect!
    
    def fetch_user_from_oauth2
      if protected_user.nil?
        RtOauthClient.configuration.authentication_methods.each do |m|
          token = send("find_#{m}")
          next unless token
          set_bearer_token(token)
          if me
            @protected_user = me
            break
          end
        end
      end
      protected_user
    end
    
    def protected_user
      @protected_user<commit_msg>return 401 unauthorized instead of 403<commit_after>require 'rt_oauth_client'
require 'rt_oauth_client/bearer_token'
require 'rt_oauth_client/param_token'
require 'rt_oauth_client/authorizer'
module RtOauthClient
  module Protector
    extend ActiveSupport::Concern
    included do
      include RtOauthClient::Authorizer
      include RtOauthClient::ParamToken
      include RtOauthClient::BearerToken
    end
    
    helper_method :protected_user

    protected
    
    def protect!
      fetch_user_from_oauth2 # load user from request headers
      
      unless protected_user
        if respond_to?(:protect_failure)
          protect_failure
        else
          render inline: 'you need sign in to continue', status: 401
        end
        return
      end
    end
    alias_method :protect_with_user!, :protect!
    
    def fetch_user_from_oauth2
      if protected_user.nil?
        RtOauthClient.configuration.authentication_methods.each do |m|
          token = send("find_#{m}")
          next unless token
          set_bearer_token(token)
          if me
            @protected_user = me
            break
          end
        end
      end
      protected_user
    end
    
    def protected_user
      @protected_user
{'fraction_non_alphanumeric': 0.04724080267558529, 'fraction_numerical': 0.006688963210702341, 'mean_word_length': 2.300689655172414, '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': '3691552', 'n_tokens_mistral': 708, 'n_tokens_neox': 684, 'n_words': 161}