Class: OAuth2::Client
- Inherits:
-
Object
- Object
- OAuth2::Client
- Defined in:
- lib/client.rb
Overview
The OAuth2::Client class
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
-
#authorize_url(params = nil) ⇒ Object
The authorize endpoint URL of the OAuth2 provider.
-
#client_credentials ⇒ Object
The Client Credentials strategy.
-
#get_token(params, access_token_opts = {}) ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint.
-
#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.
-
#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.
on responses with 400+ status codes
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/client.rb', line 28 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, :connection_opts => {}, :connection_build => block, :max_redirects => 5, :raise_errors => true}.merge(opts) [:connection_opts][:ssl] = ssl if ssl end |
Instance Attribute Details
#connection ⇒ Object
The Faraday connection object
53 54 55 56 57 58 59 60 61 |
# File 'lib/client.rb', line 53 def connection @connection ||= begin conn = Faraday.new(site, [:connection_opts]) conn.build do |b| [:connection_build].call(b) end if [:connection_build] conn end end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/client.rb', line 8 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/client.rb', line 9 def end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
8 9 10 |
# File 'lib/client.rb', line 8 def secret @secret end |
#site ⇒ Object
Returns the value of attribute site.
8 9 10 |
# File 'lib/client.rb', line 8 def site @site end |
Instance Method Details
#authorize_url(params = nil) ⇒ Object
The authorize endpoint URL of the OAuth2 provider
66 67 68 |
# File 'lib/client.rb', line 66 def (params = nil) connection.build_url([:authorize_url], params).to_s end |
#client_credentials ⇒ Object
The Client Credentials strategy
151 152 153 |
# File 'lib/client.rb', line 151 def client_credentials @client_credentials ||= OAuth2::Strategy::ClientCredentials.new(self) end |
#get_token(params, access_token_opts = {}) ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/client.rb', line 129 def get_token(params, access_token_opts = {}) opts = {:raise_errors => [:raise_errors], :parse => params.delete(:parse)} if [:token_method] == :post headers = params.delete(:headers) opts[:body] = params opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'} opts[:headers].merge!(headers) if headers else opts[:params] = params end response = request([:token_method], token_url, opts) error = Error.new(response) fail(error) if [:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token']) hash = response.parsed.merge(access_token_opts) AccessToken.new(self, hash.delete('access_token') || hash.delete(:access_token), hash) #AccessToken.from_hash(self, response.parsed.merge(access_token_opts)) #access_token_class.from_hash(self, response.parsed.merge(access_token_opts)) end |
#request(verb, url, opts = {}) {|req| ... } ⇒ Object
Makes a request relative to the specified site root.
code response for this request. Will default to client option
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 |
# File 'lib/client.rb', line 89 def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, MethodLength connection.response :logger, ::Logger.new($stdout) if ENV['OAUTH_DEBUG'] == 'true' url = connection.build_url(url, opts[:params]).to_s response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req| yield(req) if block_given? 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 request(verb, response.headers['location'], opts) when 200..299, 300..399 # on non-redirecting 3xx statuses, just return the response response when 400..599 error = Error.new(response) fail(error) if opts.fetch(:raise_errors, [:raise_errors]) response.error = error response else error = Error.new(response) fail(error, "Unhandled status code value of #{response.status}") end end |
#token_url(params = nil) ⇒ Object
The token endpoint URL of the OAuth2 provider
73 74 75 |
# File 'lib/client.rb', line 73 def token_url(params = nil) connection.build_url([:token_url], params).to_s end |