class JIRA::OauthClient

Constants

DEFAULT_OPTIONS

Attributes

consumer[RW]
options[R]

Public Class Methods

new(options) click to toggle source
# File lib/jira/oauth_client.rb, line 32
def initialize(options)
  @options = DEFAULT_OPTIONS.merge(options)
  @consumer = init_oauth_consumer(@options)
end

Public Instance Methods

access_token() click to toggle source

Returns the current access token. Raises an JIRA::Client::UninitializedAccessTokenError exception if it is not set.

# File lib/jira/oauth_client.rb, line 72
def access_token
  raise UninitializedAccessTokenError unless @access_token
  @access_token
end
authenticated?() click to toggle source
# File lib/jira/oauth_client.rb, line 110
def authenticated?
  @authenticated
end
init_access_token(params) click to toggle source

Initialises and returns a new access token from the params hash returned by the OAuth transaction.

# File lib/jira/oauth_client.rb, line 59
def init_access_token(params)
  @access_token = request_token.get_access_token(params)
end
init_oauth_consumer(_options) click to toggle source
# File lib/jira/oauth_client.rb, line 37
def init_oauth_consumer(_options)
  @options[:request_token_path] = @options[:context_path] + @options[:request_token_path]
  @options[:authorize_path] = @options[:context_path] + @options[:authorize_path]
  @options[:access_token_path] = @options[:context_path] + @options[:access_token_path]
  # proxy_address does not exist in oauth's gem context but proxy does
  @options[:proxy] = @options[:proxy_address] if @options[:proxy_address]
  OAuth::Consumer.new(@options[:consumer_key], @options[:consumer_secret], @options)
end
make_multipart_request(url, data, headers = {}) click to toggle source
# File lib/jira/oauth_client.rb, line 100
def make_multipart_request(url, data, headers = {})
  request = Net::HTTP::Post::Multipart.new url, data, headers

  access_token.sign! request

  response = consumer.http.request(request)
  @authenticated = true
  response
end
make_request(http_method, url, body = '', headers = {}) click to toggle source
# File lib/jira/oauth_client.rb, line 77
def make_request(http_method, url, body = '', headers = {})
  # When using oauth_2legged we need to add an empty oauth_token parameter to every request.
  if @options[:auth_type] == :oauth_2legged
    oauth_params_str = 'oauth_token='
    uri = URI.parse(url)
    uri.query = if uri.query.to_s == ''
                  oauth_params_str
                else
                  uri.query + '&' + oauth_params_str
                end
    url = uri.to_s
  end

  case http_method
  when :delete, :get, :head
    response = access_token.send http_method, url, headers
  when :post, :put
    response = access_token.send http_method, url, body, headers
  end
  @authenticated = true
  response
end
request_token(options = {}, *arguments, &block) click to toggle source

Returns the current request token if it is set, else it creates and sets a new token.

# File lib/jira/oauth_client.rb, line 48
def request_token(options = {}, *arguments, &block)
  @request_token ||= get_request_token(options, *arguments, &block)
end
set_access_token(token, secret) click to toggle source

Sets the access token from a preexisting token and secret.

# File lib/jira/oauth_client.rb, line 64
def set_access_token(token, secret)
  @access_token = OAuth::AccessToken.new(@consumer, token, secret)
  @authenticated = true
  @access_token
end
set_request_token(token, secret) click to toggle source

Sets the request token from a given token and secret.

# File lib/jira/oauth_client.rb, line 53
def set_request_token(token, secret)
  @request_token = OAuth::RequestToken.new(@consumer, token, secret)
end