<commit_before>require 'omniauth-oauth2'
require 'multi_json'

module OmniAuth
  module Strategies

    # An omniauth 1.0 strategy for yahoo authentication
    class Yconnect < OmniAuth::Strategies::OAuth2

      option :name, 'yconnect'

      option :client_options, {
        :access_token_url   => '/yconnect/v1/token',
        :authorize_url      => '/yconnect/v1/authorization',
        :token_url          => '/yconnect/v1/token',
        :user_info_url      => 'https://userinfo.yahooapis.jp/yconnect/v1/attribute',
        :site               => 'https://auth.login.yahoo.co.jp' ,
      }

      uid {
        #access_token.params['xoauth_yahoo_guid']
        raw_info['id']

      }

      info do
        primary_email = nil
        if user_info['emails']
          email_info    = user_info['emails'].find{|e| e['primary']} || user_info['emails'].first
          primary_email = email_info['handle']
        end
        {
          :nickname    => user_info['nickname'],
          :name        => user_info['givenName'] || user_info['nickname'],
          :image       => user_info['image']['imageUrl'],
          :description => user_info['message'],
          :email       => primary_email,
          :urls        => {
            'Profile' => user_info['profileUrl'],
          }
        }
      end

      extra do
        hash = {}
        hash[:raw_info] = raw_info unless skip_info?
        hash
      end

      def build_access_token
        options.token_params = {} if options.token_params.nil?
        options.auth_token_params = {} if options.auth_token_params.nil?
        params = {}
        params[:body] = {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true))
        params[:body].merge!({'grant_type' => 'authorization_code', 'code' => request.params['code']})
        params[:headers] = {'Expect'=> '' ,
                            'Authorization' => 'Basic ' + Base64::strict_encode64("#{options.client_id}:#{options.client_secret}").strip}
        params.delete "client_id"
        params.delete "client_secret"
        params.merge(token_params.to_hash(:symbolize_keys => true))
        get_token( params , deep_symbolize(options.auth_token_params))
      end

      def get_token(params, access_token_opts={})
        opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
        response = client.request(:post , client.token_url, params.merge(opts))
        raise Error.new(response) if client.options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
        ::OAuth2::AccessToken.from_hash(client.auth_code , response.parsed.merge(access_token_opts))
      end

      # Refreshes the current Access Token
      #
      # @return [AccessToken] a new AccessToken
      # @note options should be carried over to the new AccessToken
      def refresh!(params={})
        raise "A refresh_token is not available" unless refresh_token
        params.merge!(
                      :grant_type     => 'refresh_token',
                      :refresh_token  => refresh_token)
        params[:headers] = {'Expect'=> '' ,
                            'Authorization' => 'Basic ' + Base64::strict_encode64("#{options.client_id}:#{options.client_secret}").strip}
        new_token = get_token(params)
        new_token.options = client.options
        new_token
      end

      # Return info gathered from the v1/user/:id/profile API call

      def raw_info
        pry
        # This is a public API and does not need signing or authentication
        request = "https://userinfo.yahooapis.jp/yconnect/v1/attribute/"
        @request = request
        pry
        @raw_info ||= MultiJson.decode(access_token.get(request).body)
      rescue ::Errno::ETIMEDOUT
        raise ::Timeout::Error
      end
<commit_msg>client wrong<commit_after>require 'omniauth-oauth2'
require 'multi_json'

module OmniAuth
  module Strategies

    # An omniauth 1.0 strategy for yahoo authentication
    class Yconnect < OmniAuth::Strategies::OAuth2

      option :name, 'yconnect'

      option :client_options, {
        :access_token_url   => '/yconnect/v1/token',
        :authorize_url      => '/yconnect/v1/authorization',
        :token_url          => '/yconnect/v1/token',
        :user_info_url      => 'https://userinfo.yahooapis.jp/yconnect/v1/attribute',
        :site               => 'https://auth.login.yahoo.co.jp' ,
      }

      uid {
        #access_token.params['xoauth_yahoo_guid']
        raw_info['id']

      }

      info do
        primary_email = nil
        if user_info['emails']
          email_info    = user_info['emails'].find{|e| e['primary']} || user_info['emails'].first
          primary_email = email_info['handle']
        end
        {
          :nickname    => user_info['nickname'],
          :name        => user_info['givenName'] || user_info['nickname'],
          :image       => user_info['image']['imageUrl'],
          :description => user_info['message'],
          :email       => primary_email,
          :urls        => {
            'Profile' => user_info['profileUrl'],
          }
        }
      end

      extra do
        hash = {}
        hash[:raw_info] = raw_info unless skip_info?
        hash
      end

      def build_access_token
        options.token_params = {} if options.token_params.nil?
        options.auth_token_params = {} if options.auth_token_params.nil?
        params = {}
        params[:body] = {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true))
        params[:body].merge!({'grant_type' => 'authorization_code', 'code' => request.params['code']})
        params[:headers] = {'Expect'=> '' ,
                            'Authorization' => 'Basic ' + Base64::strict_encode64("#{options.client_id}:#{options.client_secret}").strip}
        params.delete "client_id"
        params.delete "client_secret"
        params.merge(token_params.to_hash(:symbolize_keys => true))
        get_token( params , deep_symbolize(options.auth_token_params))
      end

      def get_token(params, access_token_opts={})
        opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
        response = client.request(:post , client.token_url, params.merge(opts))
        raise Error.new(response) if client.options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
        ::OAuth2::AccessToken.from_hash(client , response.parsed.merge(access_token_opts))
      end

      # Refreshes the current Access Token
      #
      # @return [AccessToken] a new AccessToken
      # @note options should be carried over to the new AccessToken
      def refresh!(params={})
        raise "A refresh_token is not available" unless refresh_token
        params.merge!(
                      :grant_type     => 'refresh_token',
                      :refresh_token  => refresh_token)
        params[:headers] = {'Expect'=> '' ,
                            'Authorization' => 'Basic ' + Base64::strict_encode64("#{options.client_id}:#{options.client_secret}").strip}
        new_token = get_token(params)
        new_token.options = client.options
        new_token
      end

      # Return info gathered from the v1/user/:id/profile API call

      def raw_info
        pry
        # This is a public API and does not need signing or authentication
        request = "https://userinfo.yahooapis.jp/yconnect/v1/attribute/"
        @request = request
        pry
        @raw_info ||= MultiJson.decode(access_token.get(request).body)
      rescue ::Errno::ETIMEDOUT
        raise ::Timeout::Error
      end

{'fraction_non_alphanumeric': 0.12736773350751143, 'fraction_numerical': 0.00496407576747224, 'mean_word_length': 2.3771504190560213, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 6, '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': '1866146', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2201, 'n_words': 415}