5
5
require 'forwardable'
6
6
7
7
module JIRA
8
+ # Client using OAuth 1.0
9
+ #
10
+ # @!attribute [rw] consumer
11
+ # @return [OAuth::Consumer] The oauth consumer object
12
+ # @!attribute [r] options
13
+ # @return [Hash] The oauth connection options
14
+ # @!attribute [r] access_token
15
+ # @return [OAuth::AccessToken] The oauth access token
8
16
class OauthClient < RequestClient
17
+ # @private
9
18
DEFAULT_OPTIONS = {
10
19
signature_method : 'RSA-SHA1' ,
11
20
request_token_path : '/plugins/servlet/oauth/request-token' ,
@@ -31,11 +40,28 @@ def message
31
40
32
41
def_instance_delegators :@consumer , :key , :secret , :get_request_token
33
42
43
+ # @param [Hash] options Options as passed from JIRA::Client constructor.
44
+ # @option options [String] :signature_method The signature method to use (defaults to 'RSA-SHA1')
45
+ # @option options [String] :request_token_path The path to request a token (defaults to '/plugins/servlet/oauth/request-token')
46
+ # @option options [String] :authorize_path The path to authorize a token (defaults to '/plugins/servlet/oauth/authorize')
47
+ # @option options [String] :access_token_path The path to access a token (defaults to '/plugins/servlet/oauth/access-token')
48
+ # @option options [String] :private_key_file The path to the private key file
49
+ # @option options [String] :consumer_key The OAuth 1.0 consumer key
50
+ # @option options [String] :consumer_secret The OAuth 1.0 consumer secret
51
+ # @option options [Hash] :default_headers Additional headers for requests
52
+ # @option options [String] :proxy_uri Proxy URI
53
+ # @option options [String] :proxy_user Proxy user
54
+ # @option options [String] :proxy_password Proxy Password
34
55
def initialize ( options )
35
56
@options = DEFAULT_OPTIONS . merge ( options )
36
57
@consumer = init_oauth_consumer ( @options )
37
58
end
38
59
60
+ # @private
61
+ # Initialises the OAuth consumer object.
62
+ # Generally you should not call this method directly, it is called by the constructor.
63
+ # @param [Hash] _options The options hash
64
+ # @return [OAuth::Consumer] The OAuth consumer object
39
65
def init_oauth_consumer ( _options )
40
66
@options [ :request_token_path ] = @options [ :context_path ] + @options [ :request_token_path ]
41
67
@options [ :authorize_path ] = @options [ :context_path ] + @options [ :authorize_path ]
@@ -47,22 +73,31 @@ def init_oauth_consumer(_options)
47
73
48
74
# Returns the current request token if it is set, else it creates
49
75
# and sets a new token.
76
+ # @param [Hash] options
50
77
def request_token ( options = { } , ...)
51
78
@request_token ||= get_request_token ( options , ...)
52
79
end
53
80
54
81
# Sets the request token from a given token and secret.
82
+ # @param [String] token The request token
83
+ # @param [String] secret The request token secret
84
+ # @return [OAuth::RequestToken] The request token object
55
85
def set_request_token ( token , secret )
56
86
@request_token = OAuth ::RequestToken . new ( @consumer , token , secret )
57
87
end
58
88
59
89
# Initialises and returns a new access token from the params hash
60
90
# returned by the OAuth transaction.
91
+ # @param [Hash] params The params hash returned by the OAuth transaction
92
+ # @return [OAuth::AccessToken] The access token object
61
93
def init_access_token ( params )
62
94
@access_token = request_token . get_access_token ( params )
63
95
end
64
96
65
97
# Sets the access token from a preexisting token and secret.
98
+ # @param [String] token The access token
99
+ # @param [String] secret The access token secret
100
+ # @return [OAuth::AccessToken] The access token object
66
101
def set_access_token ( token , secret )
67
102
@access_token = OAuth ::AccessToken . new ( @consumer , token , secret )
68
103
@authenticated = true
@@ -71,12 +106,25 @@ def set_access_token(token, secret)
71
106
72
107
# Returns the current access token. Raises an
73
108
# JIRA::Client::UninitializedAccessTokenError exception if it is not set.
109
+ # @return [OAuth::AccessToken] The access token object
74
110
def access_token
75
111
raise UninitializedAccessTokenError unless @access_token
76
112
77
113
@access_token
78
114
end
79
115
116
+ # Makes a request to the JIRA server using the oauth gem.
117
+ #
118
+ # Generally you should not call this method directly, but use the helper methods in JIRA::Client.
119
+ #
120
+ # File uploads are not supported with this method. Use make_multipart_request instead.
121
+ #
122
+ # @param [Symbol] http_method The HTTP method to use
123
+ # @param [String] url The JIRA REST URL to call
124
+ # @param [String] body The body of the request
125
+ # @param [Hash] headers The headers to send with the request
126
+ # @return [Net::HTTPResponse] The response object
127
+ # @raise [JIRA::HTTPError] If the response is not an HTTP success code
80
128
def make_request ( http_method , url , body = '' , headers = { } )
81
129
# When using oauth_2legged we need to add an empty oauth_token parameter to every request.
82
130
if @options [ :auth_type ] == :oauth_2legged
@@ -100,6 +148,17 @@ def make_request(http_method, url, body = '', headers = {})
100
148
response
101
149
end
102
150
151
+ # Makes a multipart request to the JIRA server using the oauth gem.
152
+ #
153
+ # This is used for file uploads.
154
+ #
155
+ # Generally you should not call this method directly, but use the helper methods in JIRA::Client.
156
+ #
157
+ # @param [String] url The JIRA REST URL to call
158
+ # @param [Hash] data The Net::HTTP::Post::Multipart data to send with the request
159
+ # @param [Hash] headers The headers to send with the request
160
+ # @return [Net::HTTPResponse] The response object
161
+ # @raise [JIRA::HTTPError] If the response is not an HTTP success code
103
162
def make_multipart_request ( url , data , headers = { } )
104
163
request = Net ::HTTP ::Post ::Multipart . new url , data , headers
105
164
@@ -110,6 +169,8 @@ def make_multipart_request(url, data, headers = {})
110
169
response
111
170
end
112
171
172
+ # Returns true if the client is authenticated.
173
+ # @return [Boolean] True if the client is authenticated
113
174
def authenticated?
114
175
@authenticated
115
176
end
0 commit comments