Class: OAuth2::Client
- Inherits:
-
Object
- Object
- OAuth2::Client
- Defined in:
- lib/oauth2/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
-
#auth_code ⇒ Object
The Authorization Code strategy.
-
#authorize_url(params = nil) ⇒ Object
The authorize endpoint URL of the OAuth2 provider.
-
#get_token(params) ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint.
-
#initialize(client_id, client_secret, opts = {}) {|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.
-
#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, opts = {}) {|builder| ... } ⇒ Client
Instantiate a new OAuth 2.0 client using the Client ID and Client Secret registered to your application.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/oauth2/client.rb', line 25 def initialize(client_id, client_secret, opts={}, &block) @id = client_id @secret = client_secret @site = opts.delete(:site) ssl = opts.delete(:ssl) @options = {:authorize_url => '/oauth/authorize', :token_url => '/oauth/token', :token_method => :post, :connection_opts => {}, :connection_build => block, :max_redirects => 5, :raise_errors => true}.merge(opts) @options[:connection_opts][:ssl] = ssl if ssl end |
Instance Attribute Details
#connection ⇒ Object
The Faraday connection object
49 50 51 |
# File 'lib/oauth2/client.rb', line 49 def connection @connection end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/oauth2/client.rb', line 6 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/oauth2/client.rb', line 7 def @options end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
6 7 8 |
# File 'lib/oauth2/client.rb', line 6 def secret @secret end |
#site ⇒ Object
Returns the value of attribute site.
7 8 9 |
# File 'lib/oauth2/client.rb', line 7 def site @site end |
Instance Method Details
#auth_code ⇒ Object
The Authorization Code strategy
135 136 137 |
# File 'lib/oauth2/client.rb', line 135 def auth_code @auth_code ||= OAuth2::Strategy::AuthCode.new(self) end |
#authorize_url(params = nil) ⇒ Object
The authorize endpoint URL of the OAuth2 provider
62 63 64 |
# File 'lib/oauth2/client.rb', line 62 def (params=nil) connection.build_url([:authorize_url], params).to_s end |
#get_token(params) ⇒ AccessToken
Initializes an AccessToken by making a request to the token endpoint
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/oauth2/client.rb', line 119 def get_token(params) opts = {:raise_errors => true, :parse => params.delete(:parse)} if [:token_method] == :post opts[:body] = params opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'} else opts[:params] = params end response = request([:token_method], token_url, opts) raise Error.new(response) unless response.parsed.is_a?(Hash) && response.parsed['access_token'] AccessToken.from_hash(self, response.parsed) end |
#password ⇒ Object
The Resource Owner Password Credentials strategy
142 143 144 |
# File 'lib/oauth2/client.rb', line 142 def password @password ||= OAuth2::Strategy::Password.new(self) end |
#request(verb, url, opts = {}) {|req| ... } ⇒ Object
Makes a request relative to the specified site root.
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 |
# File 'lib/oauth2/client.rb', line 85 def request(verb, url, opts={}) url = self.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 200..299 response when 300..399 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 400..599 e = Error.new(response) raise e if opts[:raise_errors] || [:raise_errors] response.error = e response else raise Error.new(response), "Unhandled status code value of #{response.status}" end end |
#token_url(params = nil) ⇒ Object
The token endpoint URL of the OAuth2 provider
69 70 71 |
# File 'lib/oauth2/client.rb', line 69 def token_url(params=nil) connection.build_url([:token_url], params).to_s end |