Module: Dnsimple::Client::Oauth

Included in:
OauthService
Defined in:
lib/dnsimple/client/oauth.rb

Instance Method Summary collapse

Instance Method Details

#authorize_url(client_id, options = {}) ⇒ String

Gets the URL to authorize an user for an application via the OAuth2 flow.

Parameters:

  • client_id (String)

    Client Id you received when the application was registered with DNSimple.

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

    a customizable set of options

Options Hash (options):

  • :redirect_uri (String)

    The URL to redirect to after authorizing.

  • :scope (String)

    The scopes to request from the user.

  • :state (String)

    A random string to protect against CSRF.

Returns:

  • (String)

    The URL to redirect the user to authorize.

See Also:



33
34
35
36
37
38
39
40
41
42
# File 'lib/dnsimple/client/oauth.rb', line 33

def authorize_url(client_id, options = {})
  site_url = client.base_url.sub("api.", "")
  url = URI.join(site_url, "/oauth/authorize?client_id=#{client_id}")

  options = options.merge(response_type: "code")
  options.each do |key, value|
    url.query += "&#{key}=#{value}"
  end
  url.to_s
end

#exchange_authorization_for_token(code, client_id, client_secret, options = {}) ⇒ String

Exchange the short-lived authorization code for an access token you can use to authenticate your API calls.

Parameters:

  • client_id (String)

    Client Id you received when the application was registered with DNSimple.

  • client_secret (String)

    Client Secret you received when the application was registered with DNSimple.

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

    a customizable set of options

Options Hash (options):

  • :redirect_uri (String)

    The redirect URL sent for the authorization, used to validate the request.

Returns:

  • (String)

    The url to redirect the user to authorize.

See Also:



16
17
18
19
20
21
22
# File 'lib/dnsimple/client/oauth.rb', line 16

def exchange_authorization_for_token(code, client_id, client_secret, options = {})
  attributes = { code: code, client_id: client_id, client_secret: client_secret, grant_type: "authorization_code" }
  attributes[:state] = options.delete(:state) if options.key?(:state)
  attributes[:redirect_uri] = options.delete(:redirect_uri) if options.key?(:redirect_uri)
  response = client.post(Client.versioned("/oauth/access_token"), attributes, options)
  Struct::OauthToken.new(response)
end