Class: JIRA::OauthClient

Inherits:
RequestClient show all
Extended by:
Forwardable
Defined in:
lib/jira/oauth_client.rb

Defined Under Namespace

Classes: UninitializedAccessTokenError

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :signature_method   => 'RSA-SHA1',
  :request_token_path => "/plugins/servlet/oauth/request-token",
  :authorize_path     => "/plugins/servlet/oauth/authorize",
  :access_token_path  => "/plugins/servlet/oauth/access-token",
  :private_key_file   => "rsakey.pem",
  :consumer_key       => nil,
  :consumer_secret    => nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RequestClient

#request

Constructor Details

#initialize(options) ⇒ OauthClient

Returns a new instance of OauthClient.



33
34
35
36
# File 'lib/jira/oauth_client.rb', line 33

def initialize(options)
  @options = DEFAULT_OPTIONS.merge(options)
  @consumer = init_oauth_consumer(@options)
end

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer.



28
29
30
# File 'lib/jira/oauth_client.rb', line 28

def consumer
  @consumer
end

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/jira/oauth_client.rb', line 29

def options
  @options
end

Instance Method Details

#access_tokenObject

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



69
70
71
72
# File 'lib/jira/oauth_client.rb', line 69

def access_token
  raise UninitializedAccessTokenError.new unless @access_token
  @access_token
end

#init_access_token(params) ⇒ Object

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



58
59
60
# File 'lib/jira/oauth_client.rb', line 58

def init_access_token(params)
  @access_token = request_token.get_access_token(params)
end

#init_oauth_consumer(options) ⇒ Object



38
39
40
41
42
43
# File 'lib/jira/oauth_client.rb', line 38

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]
  OAuth::Consumer.new(@options[:consumer_key],@options[:consumer_secret],@options)
end

#make_request(http_method, path, body = '', headers = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/jira/oauth_client.rb', line 74

def make_request(http_method, path, body='', headers={})
  case http_method
  when :delete, :get, :head
    response = access_token.send http_method, path, headers
  when :post, :put
    response = access_token.send http_method, path, body, headers
  end
  response
end

#request_token(options = {}, *arguments, &block) ⇒ Object

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



47
48
49
# File 'lib/jira/oauth_client.rb', line 47

def request_token(options = {}, *arguments, &block)
  @request_token ||= get_request_token(options, *arguments, block)
end

#set_access_token(token, secret) ⇒ Object

Sets the access token from a preexisting token and secret.



63
64
65
# File 'lib/jira/oauth_client.rb', line 63

def set_access_token(token, secret)
  @access_token = OAuth::AccessToken.new(@consumer, token, secret)
end

#set_request_token(token, secret) ⇒ Object

Sets the request token from a given token and secret.



52
53
54
# File 'lib/jira/oauth_client.rb', line 52

def set_request_token(token, secret)
  @request_token = OAuth::RequestToken.new(@consumer, token, secret)
end