Skip to content

Commit

Permalink
Add ability to generate the authorisation URL from the client
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Feb 2, 2016
1 parent fa64b5f commit 7761ef1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/dnsimple/client/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def domains
@services[:domains] ||= Client::DomainsService.new(self)
end

# @return [Dnsimple::Client::OauthService] The oauth-related API proxy.
def oauth
@services[:oauth] ||= Client::OauthService.new(self)
end

# @return [Dnsimple::Client::RegistrarService] The registrar-related API proxy.
def registrar
@services[:registrar] ||= Client::RegistrarService.new(self)
Expand Down Expand Up @@ -79,6 +84,13 @@ class DomainsService < ClientService
end


require_relative 'oauth'

class OauthService < ClientService
include Client::Oauth
end


require_relative 'registrar'

class RegistrarService < ClientService
Expand Down
25 changes: 25 additions & 0 deletions lib/dnsimple/client/oauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Dnsimple
class Client
module Oauth

# Gets the URL to authorize an user for an application via the OAuth2 flow.
#
# @see https://developer.dnsimple.com/v2/oauth/
#
# @param client_id [String] Client Id you received when the application was registered with DNSimple.
# @option options [String] :redirect_uri The url to redirect to after authorizing.
# @option options [String] :scope The scopes to request from the user.
# @option options [String] :state A random string to protect against CSRF.
# @return [String] The url to redirect the user to authorize.
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.each do |key, value|
url.query += "&#{key}=#{value}"
end
url.to_s
end

end
end
end
20 changes: 20 additions & 0 deletions spec/dnsimple/client/oauth_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe Dnsimple::Client, ".oauth" do

subject { described_class.new(base_url: "https://api.dnsimple.test", access_token: "a1b2c3").oauth }


describe "#authorize_url" do
it "builds the correct url" do
url = subject.authorize_url("great-app")
expect(url).to eq("https://dnsimple.test/oauth/authorize?client_id=great-app")
end

it "exposes the options in the query string" do
url = subject.authorize_url("great-app", secret: "1", redirect_uri: "http://example.com")
expect(url).to eq("https://dnsimple.test/oauth/authorize?client_id=great-app&secret=1&redirect_uri=http://example.com")
end
end

end

0 comments on commit 7761ef1

Please sign in to comment.