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[headers parse].freeze
- DEFAULT_EXTRACT_ACCESS_TOKEN =
proc do |client, hash| token = hash.delete('access_token') || hash.delete(:access_token) token && AccessToken.new(client, token, hash) end
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 = ) ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint.
-
#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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/oauth2/client.rb', line 35 def initialize(client_id, client_secret, = {}, &block) opts = .dup @id = client_id @secret = client_secret @site = opts.delete(:site) ssl = opts.delete(:ssl) = {:authorize_url => 'oauth/authorize', :token_url => 'oauth/token', :token_method => :post, :auth_scheme => :request_body, :connection_opts => {}, :connection_build => block, :max_redirects => 5, :raise_errors => true, :extract_access_token => DEFAULT_EXTRACT_ACCESS_TOKEN, # DEPRECATED :logger => ::Logger.new($stdout)}.merge(opts) [:connection_opts][:ssl] = ssl if ssl end |
Instance Attribute Details
#connection ⇒ Object
The Faraday connection object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/oauth2/client.rb', line 63 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.
12 13 14 |
# File 'lib/oauth2/client.rb', line 12 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
13 14 15 |
# File 'lib/oauth2/client.rb', line 13 def end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
12 13 14 |
# File 'lib/oauth2/client.rb', line 12 def secret @secret end |
#site ⇒ Object
Returns the value of attribute site.
12 13 14 |
# File 'lib/oauth2/client.rb', line 12 def site @site end |
Instance Method Details
#assertion ⇒ Object
228 229 230 |
# File 'lib/oauth2/client.rb', line 228 def assertion @assertion ||= OAuth2::Strategy::Assertion.new(self) end |
#auth_code ⇒ Object
The Authorization Code strategy
203 204 205 |
# File 'lib/oauth2/client.rb', line 203 def auth_code @auth_code ||= OAuth2::Strategy::AuthCode.new(self) end |
#authorize_url(params = {}) ⇒ Object
The authorize endpoint URL of the OAuth2 provider
79 80 81 82 |
# File 'lib/oauth2/client.rb', line 79 def (params = {}) params = (params || {}).merge(redirection_params) connection.build_url([:authorize_url], params).to_s end |
#client_credentials ⇒ Object
The Client Credentials strategy
224 225 226 |
# File 'lib/oauth2/client.rb', line 224 def client_credentials @client_credentials ||= OAuth2::Strategy::ClientCredentials.new(self) end |
#get_token(params, access_token_opts = {}, extract_access_token = ) ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint
155 156 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 194 195 196 197 198 |
# File 'lib/oauth2/client.rb', line 155 def get_token(params, access_token_opts = {}, extract_access_token = [:extract_access_token]) # # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity Metrics/AbcSize, Metrics/MethodLength params = params.map do |key, value| if RESERVED_PARAM_KEYS.include?(key) [key.to_sym, value] else [key, value] end end params = Hash[params] params = authenticator.apply(params) opts = {:raise_errors => [:raise_errors], :parse => params.delete(:parse)} headers = params.delete(:headers) || {} if [:token_method] == :post opts[:body] = params opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'} else opts[:params] = params opts[:headers] = {} end opts[:headers] = opts[:headers].merge(headers) http_method = [:token_method] response = request(http_method, token_url, opts) access_token = begin build_access_token(response, access_token_opts, extract_access_token) rescue StandardError nil end response_contains_token = access_token || ( response.parsed.is_a?(Hash) && (response.parsed['access_token'] || response.parsed['id_token']) ) if [:raise_errors] && !response_contains_token error = Error.new(response) raise(error) elsif !response_contains_token return nil end access_token end |
#implicit ⇒ Object
The Implicit strategy
210 211 212 |
# File 'lib/oauth2/client.rb', line 210 def implicit @implicit ||= OAuth2::Strategy::Implicit.new(self) end |
#password ⇒ Object
The Resource Owner Password Credentials strategy
217 218 219 |
# File 'lib/oauth2/client.rb', line 217 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.
248 249 250 251 252 253 254 |
# File 'lib/oauth2/client.rb', line 248 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.
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 |
# File 'lib/oauth2/client.rb', line 103 def request(verb, url, opts = {}) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize url = connection.build_url(url).to_s begin response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req| req.params.update(opts[:params]) if opts[:params] yield(req) if block_given? end rescue Faraday::ConnectionFailed => e raise ConnectionError, e end response = Response.new(response, :parse => opts[:parse]) 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 request(verb, 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.error = error 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
87 88 89 |
# File 'lib/oauth2/client.rb', line 87 def token_url(params = nil) connection.build_url([:token_url], params).to_s end |