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

#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.

Parameters:

  • client_id (String)

    the client_id value

  • client_secret (String)

    the client_secret value

  • opts (Hash)

    the options to create the client with

Yields:

  • (builder)

    The Faraday connection builder



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

def initialize(client_id, client_secret, options = {}, &block)
  opts = options.dup
  @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

#connectionObject

The Faraday connection object



52
53
54
55
56
57
58
59
60
# File 'lib/oauth2/client.rb', line 52

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

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#secretObject (readonly)

Returns the value of attribute secret.



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

def secret
  @secret
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

#assertionObject



172
173
174
# File 'lib/oauth2/client.rb', line 172

def assertion
  @assertion ||= OAuth2::Strategy::Assertion.new(self)
end

#auth_codeObject

The Authorization Code strategy



147
148
149
# File 'lib/oauth2/client.rb', line 147

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

#authorize_url(params = nil) ⇒ Object

The authorize endpoint URL of the OAuth2 provider

Parameters:

  • params (Hash) (defaults to: nil)

    additional query parameters



65
66
67
# File 'lib/oauth2/client.rb', line 65

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

#client_credentialsObject

The Client Credentials strategy



168
169
170
# File 'lib/oauth2/client.rb', line 168

def client_credentials
  @client_credentials ||= OAuth2::Strategy::ClientCredentials.new(self)
end

#get_token(params, access_token_opts = {}, access_token_class = AccessToken) ⇒ AccessToken

Initializes an AccessToken by making a request to the token endpoint

Parameters:

  • params (Hash)

    a Hash of params for the token endpoint

  • access (Hash)

    token options, to pass to the AccessToken object

  • class (Class)

    of access token for easier subclassing OAuth2::AccessToken

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/oauth2/client.rb', line 128

def get_token(params, access_token_opts = {}, access_token_class = AccessToken)
  opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
  if options[: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(options[:token_method], token_url, opts)
  error = Error.new(response)
  fail(error) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
  access_token_class.from_hash(self, response.parsed.merge(access_token_opts))
end

#implicitObject

The Implicit strategy



154
155
156
# File 'lib/oauth2/client.rb', line 154

def implicit
  @implicit ||= OAuth2::Strategy::Implicit.new(self)
end

#passwordObject

The Resource Owner Password Credentials strategy



161
162
163
# File 'lib/oauth2/client.rb', line 161

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

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

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

  • :parse (Symbol)

    @see Response::initialize

Yields:

  • (req)

    The Faraday request



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
114
115
116
117
118
119
120
# File 'lib/oauth2/client.rb', line 88

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] > options[: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, options[: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

Parameters:

  • params (Hash) (defaults to: nil)

    additional query parameters



72
73
74
# File 'lib/oauth2/client.rb', line 72

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