Class: JIRA::OauthClient
- Inherits:
-
RequestClient
- Object
- RequestClient
- JIRA::OauthClient
- Extended by:
- Forwardable
- Defined in:
- lib/jira/oauth_client.rb
Overview
Client using OAuth 1.0
Defined Under Namespace
Classes: UninitializedAccessTokenError
Instance Attribute Summary collapse
-
#access_token ⇒ OAuth::AccessToken
readonly
Returns the current access token.
-
#consumer ⇒ OAuth::Consumer
The oauth consumer object.
-
#options ⇒ Hash
readonly
The oauth connection options.
Instance Method Summary collapse
-
#authenticated? ⇒ Boolean
Returns true if the client is authenticated.
-
#init_access_token(params) ⇒ OAuth::AccessToken
Initialises and returns a new access token from the params hash returned by the OAuth transaction.
-
#initialize(options) ⇒ OauthClient
constructor
Generally not used directly, but through JIRA::Client.
-
#make_multipart_request(url, data, headers = {}) ⇒ Net::HTTPResponse
Makes a multipart request to the JIRA server using the oauth gem.
-
#make_request(http_method, url, body = '', headers = {}) ⇒ Net::HTTPResponse
Makes a request to the JIRA server using the oauth gem.
-
#request_token(options = {}) ⇒ Object
Returns the current request token if it is set, else it creates and sets a new token.
-
#set_access_token(token, secret) ⇒ OAuth::AccessToken
Sets the access token from a preexisting token and secret.
-
#set_request_token(token, secret) ⇒ OAuth::RequestToken
Sets the request token from a given token and secret.
Methods inherited from RequestClient
Constructor Details
#initialize(options) ⇒ OauthClient
Generally not used directly, but through JIRA::Client.
56 57 58 59 |
# File 'lib/jira/oauth_client.rb', line 56 def initialize() @options = DEFAULT_OPTIONS.merge() @consumer = init_oauth_consumer(@options) end |
Instance Attribute Details
#access_token ⇒ OAuth::AccessToken (readonly)
Returns the current access token. Raises an JIRA::Client::UninitializedAccessTokenError exception if it is not set.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/jira/oauth_client.rb', line 16 class OauthClient < RequestClient # @private 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 # This exception is thrown when the client is used before the OAuth access token # has been initialized. class UninitializedAccessTokenError < StandardError def 'init_access_token must be called before using the client' end end extend Forwardable attr_accessor :consumer attr_reader :options def_instance_delegators :@consumer, :key, :secret, :get_request_token # Generally not used directly, but through JIRA::Client. # @param [Hash] options Options as passed from JIRA::Client constructor. # @option options [String] :signature_method The signature method to use (defaults to 'RSA-SHA1') # @option options [String] :request_token_path The path to request a token (defaults to '/plugins/servlet/oauth/request-token') # @option options [String] :authorize_path The path to authorize a token (defaults to '/plugins/servlet/oauth/authorize') # @option options [String] :access_token_path The path to access a token (defaults to '/plugins/servlet/oauth/access-token') # @option options [String] :private_key_file The path to the private key file # @option options [String] :consumer_key The OAuth 1.0 consumer key # @option options [String] :consumer_secret The OAuth 1.0 consumer secret # @option options [Hash] :default_headers Additional headers for requests # @option options [String] :proxy_uri Proxy URI # @option options [String] :proxy_user Proxy user # @option options [String] :proxy_password Proxy Password def initialize() @options = DEFAULT_OPTIONS.merge() @consumer = init_oauth_consumer(@options) end # @private # Initialises the OAuth consumer object. # Generally you should not call this method directly, it is called by the constructor. # @param [Hash] _options The options hash # @return [OAuth::Consumer] The OAuth consumer object def init_oauth_consumer() @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 # Returns the current request token if it is set, else it creates # and sets a new token. # @param [Hash] options def request_token( = {}, ...) @request_token ||= get_request_token(, ...) end # Sets the request token from a given token and secret. # @param [String] token The request token # @param [String] secret The request token secret # @return [OAuth::RequestToken] The request token object def set_request_token(token, secret) @request_token = OAuth::RequestToken.new(@consumer, token, secret) end # Initialises and returns a new access token from the params hash # returned by the OAuth transaction. # @param [Hash] params The params hash returned by the OAuth transaction # @return [OAuth::AccessToken] The access token object def init_access_token(params) @access_token = request_token.get_access_token(params) end # Sets the access token from a preexisting token and secret. # @param [String] token The access token # @param [String] secret The access token secret # @return [OAuth::AccessToken] The access token object def set_access_token(token, secret) @access_token = OAuth::AccessToken.new(@consumer, token, secret) @authenticated = true @access_token end # Returns the current access token. Raises an # JIRA::Client::UninitializedAccessTokenError exception if it is not set. # @return [OAuth::AccessToken] The access token object def access_token raise UninitializedAccessTokenError unless @access_token @access_token end # Makes a request to the JIRA server using the oauth gem. # # Generally you should not call this method directly, but use the helper methods in JIRA::Client. # # File uploads are not supported with this method. Use make_multipart_request instead. # # @param [Symbol] http_method The HTTP method to use # @param [String] url The JIRA REST URL to call # @param [String] body The body of the request # @param [Hash] headers The headers to send with the request # @return [Net::HTTPResponse] The response object # @raise [JIRA::HTTPError] If the response is not an HTTP success code 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 # Makes a multipart request to the JIRA server using the oauth gem. # # This is used for file uploads. # # Generally you should not call this method directly, but use the helper methods in JIRA::Client. # # @param [String] url The JIRA REST URL to call # @param [Hash] data The Net::HTTP::Post::Multipart data to send with the request # @param [Hash] headers The headers to send with the request # @return [Net::HTTPResponse] The response object # @raise [JIRA::HTTPError] If the response is not an HTTP success code 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 # Returns true if the client is authenticated. # @return [Boolean] True if the client is authenticated def authenticated? @authenticated end end |
#consumer ⇒ OAuth::Consumer
Returns The oauth consumer object.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/jira/oauth_client.rb', line 16 class OauthClient < RequestClient # @private 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 # This exception is thrown when the client is used before the OAuth access token # has been initialized. class UninitializedAccessTokenError < StandardError def 'init_access_token must be called before using the client' end end extend Forwardable attr_accessor :consumer attr_reader :options def_instance_delegators :@consumer, :key, :secret, :get_request_token # Generally not used directly, but through JIRA::Client. # @param [Hash] options Options as passed from JIRA::Client constructor. # @option options [String] :signature_method The signature method to use (defaults to 'RSA-SHA1') # @option options [String] :request_token_path The path to request a token (defaults to '/plugins/servlet/oauth/request-token') # @option options [String] :authorize_path The path to authorize a token (defaults to '/plugins/servlet/oauth/authorize') # @option options [String] :access_token_path The path to access a token (defaults to '/plugins/servlet/oauth/access-token') # @option options [String] :private_key_file The path to the private key file # @option options [String] :consumer_key The OAuth 1.0 consumer key # @option options [String] :consumer_secret The OAuth 1.0 consumer secret # @option options [Hash] :default_headers Additional headers for requests # @option options [String] :proxy_uri Proxy URI # @option options [String] :proxy_user Proxy user # @option options [String] :proxy_password Proxy Password def initialize() @options = DEFAULT_OPTIONS.merge() @consumer = init_oauth_consumer(@options) end # @private # Initialises the OAuth consumer object. # Generally you should not call this method directly, it is called by the constructor. # @param [Hash] _options The options hash # @return [OAuth::Consumer] The OAuth consumer object def init_oauth_consumer() @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 # Returns the current request token if it is set, else it creates # and sets a new token. # @param [Hash] options def request_token( = {}, ...) @request_token ||= get_request_token(, ...) end # Sets the request token from a given token and secret. # @param [String] token The request token # @param [String] secret The request token secret # @return [OAuth::RequestToken] The request token object def set_request_token(token, secret) @request_token = OAuth::RequestToken.new(@consumer, token, secret) end # Initialises and returns a new access token from the params hash # returned by the OAuth transaction. # @param [Hash] params The params hash returned by the OAuth transaction # @return [OAuth::AccessToken] The access token object def init_access_token(params) @access_token = request_token.get_access_token(params) end # Sets the access token from a preexisting token and secret. # @param [String] token The access token # @param [String] secret The access token secret # @return [OAuth::AccessToken] The access token object def set_access_token(token, secret) @access_token = OAuth::AccessToken.new(@consumer, token, secret) @authenticated = true @access_token end # Returns the current access token. Raises an # JIRA::Client::UninitializedAccessTokenError exception if it is not set. # @return [OAuth::AccessToken] The access token object def access_token raise UninitializedAccessTokenError unless @access_token @access_token end # Makes a request to the JIRA server using the oauth gem. # # Generally you should not call this method directly, but use the helper methods in JIRA::Client. # # File uploads are not supported with this method. Use make_multipart_request instead. # # @param [Symbol] http_method The HTTP method to use # @param [String] url The JIRA REST URL to call # @param [String] body The body of the request # @param [Hash] headers The headers to send with the request # @return [Net::HTTPResponse] The response object # @raise [JIRA::HTTPError] If the response is not an HTTP success code 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 # Makes a multipart request to the JIRA server using the oauth gem. # # This is used for file uploads. # # Generally you should not call this method directly, but use the helper methods in JIRA::Client. # # @param [String] url The JIRA REST URL to call # @param [Hash] data The Net::HTTP::Post::Multipart data to send with the request # @param [Hash] headers The headers to send with the request # @return [Net::HTTPResponse] The response object # @raise [JIRA::HTTPError] If the response is not an HTTP success code 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 # Returns true if the client is authenticated. # @return [Boolean] True if the client is authenticated def authenticated? @authenticated end end |
#options ⇒ Hash (readonly)
Returns The oauth connection options.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/jira/oauth_client.rb', line 16 class OauthClient < RequestClient # @private 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 # This exception is thrown when the client is used before the OAuth access token # has been initialized. class UninitializedAccessTokenError < StandardError def 'init_access_token must be called before using the client' end end extend Forwardable attr_accessor :consumer attr_reader :options def_instance_delegators :@consumer, :key, :secret, :get_request_token # Generally not used directly, but through JIRA::Client. # @param [Hash] options Options as passed from JIRA::Client constructor. # @option options [String] :signature_method The signature method to use (defaults to 'RSA-SHA1') # @option options [String] :request_token_path The path to request a token (defaults to '/plugins/servlet/oauth/request-token') # @option options [String] :authorize_path The path to authorize a token (defaults to '/plugins/servlet/oauth/authorize') # @option options [String] :access_token_path The path to access a token (defaults to '/plugins/servlet/oauth/access-token') # @option options [String] :private_key_file The path to the private key file # @option options [String] :consumer_key The OAuth 1.0 consumer key # @option options [String] :consumer_secret The OAuth 1.0 consumer secret # @option options [Hash] :default_headers Additional headers for requests # @option options [String] :proxy_uri Proxy URI # @option options [String] :proxy_user Proxy user # @option options [String] :proxy_password Proxy Password def initialize() @options = DEFAULT_OPTIONS.merge() @consumer = init_oauth_consumer(@options) end # @private # Initialises the OAuth consumer object. # Generally you should not call this method directly, it is called by the constructor. # @param [Hash] _options The options hash # @return [OAuth::Consumer] The OAuth consumer object def init_oauth_consumer() @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 # Returns the current request token if it is set, else it creates # and sets a new token. # @param [Hash] options def request_token( = {}, ...) @request_token ||= get_request_token(, ...) end # Sets the request token from a given token and secret. # @param [String] token The request token # @param [String] secret The request token secret # @return [OAuth::RequestToken] The request token object def set_request_token(token, secret) @request_token = OAuth::RequestToken.new(@consumer, token, secret) end # Initialises and returns a new access token from the params hash # returned by the OAuth transaction. # @param [Hash] params The params hash returned by the OAuth transaction # @return [OAuth::AccessToken] The access token object def init_access_token(params) @access_token = request_token.get_access_token(params) end # Sets the access token from a preexisting token and secret. # @param [String] token The access token # @param [String] secret The access token secret # @return [OAuth::AccessToken] The access token object def set_access_token(token, secret) @access_token = OAuth::AccessToken.new(@consumer, token, secret) @authenticated = true @access_token end # Returns the current access token. Raises an # JIRA::Client::UninitializedAccessTokenError exception if it is not set. # @return [OAuth::AccessToken] The access token object def access_token raise UninitializedAccessTokenError unless @access_token @access_token end # Makes a request to the JIRA server using the oauth gem. # # Generally you should not call this method directly, but use the helper methods in JIRA::Client. # # File uploads are not supported with this method. Use make_multipart_request instead. # # @param [Symbol] http_method The HTTP method to use # @param [String] url The JIRA REST URL to call # @param [String] body The body of the request # @param [Hash] headers The headers to send with the request # @return [Net::HTTPResponse] The response object # @raise [JIRA::HTTPError] If the response is not an HTTP success code 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 # Makes a multipart request to the JIRA server using the oauth gem. # # This is used for file uploads. # # Generally you should not call this method directly, but use the helper methods in JIRA::Client. # # @param [String] url The JIRA REST URL to call # @param [Hash] data The Net::HTTP::Post::Multipart data to send with the request # @param [Hash] headers The headers to send with the request # @return [Net::HTTPResponse] The response object # @raise [JIRA::HTTPError] If the response is not an HTTP success code 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 # Returns true if the client is authenticated. # @return [Boolean] True if the client is authenticated def authenticated? @authenticated end end |
Instance Method Details
#authenticated? ⇒ Boolean
Returns true if the client is authenticated.
175 176 177 |
# File 'lib/jira/oauth_client.rb', line 175 def authenticated? @authenticated end |
#init_access_token(params) ⇒ OAuth::AccessToken
Initialises and returns a new access token from the params hash returned by the OAuth transaction.
94 95 96 |
# File 'lib/jira/oauth_client.rb', line 94 def init_access_token(params) @access_token = request_token.get_access_token(params) end |
#make_multipart_request(url, data, headers = {}) ⇒ Net::HTTPResponse
Makes a multipart request to the JIRA server using the oauth gem.
This is used for file uploads.
Generally you should not call this method directly, but use the helper methods in JIRA::Client.
163 164 165 166 167 168 169 170 171 |
# File 'lib/jira/oauth_client.rb', line 163 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 = {}) ⇒ Net::HTTPResponse
Makes a request to the JIRA server using the oauth gem.
Generally you should not call this method directly, but use the helper methods in JIRA::Client.
File uploads are not supported with this method. Use make_multipart_request instead.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/jira/oauth_client.rb', line 129 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 = {}) ⇒ Object
Returns the current request token if it is set, else it creates and sets a new token.
78 79 80 |
# File 'lib/jira/oauth_client.rb', line 78 def request_token( = {}, ...) @request_token ||= get_request_token(, ...) end |
#set_access_token(token, secret) ⇒ OAuth::AccessToken
Sets the access token from a preexisting token and secret.
102 103 104 105 106 |
# File 'lib/jira/oauth_client.rb', line 102 def set_access_token(token, secret) @access_token = OAuth::AccessToken.new(@consumer, token, secret) @authenticated = true @access_token end |
#set_request_token(token, secret) ⇒ OAuth::RequestToken
Sets the request token from a given token and secret.
86 87 88 |
# File 'lib/jira/oauth_client.rb', line 86 def set_request_token(token, secret) @request_token = OAuth::RequestToken.new(@consumer, token, secret) end |