Class: OAuth2::Client
- Inherits:
-
Object
- Object
- OAuth2::Client
- Defined in:
- lib/oauth2/client.rb
Overview
The OAuth2::Client class
Constant Summary collapse
- RESERVED_PARAM_KEYS =
rubocop:disable Metrics/ClassLength
%w[body headers params parse snaky].freeze
Instance Attribute Summary collapse
-
#connection ⇒ Object
The Faraday connection object.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#options ⇒ Object
Returns the value of attribute options.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#site ⇒ Object
Returns the value of attribute site.
Instance Method Summary collapse
- #assertion ⇒ Object
-
#auth_code ⇒ Object
The Authorization Code strategy.
-
#authorize_url(params = {}) ⇒ Object
The authorize endpoint URL of the OAuth2 provider.
-
#client_credentials ⇒ Object
The Client Credentials strategy.
-
#get_token(params, access_token_opts = {}, extract_access_token = nil) {|req| ... } ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint.
-
#http_method ⇒ Symbol
The HTTP Method of the request.
-
#implicit ⇒ Object
The Implicit strategy.
-
#initialize(client_id, client_secret, options = {}) {|builder| ... } ⇒ Client
constructor
Instantiate a new OAuth 2.0 client using the Client ID and Client Secret registered to your application.
-
#password ⇒ Object
The Resource Owner Password Credentials strategy.
-
#redirection_params ⇒ Hash
The redirect_uri parameters, if configured.
-
#request(verb, url, opts = {}) {|req| ... } ⇒ Object
Makes a request relative to the specified site root.
-
#token_url(params = nil) ⇒ Object
The token endpoint URL of the OAuth2 provider.
Constructor Details
#initialize(client_id, client_secret, options = {}) {|builder| ... } ⇒ Client
Instantiate a new OAuth 2.0 client using the Client ID and Client Secret registered to your application.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/oauth2/client.rb', line 38 def initialize(client_id, client_secret, = {}, &block) opts = .dup @id = client_id @secret = client_secret @site = opts.delete(:site) ssl = opts.delete(:ssl) warn('OAuth2::Client#initialize argument `extract_access_token` will be removed in oauth2 v3. Refactor to use `access_token_class`.') if opts[:extract_access_token] @options = { authorize_url: 'oauth/authorize', token_url: 'oauth/token', token_method: :post, auth_scheme: :basic_auth, connection_opts: {}, connection_build: block, max_redirects: 5, raise_errors: true, logger: ::Logger.new($stdout), access_token_class: AccessToken, }.merge(opts) @options[:connection_opts][:ssl] = ssl if ssl end |
Instance Attribute Details
#connection ⇒ Object
The Faraday connection object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/oauth2/client.rb', line 69 def connection @connection ||= Faraday.new(site, [:connection_opts]) do |builder| oauth_debug_logging(builder) if [:connection_build] [:connection_build].call(builder) else builder.request :url_encoded # form-encode POST params builder.adapter Faraday.default_adapter # make requests with Net::HTTP end end end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/oauth2/client.rb', line 14 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/oauth2/client.rb', line 15 def @options end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
14 15 16 |
# File 'lib/oauth2/client.rb', line 14 def secret @secret end |
#site ⇒ Object
Returns the value of attribute site.
14 15 16 |
# File 'lib/oauth2/client.rb', line 14 def site @site end |
Instance Method Details
#assertion ⇒ Object
232 233 234 |
# File 'lib/oauth2/client.rb', line 232 def assertion @assertion ||= OAuth2::Strategy::Assertion.new(self) end |
#auth_code ⇒ Object
The Authorization Code strategy
207 208 209 |
# File 'lib/oauth2/client.rb', line 207 def auth_code @auth_code ||= OAuth2::Strategy::AuthCode.new(self) end |
#authorize_url(params = {}) ⇒ Object
The authorize endpoint URL of the OAuth2 provider
85 86 87 88 |
# File 'lib/oauth2/client.rb', line 85 def (params = {}) params = (params || {}).merge(redirection_params) connection.build_url([:authorize_url], params).to_s end |
#client_credentials ⇒ Object
The Client Credentials strategy
228 229 230 |
# File 'lib/oauth2/client.rb', line 228 def client_credentials @client_credentials ||= OAuth2::Strategy::ClientCredentials.new(self) end |
#get_token(params, access_token_opts = {}, extract_access_token = nil) {|req| ... } ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/oauth2/client.rb', line 157 def get_token(params, access_token_opts = {}, extract_access_token = nil, &block) warn('OAuth2::Client#get_token argument `extract_access_token` will be removed in oauth2 v3. Refactor to use `access_token_class` on #initialize.') if extract_access_token extract_access_token ||= [:extract_access_token] parse, snaky, params, headers = parse_snaky_params_headers(params) request_opts = { raise_errors: [:raise_errors], parse: parse, snaky: snaky, } if [:token_method] == :post # NOTE: If proliferation of request types continues we should implement a parser solution for Request, # just like we have with Response. request_opts[:body] = if headers['Content-Type'] == 'application/json' params.to_json else params end request_opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'} else request_opts[:params] = params request_opts[:headers] = {} end request_opts[:headers].merge!(headers) response = request(http_method, token_url, request_opts, &block) # In v1.4.x, the deprecated extract_access_token option retrieves the token from the response. # We preserve this behavior here, but a custom access_token_class that implements #from_hash # should be used instead. if extract_access_token parse_response_legacy(response, access_token_opts, extract_access_token) else parse_response(response, access_token_opts) end end |
#http_method ⇒ Symbol
The HTTP Method of the request
197 198 199 200 201 202 |
# File 'lib/oauth2/client.rb', line 197 def http_method http_meth = [:token_method].to_sym return :post if http_meth == :post_with_query_string http_meth end |
#implicit ⇒ Object
The Implicit strategy
214 215 216 |
# File 'lib/oauth2/client.rb', line 214 def implicit @implicit ||= OAuth2::Strategy::Implicit.new(self) end |
#password ⇒ Object
The Resource Owner Password Credentials strategy
221 222 223 |
# File 'lib/oauth2/client.rb', line 221 def password @password ||= OAuth2::Strategy::Password.new(self) end |
#redirection_params ⇒ Hash
The redirect_uri parameters, if configured
The redirect_uri query parameter is OPTIONAL (though encouraged) when requesting authorization. If it is provided at authorization time it MUST also be provided with the token exchange request.
Providing the :redirect_uri to the OAuth2::Client instantiation will take care of managing this.
252 253 254 255 256 257 258 |
# File 'lib/oauth2/client.rb', line 252 def redirection_params if [:redirect_uri] {'redirect_uri' => [:redirect_uri]} else {} end end |
#request(verb, url, opts = {}) {|req| ... } ⇒ Object
Makes a request relative to the specified site root. Updated HTTP 1.1 specification (IETF RFC 7231) relaxed the original constraint (IETF RFC 2616),
allowing the use of relative URLs in Location headers.
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 |
# File 'lib/oauth2/client.rb', line 113 def request(verb, url, opts = {}, &block) response = execute_request(verb, url, opts, &block) case response.status when 301, 302, 303, 307 opts[:redirect_count] ||= 0 opts[:redirect_count] += 1 return response if opts[:redirect_count] > [:max_redirects] if response.status == 303 verb = :get opts.delete(:body) end location = response.headers['location'] if location full_location = response.response.env.url.merge(location) request(verb, full_location, opts) else error = Error.new(response) raise(error, "Got #{response.status} status code, but no Location header was present") end when 200..299, 300..399 # on non-redirecting 3xx statuses, just return the response response when 400..599 error = Error.new(response) raise(error) if opts.fetch(:raise_errors, [:raise_errors]) response else error = Error.new(response) raise(error, "Unhandled status code value of #{response.status}") end end |
#token_url(params = nil) ⇒ Object
The token endpoint URL of the OAuth2 provider
93 94 95 |
# File 'lib/oauth2/client.rb', line 93 def token_url(params = nil) connection.build_url([:token_url], params).to_s end |