Skip to content

Commit 200b109

Browse files
authored
Merge pull request #162 from AgoraSecurity/add_authorize
Implement authenticate_url
2 parents 4cb652e + c7e95ca commit 200b109

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

lib/oauth/consumer.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Consumer
2323

2424
# default paths on site. These are the same as the defaults set up by the generators
2525
:request_token_path => '/oauth/request_token',
26+
:authenticate_path => '/oauth/authenticate',
2627
:authorize_path => '/oauth/authorize',
2728
:access_token_path => '/oauth/access_token',
2829

@@ -266,6 +267,10 @@ def request_token_path
266267
@options[:request_token_path]
267268
end
268269

270+
def authenticate_path
271+
@options[:authenticate_path]
272+
end
273+
269274
def authorize_path
270275
@options[:authorize_path]
271276
end
@@ -283,6 +288,14 @@ def request_token_url?
283288
@options.has_key?(:request_token_url)
284289
end
285290

291+
def authenticate_url
292+
@options[:authenticate_url] || site + authenticate_path
293+
end
294+
295+
def authenticate_url?
296+
@options.has_key?(:authenticate_url)
297+
end
298+
286299
def authorize_url
287300
@options[:authorize_url] || site + authorize_path
288301
end

lib/oauth/tokens/request_token.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ def authorize_url(params = nil)
88
return nil if self.token.nil?
99

1010
params = (params || {}).merge(:oauth_token => self.token)
11-
build_authorize_url(consumer.authorize_url, params)
11+
build_url(consumer.authorize_url, params)
12+
end
13+
14+
def authenticate_url(params = nil)
15+
return nil if self.token.nil?
16+
17+
params = (params || {}).merge(:oauth_token => self.token)
18+
build_url(consumer.authenticate_url, params)
1219
end
1320

1421
def callback_confirmed?
@@ -23,8 +30,8 @@ def get_access_token(options = {}, *arguments)
2330

2431
protected
2532

26-
# construct an authorization url
27-
def build_authorize_url(base_url, params)
33+
# construct an authorization or authentication url
34+
def build_url(base_url, params)
2835
uri = URI.parse(base_url.to_s)
2936
queries = {}
3037
queries = Hash[URI.decode_www_form(uri.query)] if uri.query

lib/oauth/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module OAuth
2-
VERSION = "0.5.4"
2+
VERSION = "0.5.5"
33
end

test/units/test_request_token.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require File.expand_path('../../test_helper', __FILE__)
22

33
class StubbedToken < OAuth::RequestToken
4-
define_method :build_authorize_url_promoted do |root_domain, params|
5-
build_authorize_url root_domain, params
4+
define_method :build_url_promoted do |root_domain, params|
5+
build_url root_domain, params
66
end
77
end
88

@@ -40,14 +40,38 @@ def test_request_token_returns_nil_authorize_url_when_token_is_nil
4040
assert_nil @request_token.authorize_url
4141
end
4242

43+
def test_request_token_builds_authenticate_url_connectly_with_additional_params
44+
authenticate_url = @request_token.authenticate_url({:oauth_callback => "github.com"})
45+
assert authenticate_url
46+
assert_match(/oauth_token/, authenticate_url)
47+
assert_match(/oauth_callback/, authenticate_url)
48+
end
49+
50+
def test_request_token_builds_authenticate_url_connectly_with_no_or_nil_params
51+
# we should only have 1 key in the url returned if we didn't pass anything.
52+
# this is the only required param to authenticate the client.
53+
authenticate_url = @request_token.authenticate_url(nil)
54+
assert authenticate_url
55+
assert_match(/\?oauth_token=/, authenticate_url)
56+
57+
authenticate_url2 = @request_token.authenticate_url
58+
assert authenticate_url2
59+
assert_match(/\?oauth_token=/, authenticate_url2)
60+
end
61+
62+
def test_request_token_returns_nil_authenticate_url_when_token_is_nil
63+
@request_token.token = nil
64+
assert_nil @request_token.authenticate_url
65+
end
66+
4367
#TODO: mock out the Consumer to test the Consumer/AccessToken interaction.
4468
def test_get_access_token
4569
end
4670

47-
def test_build_authorize_url
71+
def test_build_url
4872
@stubbed_token = StubbedToken.new(nil, nil, nil)
49-
assert_respond_to @stubbed_token, :build_authorize_url_promoted
50-
url = @stubbed_token.build_authorize_url_promoted(
73+
assert_respond_to @stubbed_token, :build_url_promoted
74+
url = @stubbed_token.build_url_promoted(
5175
"http://github.com/oauth/authorize",
5276
{:foo => "bar bar"})
5377
assert url

0 commit comments

Comments
 (0)