Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Partner app implementation

  • Loading branch information...
commit 557cc9dd5fe9a14327f204739abd7bc763032518 1 parent 289aab3
@nikz nikz authored
View
36 examples/partner_app.rb
@@ -0,0 +1,36 @@
+require 'rubygems'
+require 'pp'
+
+require File.dirname(__FILE__) + '/../lib/xero_gateway.rb'
+
+XERO_CONSUMER_KEY = "YOUR CONSUMER KEY"
+XERO_CONSUMER_SECRET = "YOUR CONSUMER SERET"
+
+ENTRUST_CERT_NAME = "YOUR_ENTRUST_CERT.pem"
+ENTRUST_KEY_NAME = "YOUR_ENTRUST_KEY.pem"
+PRIVATE_KEY = "YOUR_PRIVATE_KEY.pem"
+
+gateway = XeroGateway::PartnerApp.new(XERO_CONSUMER_KEY, XERO_CONSUMER_SECRET,
+ :ssl_client_cert => File.join(File.dirname(__FILE__), ENTRUST_CERT_NAME)
+ :ssl_client_key => File.join(File.dirname(__FILE__), ENTRUST_KEY_NAME),
+ :private_key_file => File.join(File.dirname(__FILE__), PRIVATE_KEY))
+
+# authorize in browser
+%x(open #{gateway.request_token.authorize_url})
+
+puts "Enter the verification code from Xero?"
+oauth_verifier = gets.chomp
+
+gateway.authorize_from_request(gateway.request_token.token, gateway.request_token.secret, :oauth_verifier => oauth_verifier)
+
+puts "Your access token/secret: #{gateway.access_token.token}, #{gateway.access_token.secret}. Expires: #{gateway.authorization_expires_at}"
+puts "(Good for 30 Minutes - but we can renew it!)"
+
+puts "Session Handle: #{gateway.session_handle}"
+
+# Example API Call
+pp gateway.get_contacts.contacts.map(&:name)
+
+# Renew!
+gateway.renew_access_token(gateway.access_token.token, gateway.access_token.secret, gateway.session_handle)
+puts "Your renewed access token/secret is: #{gateway.access_token.token}, #{gateway.access_token.secret}. Expires: #{gateway.authorization_expires_at}"
View
14 lib/oauth/oauth_consumer.rb
@@ -0,0 +1,14 @@
+module OAuth
+ class Consumer
+
+ def http_with_ssl_client_certificates(*args)
+ @http ||= http_without_ssl_client_certificates(*args).tap do |http|
+ http.cert = options[:ssl_client_cert]
+ http.key = options[:ssl_client_key]
+ end
+ end
+
+ alias_method_chain :http, :ssl_client_certificates
+
+ end
+end
View
5 lib/xero_gateway.rb
@@ -7,7 +7,9 @@
require "oauth"
require 'oauth/signature/rsa/sha1'
require "forwardable"
-require "active_support/inflector"
+require "active_support/all"
+
+require File.join(File.dirname(__FILE__), 'oauth', 'oauth_consumer')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'http_encoding_helper')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'http')
@@ -32,3 +34,4 @@
require File.join(File.dirname(__FILE__), 'xero_gateway', 'exceptions')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'gateway')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'private_app')
+require File.join(File.dirname(__FILE__), 'xero_gateway', 'partner_app')
View
2  lib/xero_gateway/gateway.rb
@@ -7,7 +7,7 @@ class Gateway
attr_accessor :client, :xero_url, :logger
extend Forwardable
- def_delegators :client, :request_token, :access_token, :authorize_from_request, :authorize_from_access
+ def_delegators :client, :request_token, :access_token, :authorize_from_request, :authorize_from_access, :authorization_expires_at
#
# The consumer key and secret here correspond to those provided
View
31 lib/xero_gateway/oauth.rb
@@ -25,7 +25,7 @@ class UnknownError < StandardError; end
extend Forwardable
def_delegators :access_token, :get, :post, :put, :delete
- attr_reader :ctoken, :csecret, :consumer_options
+ attr_reader :ctoken, :csecret, :consumer_options, :session_handle, :authorization_expires_at
def initialize(ctoken, csecret, options = {})
@ctoken, @csecret = ctoken, csecret
@@ -44,6 +44,8 @@ def authorize_from_request(rtoken, rsecret, params = {})
request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
access_token = request_token.get_access_token(params)
@atoken, @asecret = access_token.token, access_token.secret
+
+ update_attributes_from_token(access_token)
end
def access_token
@@ -53,6 +55,33 @@ def access_token
def authorize_from_access(atoken, asecret)
@atoken, @asecret = atoken, asecret
end
+
+ # Renewing access tokens only works for Partner applications
+ def renew_access_token(access_token = nil, access_secret = nil, session_handle = nil)
+ access_token ||= @atoken
+ access_secret ||= @asecret
+ session_handle ||= @session_handle
+
+ old_token = ::OAuth::RequestToken.new(consumer, access_token, access_secret)
+
+ access_token = old_token.get_access_token({
+ :oauth_session_handle => session_handle,
+ :token => old_token
+ })
+
+ update_attributes_from_token(access_token)
+ end
+
+ private
+
+ # Update instance variables with those from the AccessToken.
+ def update_attributes_from_token(access_token)
+ @expires_at = Time.now + access_token.params[:oauth_expires_in].to_i
+ @authorization_expires_at = Time.now + access_token.params[:oauth_authorization_expires_in].to_i
+ @session_handle = access_token.params[:oauth_session_handle]
+ @atoken, @asecret = access_token.token, access_token.secret
+ @access_token = nil
+ end
end
end
View
30 lib/xero_gateway/partner_app.rb
@@ -0,0 +1,30 @@
+module XeroGateway
+ class PartnerApp < Gateway
+
+ class CertificateRequired < StandardError; end
+
+ NO_SSL_CLIENT_CERT_MESSAGE = "You need to provide a client ssl certificate and key pair (these are the ones you got from Entrust and should not be password protected) as :ssl_client_cert and :ssl_client_key (should be .crt or .pem files)"
+ NO_PRIVATE_KEY_ERROR_MESSAGE = "You need to provide your private key (corresponds to the public key you uploaded at api.xero.com) as :private_key_file (should be .crt or .pem files)"
+
+ def_delegators :client, :session_handle, :renew_access_token
+
+ def initialize(consumer_key, consumer_secret, options = {})
+
+ raise CertificateRequired.new(NO_SSL_CLIENT_CERT_MESSAGE) unless options[:ssl_client_cert]
+ raise CertificateRequired.new(NO_SSL_CLIENT_CERT_MESSAGE) unless options[:ssl_client_key]
+ raise CertificateRequired.new(NO_PRIVATE_KEY_ERROR_MESSAGE) unless options[:private_key_file]
+
+ options.merge!(
+ :site => "https://api-partner.network.xero.com",
+ :authorize_url => 'https://api.xero.com/oauth/Authorize',
+ :signature_method => 'RSA-SHA1',
+ :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read(options[:ssl_client_cert])),
+ :ssl_client_key => OpenSSL::PKey::RSA.new(File.read(options[:ssl_client_key])),
+ :private_key_file => options[:private_key_file]
+ )
+
+ @xero_url = options[:xero_url] || "https://api-partner.xero.com/api.xro/2.0"
+ @client = OAuth.new(consumer_key, consumer_secret, options)
+ end
+ end
+end

0 comments on commit 557cc9d

Please sign in to comment.
Something went wrong with that request. Please try again.