module HomeAway::API::Util::OAuth

Public Instance Methods

auth_url() click to toggle source

@return [String] the authorization URL you need to redirect a HomeAway user

to grant you access to their account.
# File lib/homeaway/api/util/oauth.rb, line 30
def auth_url
  oauth_client_strategy.authorize_url(state: state)
end
credentials() click to toggle source

@return [String] the Base64 encoded credentials for the current client

# File lib/homeaway/api/util/oauth.rb, line 24
def credentials
  Base64.strict_encode64 "#{@configuration.client_id}:#{@configuration.client_secret}"
end
oauth_code=(code) click to toggle source

completes the oauth flow @param code [String] service ticket to authenticate @return [Boolean] true if the authentication succeeded, false otherwise

# File lib/homeaway/api/util/oauth.rb, line 43
def oauth_code=(code)
  begin
    auth_code_strategy = oauth_client_strategy
    token = auth_code_strategy.get_token(code, :headers => {'Authorization' => "Basic #{credentials}"})
    @token = token.token
    @token_expires = Time.at token.expires_at
    @refresh_token = token.refresh_token
    @mode = :three_legged
    return true
  rescue => e
    if e.is_a? OAuth2::Error
      error_class = HomeAway::API::Errors.for_http_code e.response.status
      raise error_class.new(JSON.parse(e.response.response.body))
    end
    raise HomeAway::API::Errors::HomeAwayAPIError.new e.message
  end
end
state() click to toggle source

@return [String] a 48 characters long, securely random string, used to mitigate

CSRF attacks during the authorization process.
# File lib/homeaway/api/util/oauth.rb, line 36
def state
  @_state ||= @configuration.state || SecureRandom.hex(24)
end

Private Instance Methods

oauth_client() click to toggle source
# File lib/homeaway/api/util/oauth.rb, line 67
def oauth_client
  OAuth2::Client.new(@configuration.client_id,
                     @configuration.client_secret,
                     :site => oauth_site,
                     :raise_errors => true
  )
end
oauth_client_strategy() click to toggle source
# File lib/homeaway/api/util/oauth.rb, line 75
def oauth_client_strategy
  client = oauth_client
  client.auth_code
end
oauth_site() click to toggle source
# File lib/homeaway/api/util/oauth.rb, line 63
def oauth_site
  @configuration[:oauth_site] ||= @configuration[:site]
end
refresh() click to toggle source
# File lib/homeaway/api/util/oauth.rb, line 99
def refresh
  begin
    token = OAuth2::AccessToken.new(oauth_client, nil, {:refresh_token => @refresh_token})
    params = {
        :headers => {'Authorization' => "Basic #{credentials}"}
    }
    token = token.refresh!(params)
    @token = token.token
    @token_expires = Time.at token.expires_at
    @refresh_token = token.refresh_token
    @mode = :three_legged
    return true
  rescue => e
    if e.is_a? OAuth2::Error
      error_class = HomeAway::API::Errors.for_http_code e.response.status
      raise error_class.new(JSON.parse(e.response.response.body))
    end
    raise HomeAway::API::Errors::HomeAwayAPIError.new e.message
  end
end
two_legged!() click to toggle source
# File lib/homeaway/api/util/oauth.rb, line 80
def two_legged!
  begin
    client = oauth_client
    client_credentials_strategy = client.client_credentials
    token = client_credentials_strategy.get_token
    @token = token.token
    @token_expires = Time.at token.expires_at
    @refresh_token = token.refresh_token
    @mode = :two_legged
    return true
  rescue => e
    if e.is_a? OAuth2::Error
      error_class = HomeAway::API::Errors.for_http_code e.response.status
      raise error_class.new(JSON.parse(e.response.response.body))
    end
    raise HomeAway::API::Errors::HomeAwayAPIError.new e.message
  end
end