lib/omniauth/strategies/sage_one.rb in omniauth-sageone-0.3.0 vs lib/omniauth/strategies/sage_one.rb in omniauth-sageone-0.4.0

- old
+ new

@@ -1,25 +1,19 @@ +# frozen_string_literal: true + require 'omniauth-oauth2' module OmniAuth module Strategies class SageOne < OmniAuth::Strategies::OAuth2 - TOKEN_URLS = { - 'ca' => 'https://mysageone.ca.sageone.com/oauth2/token', - 'de' => 'https://oauth.eu.sageone.com/token', - 'es' => 'https://oauth.eu.sageone.com/token', - 'fr' => 'https://oauth.eu.sageone.com/token', - 'gb' => 'https://app.sageone.com/oauth2/token', - 'ie' => 'https://app.sageone.com/oauth2/token', - 'us' => 'https://mysageone.na.sageone.com/oauth2/token' - }.freeze - option :client_options, - authorize_url: 'https://www.sageone.com/oauth2/auth/central' + authorize_url: 'https://www.sageone.com/oauth2/auth/central', + token_url: 'https://oauth.accounting.sage.com/token' option :authorize_params, - response_type: 'code' + response_type: 'code', + filter: 'apiv3.1' uid do access_token['requested_by_id'] end @@ -33,40 +27,19 @@ { country: country } end - # SageOne has different token endpoints for each available country. The country is returned in - # the authorization callback. Configure the OAuth client to use that information by - # customizing the client options - def client - ::OAuth2::Client.new(options.client_id, options.client_secret, client_options) - end - - protected - # Override this method to remove the query string from the callback_url because SageOne # requires an exact match - def build_access_token - client.auth_code.get_token( - request.params['code'], - { - redirect_uri: callback_url.split('?').first - }.merge(token_params.to_hash(symbolize_keys: true)), - deep_symbolize(options.auth_token_params) - ) + def callback_url + super.split('?').first end + protected + def country request.env ? request[:country].try(:downcase) : options.client_options[:country] - end - - # Override client_options[:token_url] using the country from ether the request or the - # provided country option - def client_options - hash = options.client_options - hash.merge!(token_url: TOKEN_URLS[country]) if country - deep_symbolize(hash) end end end end