Class: Xeroizer::OAuth

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/xeroizer/oauth.rb

Overview

Shamelessly taken from the XeroGateway library by Tim Connor which is shamelessly based on the Twitter Gem’s OAuth implementation by John Nunemaker Thanks!

github.com/tlconnor/xero_gateway twitter.rubyforge.org/ github.com/jnunemaker/twitter/

Defined Under Namespace

Classes: RateLimitExceeded, TokenExpired, TokenInvalid, UnknownError

Constant Summary collapse

XERO_CONSUMER_OPTIONS =
{
  :site               => "https://api.xero.com",
  :request_token_path => "/oauth/RequestToken",
  :access_token_path  => "/oauth/AccessToken",
  :authorize_path     => "/oauth/Authorize",
  :ca_file            => File.expand_path(File.join(File.dirname(__FILE__), 'ca-certificates.crt'))
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctoken, csecret, options = {}) ⇒ OAuth

OAuth constructor.

Parameters:

  • ctoken (String)

    consumer key/token from application developer (found at api.xero.com for your application).

  • csecret (String)

    consumer secret from application developer (found at api.xero.com for your application).

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :access_token_path (String)

    base URL path for getting an AccessToken (default: “/oauth/AccessToken”)

  • :authorize_path (String)

    base URL path for authorising (default: “/oauth/Authorize”)

  • :ca_file (String)

    file containing SSL root certificates (default: “lib/xeroizer/ca-certificates.crt”)

  • :private_key_file (String)

    private key used when :signature_method set to RSA-SHA1 (used for PartnerApplication and PrivateApplication modes)

  • :request_token_path (String)

    base URL path for getting a RequestToken (default: “/oauth/RequestToken”)

  • :signature_method (String)

    method usd to sign requests (default: OAuth library default)

  • :site (String)

    base site for API requests (default: “api.xero.com”)

  • :http_debug_output (IO)

    filehandle to write HTTP traffic to

  • :ssl_client_cert (OpenSSL:X509::Certificate)

    client-side SSL certificate to use for requests (used for PartnerApplication mode)

  • :ssl_client_key (OpenSSL::PKey::RSA)

    client-side SSL private key to use for requests (used for PartnerApplication mode)



70
71
72
73
# File 'lib/xeroizer/oauth.rb', line 70

def initialize(ctoken, csecret, options = {})
  @ctoken, @csecret = ctoken, csecret
  @consumer_options = XERO_CONSUMER_OPTIONS.merge(options)
end

Instance Attribute Details

#authorization_expires_atObject (readonly)



50
51
52
# File 'lib/xeroizer/oauth.rb', line 50

def authorization_expires_at
  @authorization_expires_at
end

#consumer_optionsObject (readonly)



50
51
52
# File 'lib/xeroizer/oauth.rb', line 50

def consumer_options
  @consumer_options
end

#csecretObject (readonly)



50
51
52
# File 'lib/xeroizer/oauth.rb', line 50

def csecret
  @csecret
end

#ctokenObject (readonly)



50
51
52
# File 'lib/xeroizer/oauth.rb', line 50

def ctoken
  @ctoken
end

#expires_atObject (readonly)



50
51
52
# File 'lib/xeroizer/oauth.rb', line 50

def expires_at
  @expires_at
end

#session_handleObject



54
55
56
# File 'lib/xeroizer/oauth.rb', line 54

def session_handle
  @session_handle
end

Instance Method Details

#access_tokenObject

AccessToken created from authorize_from_access method.



98
99
100
# File 'lib/xeroizer/oauth.rb', line 98

def access_token
  ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
end

#authorize_from_access(atoken, asecret) ⇒ Object

Used for PRIVATE applications where the AccessToken uses the token/secret from Xero which would normally be used in the request. No request authorisation necessary.

For PUBLIC/PARTNER applications this is used to recreate a client from a stored AccessToken key/secret.



108
109
110
# File 'lib/xeroizer/oauth.rb', line 108

def authorize_from_access(atoken, asecret)
  @atoken, @asecret = atoken, asecret
end

#authorize_from_request(rtoken, rsecret, params = {}) ⇒ Object

Create an AccessToken from a PUBLIC/PARTNER authorisation.



91
92
93
94
95
# File 'lib/xeroizer/oauth.rb', line 91

def authorize_from_request(rtoken, rsecret, params = {})
  request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
  access_token = request_token.get_access_token(params)
  update_attributes_from_token(access_token)
end

#consumerOAuth::Consumer

OAuth consumer creator.

Returns:

  • (OAuth::Consumer)

    consumer object for GET/POST/PUT methods.



78
79
80
# File 'lib/xeroizer/oauth.rb', line 78

def consumer
  create_consumer
end

#renew_access_token(atoken = nil, asecret = nil, session_handle = nil) ⇒ Object

Renew an access token from a previously authorised token for a PARTNER application.



114
115
116
117
118
119
120
121
# File 'lib/xeroizer/oauth.rb', line 114

def renew_access_token(atoken = nil, asecret = nil, session_handle = nil)
  old_token = ::OAuth::RequestToken.new(consumer, atoken || @atoken, asecret || @asecret)
  access_token = old_token.get_access_token({
    :oauth_session_handle => (session_handle || @session_handle), 
    :token => old_token
  })
  update_attributes_from_token(access_token)
end

#request_token(params = {}) ⇒ Object

RequestToken for PUBLIC/PARTNER authorisation (used to redirect to Xero for authentication).

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :oauth_callback (String)

    URL to redirect user to when they have authenticated your application with Xero. If not specified, the user will be shown an authorisation code on the screen that they need to get into your application.



86
87
88
# File 'lib/xeroizer/oauth.rb', line 86

def request_token(params = {})
  consumer.get_request_token(params)
end