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
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RequestClient

#request, #request_multipart

Constructor Details

#initialize(options) ⇒ OauthClient

Returns a new instance of OauthClient.


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

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

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer


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

def consumer
  @consumer
end

#optionsObject (readonly)

Returns the value of attribute options


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

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.


72
73
74
75
# File 'lib/jira/oauth_client.rb', line 72

def access_token
  raise UninitializedAccessTokenError unless @access_token
  @access_token
end

#authenticated?Boolean

Returns:

  • (Boolean)

110
111
112
# File 'lib/jira/oauth_client.rb', line 110

def authenticated?
  @authenticated
end

#init_access_token(params) ⇒ Object

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


59
60
61
# 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) ⇒ Object


37
38
39
40
41
42
43
44
# 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 = {}) ⇒ Object


100
101
102
103
104
105
106
107
108
# 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 = {}) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

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


48
49
50
# 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) ⇒ Object

Sets the access token from a preexisting token and secret.


64
65
66
67
68
# 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) ⇒ Object

Sets the request token from a given token and secret.


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

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