Class: OAuth2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, opts = {}) ⇒ Client

Instantiate a new OAuth 2.0 client using the client ID and client secret registered to your application.

Options:

:site

Specify a base URL for your OAuth 2.0 client.

:authorize_path

Specify the path to the authorization endpoint.

:authorize_url

Specify a full URL of the authorization endpoint.

:access_token_path

Specify the path to the access token endpoint.

:access_token_method

Specify the method to use for token endpoints, can be :get or :post

(note: for Facebook this should be :get and for Google this should be :post)

:access_token_url

Specify the full URL of the access token endpoint.

:parse_json

If true, application/json responses will be automatically parsed.

:ssl

Specify SSL options for the connection.

:adapter

The name of the Faraday::Adapter::* class to use, e.g. :net_http. To pass arguments

to the adapter pass an array here, e.g. [:action_dispatch, my_test_session]

:raise_errors

Default true. When false it will then return the error status and response instead of raising an exception.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oauth2/client.rb', line 26

def initialize(client_id, client_secret, opts={})
  self.options      = opts.dup
  self.token_method = self.options.delete(:access_token_method) || :post
  adapter           = self.options.delete(:adapter)
  ssl_opts          = self.options.delete(:ssl) || {}
  connection_opts   = ssl_opts ? {:ssl => ssl_opts} : {}
  self.id           = client_id
  self.secret       = client_secret
  self.site         = self.options.delete(:site) if self.options[:site]
  self.connection   = Faraday::Connection.new(site, connection_opts)
  self.json         = self.options.delete(:parse_json)
  self.raise_errors = !(self.options.delete(:raise_errors) == false)

  if adapter && adapter != :test
    connection.build do |b|
      b.adapter(*[adapter].flatten)
    end
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def connection
  @connection
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def id
  @id
end

#json=(value) ⇒ Object (writeonly)

Sets the attribute json

Parameters:

  • value

    the value to set the attribute json to.



6
7
8
# File 'lib/oauth2/client.rb', line 6

def json=(value)
  @json = value
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def options
  @options
end

#raise_errorsObject

Returns the value of attribute raise_errors.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def raise_errors
  @raise_errors
end

#secretObject

Returns the value of attribute secret.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def secret
  @secret
end

#siteObject

Returns the value of attribute site.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def site
  @site
end

#token_methodObject

Returns the value of attribute token_method.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def token_method
  @token_method
end

Instance Method Details

#access_token_url(params = nil) ⇒ Object



51
52
53
54
# File 'lib/oauth2/client.rb', line 51

def access_token_url(params=nil)
  path = options[:access_token_url] || options[:access_token_path] || "/oauth/access_token"
  connection.build_url(path, params).to_s
end

#authorize_url(params = nil) ⇒ Object



46
47
48
49
# File 'lib/oauth2/client.rb', line 46

def authorize_url(params=nil)
  path = options[:authorize_url] || options[:authorize_path] || "/oauth/authorize"
  connection.build_url(path, params).to_s
end

#json?Boolean

Returns:

  • (Boolean)


90
# File 'lib/oauth2/client.rb', line 90

def json?; !!@json end

#passwordObject



93
# File 'lib/oauth2/client.rb', line 93

def password; OAuth2::Strategy::Password.new(self) end

#request(verb, url, params = {}, headers = {}) ⇒ Object

Makes a request relative to the specified site root.



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
# File 'lib/oauth2/client.rb', line 57

def request(verb, url, params={}, headers={})
  if (verb == :get) || (verb == :delete)
    resp = connection.run_request(verb, url, nil, headers) do |req|
      req.params.update(params)
    end
  else
    resp = connection.run_request(verb, url, params, headers)
  end

  if raise_errors
    case resp.status
      when 200...299
        return response_for(resp)
      when 302
        return request(verb, resp.headers['location'], params, headers)
      when 401
        e = OAuth2::AccessDenied.new("Received HTTP 401 during request.")
        e.response = resp
        raise e
      when 409
        e = OAuth2::Conflict.new("Received HTTP 409 during request.")
        e.response = resp
        raise e
      else
        e = OAuth2::HTTPError.new("Received HTTP #{resp.status} during request.")
        e.response = resp
        raise e
    end
  else
    response_for resp
  end
end

#web_serverObject



92
# File 'lib/oauth2/client.rb', line 92

def web_server; OAuth2::Strategy::WebServer.new(self) end