Sha256: c9cf88f6a19976c13fc2d42a79197e9bf458235ee0de0649904632393fb00fbc

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require 'json'
require 'faraday'

module Fintecture
  class Authentication
    class << self

      def authorize(redirect_uri, state = nil)
        query_string = "?#{{
            response_type: 'code',
            app_id: Fintecture.app_id,
            redirect_uri: redirect_uri,
            state: state
        }.map{|key, value| "#{key}=#{value}"}.join('&')}"

        ::Faraday.get "#{token_authorize_endpoint}#{query_string}"
      end

      def get_access_token(auth_code =  nil)
        body = access_token_data auth_code

        Fintecture::Faraday::Authentication::Connection.post url: access_token_url, req_body: body
      end

      def refresh_token(refresh_token)
        body = refresh_token_data refresh_token

        Fintecture::Faraday::Authentication::Connection.post url: refresh_token_url, req_body: body
      end

      private

      def base_url
        Fintecture::Api::BaseUrl::FINTECTURE_OAUTH_URL[Fintecture.environment.to_sym]
      end

      def token_authorize_endpoint
        "#{base_url}#{Fintecture::Api::Endpoints::Authentication::OAUTH_TOKEN_AUTHORIZE}"
      end

      def access_token_url
        "#{base_url}#{Fintecture::Api::Endpoints::Authentication::OAUTH_ACCESS_TOKEN}"
      end

      def refresh_token_url
        "#{base_url}#{Fintecture::Api::Endpoints::Authentication::OAUTH_REFRESH_TOKEN}"
      end

      def access_token_data(auth_code)
        data =  {
            scope: 'PIS',
            app_id: Fintecture.app_id,
            grant_type: 'client_credentials'
        }

        if auth_code
          data = {
              scope: 'AIS',
              code: auth_code,
              grant_type: 'authorization_code'
          }
        end

        data
      end

      def refresh_token_data(refresh_token)
        {
            grant_type: 'refresh_token',
            refresh_token: refresh_token
        }
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fintecture-0.1.9 lib/fintecture/authentication.rb
fintecture-0.1.8 lib/fintecture/authentication.rb
fintecture-0.1.7 lib/fintecture/authentication.rb