mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2025-11-08 08:54:23 -05:00
Started reworking Oauth token sorage
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#The MIT License (MIT)
|
#The MIT License (MIT)
|
||||||
#
|
#
|
||||||
#Copyright (c) 2022 rick barrette
|
#Copyright (c) 2023 rick barrette
|
||||||
#
|
#
|
||||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
@@ -26,7 +26,7 @@ class QboController < ApplicationController
|
|||||||
# Called when the user requests that Redmine to connect to QBO
|
# Called when the user requests that Redmine to connect to QBO
|
||||||
#
|
#
|
||||||
def authenticate
|
def authenticate
|
||||||
oauth2_client = Qbo.get_client
|
oauth2_client = Qbo.construct_oauth2_client
|
||||||
callback = Setting.host_name + "/qbo/oauth_callback/"
|
callback = Setting.host_name + "/qbo/oauth_callback/"
|
||||||
grant_url = oauth2_client.auth_code.authorize_url(redirect_uri: callback, response_type: "code", state: SecureRandom.hex(12), scope: "com.intuit.quickbooks.accounting")
|
grant_url = oauth2_client.auth_code.authorize_url(redirect_uri: callback, response_type: "code", state: SecureRandom.hex(12), scope: "com.intuit.quickbooks.accounting")
|
||||||
redirect_to grant_url
|
redirect_to grant_url
|
||||||
@@ -37,7 +37,7 @@ class QboController < ApplicationController
|
|||||||
#
|
#
|
||||||
def oauth_callback
|
def oauth_callback
|
||||||
if params[:state].present?
|
if params[:state].present?
|
||||||
oauth2_client = Qbo.get_client
|
oauth2_client = Qbo.construct_oauth2_client
|
||||||
# use the state value to retrieve from your backend any information you need to identify the customer in your system
|
# use the state value to retrieve from your backend any information you need to identify the customer in your system
|
||||||
redirect_uri = Setting.host_name + "/qbo/oauth_callback/"
|
redirect_uri = Setting.host_name + "/qbo/oauth_callback/"
|
||||||
if resp = oauth2_client.auth_code.get_token(params[:code], redirect_uri: redirect_uri)
|
if resp = oauth2_client.auth_code.get_token(params[:code], redirect_uri: redirect_uri)
|
||||||
@@ -47,12 +47,7 @@ class QboController < ApplicationController
|
|||||||
|
|
||||||
# Save the authentication information
|
# Save the authentication information
|
||||||
qbo = Qbo.new
|
qbo = Qbo.new
|
||||||
qbo.company_id = params[:realmId]
|
qbo.update(access_token: resp.token, refresh_token: resp.refresh_token, realm_id: params[:realmId])
|
||||||
|
|
||||||
# Generate Access Token & Serialize it into the database
|
|
||||||
access_token = OAuth2::AccessToken.new(oauth2_client, resp.token, refresh_token: resp.refresh_token)
|
|
||||||
qbo.token = access_token.to_hash
|
|
||||||
qbo.expire = 1.hour.from_now.utc
|
|
||||||
|
|
||||||
if qbo.save!
|
if qbo.save!
|
||||||
redirect_to qbo_sync_path, :flash => { :notice => "Successfully connected to Quickbooks" }
|
redirect_to qbo_sync_path, :flash => { :notice => "Successfully connected to Quickbooks" }
|
||||||
@@ -154,6 +149,6 @@ class QboController < ApplicationController
|
|||||||
ActiveRecord::Base.connection.close
|
ActiveRecord::Base.connection.close
|
||||||
end
|
end
|
||||||
|
|
||||||
redirect_to :home, :flash => { :notice => "Successfully synced to Quickbooks" }
|
redirect_to :home, :flash => { :notice => "Syncing Quickbooks" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
80
app/models/concerns/quickbooks_oauth.rb
Normal file
80
app/models/concerns/quickbooks_oauth.rb
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#The MIT License (MIT)
|
||||||
|
#
|
||||||
|
#Copyright (c) 2023 rick barrette
|
||||||
|
#
|
||||||
|
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
module QuickbooksOauth
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
OAUTH_CONSUMER_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey']
|
||||||
|
OAUTH_CONSUMER_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret']
|
||||||
|
|
||||||
|
#== Instance Methods
|
||||||
|
|
||||||
|
def perform_authenticated_request(&block)
|
||||||
|
attempts = 0
|
||||||
|
begin
|
||||||
|
yield oauth_access_token
|
||||||
|
rescue OAuth2::Error, Quickbooks::AuthorizationFailure => ex
|
||||||
|
Rails.logger.info("QuickbooksOauth.perform: #{ex.message}")
|
||||||
|
|
||||||
|
# to prevent an infinite loop here keep a counter and bail out after N times...
|
||||||
|
attempts += 1
|
||||||
|
|
||||||
|
raise "QuickbooksOauth:ExceededAuthAttempts" if attempts >= 3
|
||||||
|
|
||||||
|
# check if its an invalid_grant first, but assume it is for now
|
||||||
|
refresh_token!
|
||||||
|
|
||||||
|
retry
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def refresh_token!
|
||||||
|
t = oauth_access_token
|
||||||
|
refreshed = t.refresh!
|
||||||
|
|
||||||
|
if refreshed.params['x_refresh_token_expires_in'].to_i > 0
|
||||||
|
oauth2_refresh_token_expires_at = Time.now + refreshed.params['x_refresh_token_expires_in'].to_i.seconds
|
||||||
|
else
|
||||||
|
oauth2_refresh_token_expires_at = 100.days.from_now
|
||||||
|
end
|
||||||
|
|
||||||
|
update!(
|
||||||
|
oauth2_access_token: refreshed.token,
|
||||||
|
oauth2_access_token_expires_at: Time.at(refreshed.expires_at),
|
||||||
|
oauth2_refresh_token: refreshed.refresh_token,
|
||||||
|
oauth2_refresh_token_expires_at: oauth2_refresh_token_expires_at
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def oauth_client
|
||||||
|
self.class.construct_oauth2_client
|
||||||
|
end
|
||||||
|
|
||||||
|
def oauth_access_token
|
||||||
|
OAuth2::AccessToken.new(oauth_client, oauth2_access_token, refresh_token: oauth2_refresh_token)
|
||||||
|
end
|
||||||
|
|
||||||
|
def consumer
|
||||||
|
oauth_access_token
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
|
||||||
|
def construct_oauth2_client
|
||||||
|
options = {
|
||||||
|
site: "https://appcenter.intuit.com/connect/oauth2",
|
||||||
|
authorize_url: "https://appcenter.intuit.com/connect/oauth2",
|
||||||
|
token_url: "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"
|
||||||
|
}
|
||||||
|
OAuth2::Client.new(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -10,31 +10,8 @@
|
|||||||
|
|
||||||
class Qbo < ActiveRecord::Base
|
class Qbo < ActiveRecord::Base
|
||||||
unloadable
|
unloadable
|
||||||
validates_presence_of :token, :company_id, :expire
|
|
||||||
serialize :token
|
include QuickbooksOauth
|
||||||
|
|
||||||
OAUTH_CONSUMER_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey']
|
|
||||||
OAUTH_CONSUMER_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret']
|
|
||||||
|
|
||||||
#
|
|
||||||
# Getter for quickbooks OAuth2 client
|
|
||||||
#
|
|
||||||
def self.get_client
|
|
||||||
oauth_params = {
|
|
||||||
site: "https://appcenter.intuit.com/connect/oauth2",
|
|
||||||
authorize_url: "https://appcenter.intuit.com/connect/oauth2",
|
|
||||||
token_url: "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"
|
|
||||||
}
|
|
||||||
return OAuth2::Client.new(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, oauth_params)
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
|
||||||
# Getter for oauth consumer
|
|
||||||
#
|
|
||||||
def self.get_oauth_consumer
|
|
||||||
# Quickbooks Config Info
|
|
||||||
return $qb_oauth_consumer
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Get a quickbooks base service object for type
|
# Get a quickbooks base service object for type
|
||||||
@@ -42,47 +19,27 @@ class Qbo < ActiveRecord::Base
|
|||||||
#
|
#
|
||||||
def self.get_base(type)
|
def self.get_base(type)
|
||||||
# lets getnourbold access token from the database
|
# lets getnourbold access token from the database
|
||||||
oauth2_client = get_client
|
oauth2_client = construct_oauth2_client
|
||||||
qbo = self.first
|
qbo = self.first
|
||||||
access_token = OAuth2::AccessToken.from_hash(oauth2_client, qbo.token)
|
|
||||||
|
|
||||||
# check to see if we need to refresh the acesstoken
|
qbo.perform_authenticated_request do |access_token|
|
||||||
if qbo.expire.to_time.utc.past?
|
# build the reqiested service
|
||||||
puts "Updating access token"
|
case type
|
||||||
new_access_token_object = access_token.refresh!
|
when :time_activity
|
||||||
qbo.token = new_access_token_object.to_hash
|
return Quickbooks::Service::TimeActivity.new(:company_id => qbo.company_id, :access_token => access_token)
|
||||||
qbo.expire = 1.hour.from_now.utc
|
when :customer
|
||||||
qbo.save!
|
return Quickbooks::Service::Customer.new(:company_id => qbo.company_id, :access_token => access_token)
|
||||||
access_token = new_access_token_object
|
when :invoice
|
||||||
else
|
return Quickbooks::Service::Invoice.new(:company_id => qbo.company_id, :access_token => access_token)
|
||||||
puts "Using current token"
|
when :estimate
|
||||||
end
|
return Quickbooks::Service::Estimate.new(:company_id => qbo.company_id, :access_token => access_token)
|
||||||
|
when :employee
|
||||||
# build the reqiested service
|
return Quickbooks::Service::Employee.new(:company_id => qbo.company_id, :access_token => access_token)
|
||||||
case type
|
else
|
||||||
when :item
|
return access_token
|
||||||
return Quickbooks::Service::Item.new(:company_id => qbo.company_id, :access_token => access_token)
|
end
|
||||||
when :time_activity
|
|
||||||
return Quickbooks::Service::TimeActivity.new(:company_id => qbo.company_id, :access_token => access_token)
|
|
||||||
when :customer
|
|
||||||
return Quickbooks::Service::Customer.new(:company_id => qbo.company_id, :access_token => access_token)
|
|
||||||
when :invoice
|
|
||||||
return Quickbooks::Service::Invoice.new(:company_id => qbo.company_id, :access_token => access_token)
|
|
||||||
when :estimate
|
|
||||||
return Quickbooks::Service::Estimate.new(:company_id => qbo.company_id, :access_token => access_token)
|
|
||||||
when :account
|
|
||||||
return Quickbooks::Service::Account.new(:company_id => qbo.company_id, :access_token => access_token)
|
|
||||||
when :employee
|
|
||||||
return Quickbooks::Service::Employee.new(:company_id => qbo.company_id, :access_token => access_token)
|
|
||||||
else
|
|
||||||
return access_token
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
# Get the QBO account
|
|
||||||
def self.get_account
|
|
||||||
first
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Updates last sync time stamp
|
# Updates last sync time stamp
|
||||||
|
|||||||
18
db/migrate/037_update_qbo_token.rb
Normal file
18
db/migrate/037_update_qbo_token.rb
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#The MIT License (MIT)
|
||||||
|
#
|
||||||
|
#Copyright (c) 2023 rick barrette
|
||||||
|
#
|
||||||
|
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
class UpdateQboToken < ActiveRecord::Migration[5.1]
|
||||||
|
def change
|
||||||
|
add_column :qbos, :oauth2_access_token, :text
|
||||||
|
add_column :qbos, :oauth2_access_token_expires_at, :datetime
|
||||||
|
add_column :qbos, :oauth2_refresh_token, :text
|
||||||
|
add_column :qbos, :oauth2_refresh_token_expires_at, :datetime
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user