<commit_before>require 'oauth2'

module Hieraviz
  class AuthGitlab

    def initialize(settings)
      if settings['proxy_uri']
        @@client ||= OAuth2::Client.new(
          settings['application_id'], 
          settings['secret'], 
          :site => settings['host'],
          connection_opts: {
            :proxy => {
              :uri => settings['proxy_uri']
            }
          }
        )
      else
        @@client ||= OAuth2::Client.new(
          settings['application_id'], 
          settings['secret'], 
          :site => settings['host']
          )
      end
      @settings = settings
    end

    def access_token(request, code)
      @@client.auth_code.get_token(code, :redirect_uri => redirect_uri(request.url))
    end

    def get_response(url, token)
      a_token = OAuth2::AccessToken.new(@@client, token)
      begin
        JSON.parse(a_token.get(url).body)
      rescue Exception => e
        { 'error' => JSON.parse(e.message.split(/\n/)[1])['message'] }
      end
    end

    def redirect_uri(url)
      uri = URI.parse(url)
      uri.path = '/logged-in'
      uri.query = nil
      uri.fragment = nil
      uri.to_s
    end

    def login_url(request)
      @@client.auth_code.authorize_url(:redirect_uri => redirect_uri(request.url))
    end

    def authorized?(token)<commit_msg>actually faraday takes proxy setting from env<commit_after>require 'oauth2'

module Hieraviz
  class AuthGitlab

    def initialize(settings)
      @@client ||= OAuth2::Client.new(
        settings['application_id'], 
        settings['secret'], 
        :site => settings['host']
        )
      @settings = settings
    end

    def access_token(request, code)
      @@client.auth_code.get_token(code, :redirect_uri => redirect_uri(request.url))
    end

    def get_response(url, token)
      a_token = OAuth2::AccessToken.new(@@client, token)
      begin
        JSON.parse(a_token.get(url).body)
      rescue Exception => e
        { 'error' => JSON.parse(e.message.split(/\n/)[1])['message'] }
      end
    end

    def redirect_uri(url)
      uri = URI.parse(url)
      uri.path = '/logged-in'
      uri.query = nil
      uri.fragment = nil
      uri.to_s
    end

    def login_url(request)
      @@client.auth_code.authorize_url(:redirect_uri => redirect_uri(request.url))
    end

    def authorized?(token)
{'fraction_non_alphanumeric': 0.12995313165743502, 'fraction_numerical': 0.0038346825734980826, 'mean_word_length': 2.265646731571627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '1332415', 'n_tokens_mistral': 748, 'n_tokens_neox': 721, 'n_words': 135}