<commit_before>require "omniauth-oauth2"

module OmniAuth
  module Strategies
    class DefenceRequest < OmniAuth::Strategies::OAuth2
      option :name, "defence_request"

      option :client_options, {
        site: ENV.fetch('AUTHENTICATION_SITE_URL', nil),
        redirect_url: ENV.fetch('AUTHENTICATION_REDIRECT_URI', nil)
      }

      uid { raw_info["profile"]["uid"] }

      def raw_info
        @_raw_info ||= MultiJson.decode access_token.get("/api/v1/profiles/me").body
      end
    end
  end
end<commit_msg>check the users roles in the oauth callback phase and raise :unauthorized<commit_after>require "omniauth-oauth2"

module OmniAuth
  module Strategies
    module CallbackOverride
      def callback_phase
        if raw_info["roles"].nil? || raw_info["roles"].empty?
          fail!(:unauthorized, OAuth2::CallbackError.new(:unauthorized, 'Unauthorized access'))
        else
          super
        end
      end
    end

    # Need to check roles after the access_token has been built by OmniAuth::OAuth2#callback_phase
    # but before the parent app is called by OmniAuth::Strategy#callback_phase
    # so we place a module in the ancestors chain between them
    OAuth2.send :include, CallbackOverride

    class DefenceRequest < OmniAuth::Strategies::OAuth2
      option :name, "defence_request"

      option :client_options, {
        site: ENV.fetch('AUTHENTICATION_SITE_URL', nil),
        redirect_url: ENV.fetch('AUTHENTICATION_REDIRECT_URI', nil)
      }

      uid { raw_info["profile"]["uid"] }

      def raw_info
        @_raw_info ||= MultiJson.decode access_token.get("/api/v1/profiles/me").body
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09555288461538461, 'fraction_numerical': 0.005408653846153846, 'mean_word_length': 3.1939546599496222, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2389911', 'n_tokens_mistral': 517, 'n_tokens_neox': 510, 'n_words': 123}