Class: OAuth2::Client

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

Overview

The OAuth2::Client class

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(client_id, client_secret, opts = {}) {|builder| ... }

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

Parameters:

  • client_id (String)

    the client_id value

  • client_secret (String)

    the client_secret value

  • opts (Hash) (defaults to: {})

    the options to create the client with

Options Hash (opts):

  • :site (String)

    the OAuth2 provider site host

  • :authorize_url (String) — default: '/oauth/authorize'

    absolute or relative URL path to the Authorization endpoint

  • :token_url (String) — default: '/oauth/token'

    absolute or relative URL path to the Token endpoint

  • :token_method (Symbol) — default: :post

    HTTP method to use to request token (:get or :post)

  • :connection_opts (Hash) — default: {}

    Hash of connection options to pass to initialize Faraday with

  • :max_redirects (FixNum) — default: 5

    maximum number of redirects to follow

  • :raise_errors (Boolean) — default: true

    whether or not to raise an OAuth2::Error on responses with 400+ status codes

Yields:

  • (builder)

    The Faraday connection builder



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

- (Object) connection

The Faraday connection object



49
50
51
52
53
54
55
56
57
# File 'lib/oauth2/client.rb', line 49

def connection
  @connection ||= begin
    conn = Faraday.new(site, options[:connection_opts])
    conn.build do |b|
      options[:connection_build].call(b)
    end if options[:connection_build]
    conn
  end
end

- (Object) id (readonly)

Returns the value of attribute id



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

def id
  @id
end

- (Object) options

Returns the value of attribute options



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

def options
  @options
end

- (Object) secret (readonly)

Returns the value of attribute secret



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

def secret
  @secret
end

- (Object) site

Returns the value of attribute site



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

def site
  @site
end

Instance Method Details

- (Object) auth_code

The Authorization Code strategy



117
118
119
# File 'lib/oauth2/client.rb', line 117

def auth_code
  @auth_code ||= OAuth2::Strategy::AuthCode.new(self) 
end

- (Object) authorize_url(params = nil)

The authorize endpoint URL of the OAuth2 provider

Parameters:

  • params (Hash) (defaults to: nil)

    additional query parameters



62
63
64
# File 'lib/oauth2/client.rb', line 62

def authorize_url(params=nil)
  connection.build_url(options[:authorize_url], params).to_s
end

- (Object) password

The Resource Owner Password Credentials strategy



124
125
126
# File 'lib/oauth2/client.rb', line 124

def password
  @password ||= OAuth2::Strategy::Password.new(self)
end

- (Object) request(verb, url, opts = {}) {|req| ... }

Makes a request relative to the specified site root.

Parameters:

  • verb (Symbol)

    one of :get, :post, :put, :delete

  • url (String)

    URL path of request

  • opts (Hash) (defaults to: {})

    the options to make the request with

Options Hash (opts):

  • :params (Hash)

    additional query parameters for the URL of the request

  • :body (Hash, String)

    the body of the request

  • :headers (Hash)

    http request headers

  • :raise_errors (Boolean)

    whether or not to raise an OAuth2::Error on 400+ status code response for this request. Will default to client option

Yields:

  • (req)

    The Faraday request



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

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)
  
  case response.status
  when 200...299
    response
  when 300...307
    opts[:redirect_count] ||= 0
    opts[:redirect_count] += 1
    return response if opts[:redirect_count] > options[: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] || options[:raise_errors]
    response.error = e
    response
  else
    raise Error.new(response), "Unhandled status code value of #{response.status}"
  end
end

- (Object) token_url(params = nil)

The token endpoint URL of the OAuth2 provider

Parameters:

  • params (Hash) (defaults to: nil)

    additional query parameters



69
70
71
# File 'lib/oauth2/client.rb', line 69

def token_url(params=nil)
  connection.build_url(options[:token_url], params).to_s
end