Sha256: 76185191a1e576ce678cc2fe9f232bcd9aa9252f2f7523d4b8c886fff5f3b876

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# https://github.com/googleapis/google-api-ruby-client/blob/main/docs/oauth-web.md

require 'googleauth'
require 'googleauth/web_user_authorizer'
require 'googleauth/stores/file_token_store'

module GoogleSpreadsheetFetcher
  module Authorizer
    module Oauth2
      class Authorizer
        include Interface

        def initialize(config: nil, callback_uri: nil)
          @config = config || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::Config.new
          @callback_uri = callback_uri || ::GoogleSpreadsheetFetcher::Authorizer::Oauth2::RackApplication::CALLBACK_PATH
          @web_user_authorizer = build_web_user_authorizer

          freeze
        end

        def fetch_credentials!(user_id: nil)
          web_user_authorizer.get_credentials(user_id || config.user_id)
        end

        def fetch_credentials
          fetch_credentials!
        rescue StandardError
          nil
        end

        def get_authorization_url(rack_request)
          web_user_authorizer.get_authorization_url(request: rack_request)
        end

        def handle_auth_callback(rack_request)
          web_user_authorizer.handle_auth_callback(config.user_id, rack_request)
        end

        private

        attr_reader :config, :callback_uri, :web_user_authorizer

        def build_web_user_authorizer
          ::Google::Auth::WebUserAuthorizer.new(
            config.client_id, ::GoogleSpreadsheetFetcher.config.scopes, config.token_store, callback_uri
          )
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
google_spreadsheet_fetcher-2.2.0 lib/google_spreadsheet_fetcher/authorizer/oauth2/authorizer.rb
google_spreadsheet_fetcher-2.1.0 lib/google_spreadsheet_fetcher/authorizer/oauth2/authorizer.rb
google_spreadsheet_fetcher-2.0.1 lib/google_spreadsheet_fetcher/authorizer/oauth2/authorizer.rb
google_spreadsheet_fetcher-2.0.0 lib/google_spreadsheet_fetcher/authorizer/oauth2/authorizer.rb