-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to generate the authorisation URL from the client
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |