oauth2 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "michael@intridea.com"
11
11
  gem.homepage = "http://github.com/intridea/oauth2"
12
12
  gem.authors = ["Michael Bleigh"]
13
+ gem.add_dependency 'faraday', '~> 0.3.0'
13
14
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -4,7 +4,6 @@ module OAuth2
4
4
  class HTTPError < ErrorWithResponse; end
5
5
  end
6
6
 
7
- require 'oauth2/uri'
8
7
  require 'oauth2/client'
9
8
  require 'oauth2/strategy/base'
10
9
  require 'oauth2/strategy/web_server'
@@ -1,6 +1,6 @@
1
1
  module OAuth2
2
2
  class AccessToken
3
- attr_accessor :token
3
+ attr_reader :client, :token
4
4
 
5
5
  def initialize(client, token)
6
6
  @client = client
@@ -1,8 +1,13 @@
1
- require 'net/https'
1
+ require 'faraday'
2
2
 
3
3
  module OAuth2
4
4
  class Client
5
- attr_accessor :id, :secret, :site, :options
5
+ class << self
6
+ attr_accessor :default_connection_adapter
7
+ end
8
+ self.default_connection_adapter = :net_http
9
+
10
+ attr_accessor :id, :secret, :site, :connection, :options
6
11
 
7
12
  # Instantiate a new OAuth 2.0 client using the
8
13
  # client ID and client secret registered to your
@@ -16,66 +21,41 @@ module OAuth2
16
21
  # <tt>:access_token_path</tt> :: Specify the path to the access token endpoint.
17
22
  # <tt>:access_token_url</tt> :: Specify the full URL of the access token endpoint.
18
23
  def initialize(client_id, client_secret, opts = {})
19
- self.id = client_id
20
- self.secret = client_secret
21
- self.site = opts.delete(:site) if opts[:site]
22
- self.options = opts
24
+ adapter = opts.delete(:adapter) || self.class.default_connection_adapter
25
+ self.id = client_id
26
+ self.secret = client_secret
27
+ self.site = opts.delete(:site) if opts[:site]
28
+ self.options = opts
29
+ self.connection = Faraday::Connection.new(site)
30
+ if adapter != :test
31
+ connection.build { |b| b.adapter(adapter) }
32
+ end
23
33
  end
24
34
 
25
- def authorize_url
26
- return options[:authorize_url] if options[:authorize_url]
27
-
28
- uri = URI.parse(site)
29
- uri.path = options[:authorize_path] || "/oauth/authorize"
30
- uri.to_s
35
+ def authorize_url(params = nil)
36
+ path = options[:authorize_url] || options[:authorize_path] || "/oauth/authorize"
37
+ connection.build_url(path, params).to_s
31
38
  end
32
39
 
33
- def access_token_url
34
- return options[:access_token_url] if options[:access_token_url]
35
-
36
- uri = URI.parse(site)
37
- uri.path = options[:access_token_path] || "/oauth/access_token"
38
- uri.to_s
40
+ def access_token_url(params = nil)
41
+ path = options[:access_token_url] || options[:access_token_path] || "/oauth/access_token"
42
+ connection.build_url(path, params).to_s
39
43
  end
40
44
 
41
- def request(verb, url_or_path, params = {}, headers = {})
42
- if url_or_path[0..3] == 'http'
43
- uri = URI.parse(url_or_path)
44
- path = uri.path
45
- else
46
- uri = URI.parse(self.site)
47
- path = (uri.path + url_or_path).gsub('//','/')
45
+ def request(verb, url, params = {}, headers = {})
46
+ resp = connection.run_request(verb, url, nil, headers) do |req|
47
+ req.params.update(params)
48
48
  end
49
-
50
- net = Net::HTTP.new(uri.host, uri.port)
51
- net.use_ssl = (uri.scheme == 'https')
52
-
53
- net.start do |http|
54
- if verb == :get
55
- uri.query_hash = uri.query_hash.merge(params)
56
- path += "?#{uri.query}"
57
- end
58
-
59
- req = Net::HTTP.const_get(verb.to_s.capitalize).new(path, headers)
60
-
61
- unless verb == :get
62
- req.set_form_data(params)
63
- end
64
-
65
- response = http.request(req)
66
-
67
- case response
68
- when Net::HTTPSuccess
69
- response.body
70
- when Net::HTTPUnauthorized
71
- e = OAuth2::AccessDenied.new("Received HTTP 401 when retrieving access token.")
72
- e.response = response
73
- raise e
74
- else
75
- e = OAuth2::HTTPError.new("Received HTTP #{response.code} when retrieving access token.")
76
- e.response = response
77
- raise e
78
- end
49
+ case resp.status
50
+ when 200...201 then resp.body
51
+ when 401
52
+ e = OAuth2::AccessDenied.new("Received HTTP 401 when retrieving access token.")
53
+ e.response = resp
54
+ raise e
55
+ else
56
+ e = OAuth2::HTTPError.new("Received HTTP #{resp.status} when retrieving access token.")
57
+ e.response = resp
58
+ raise e
79
59
  end
80
60
  end
81
61
 
@@ -6,9 +6,7 @@ module OAuth2
6
6
  end
7
7
 
8
8
  def authorize_url(options = {}) #:nodoc:
9
- uri = URI.parse(@client.authorize_url)
10
- uri.query_hash = authorize_params(options)
11
- uri.to_s
9
+ @client.authorize_url(authorize_params(options))
12
10
  end
13
11
 
14
12
  def authorize_params(options = {}) #:nodoc:
@@ -17,9 +15,7 @@ module OAuth2
17
15
  end
18
16
 
19
17
  def access_token_url(options = {})
20
- uri = URI.parse(@client.access_token_url)
21
- uri.query_hash = access_token_params(options)
22
- uri.to_s
18
+ @client.access_token_url(access_token_params(options))
23
19
  end
24
20
 
25
21
  def access_token_params(options = {})
@@ -7,7 +7,8 @@ module OAuth2
7
7
 
8
8
  def access_token(code, options = {})
9
9
  response = @client.request(:get, @client.access_token_url, access_token_params(code, options))
10
- token = response.split('&').inject({}){|h,kv| (k,v) = kv.split('='); h[k] = v; h}['access_token']
10
+ params = Rack::Utils.parse_query(response)
11
+ token = params['access_token']
11
12
  OAuth2::AccessToken.new(@client, token)
12
13
  end
13
14
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{oauth2}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Bleigh"]
@@ -28,12 +28,11 @@ Gem::Specification.new do |s|
28
28
  "lib/oauth2/client.rb",
29
29
  "lib/oauth2/strategy/base.rb",
30
30
  "lib/oauth2/strategy/web_server.rb",
31
- "lib/oauth2/uri.rb",
32
31
  "oauth2.gemspec",
32
+ "spec/oauth2/access_token_spec.rb",
33
33
  "spec/oauth2/client_spec.rb",
34
34
  "spec/oauth2/strategy/base_spec.rb",
35
35
  "spec/oauth2/strategy/web_server_spec.rb",
36
- "spec/oauth2/uri_spec.rb",
37
36
  "spec/spec.opts",
38
37
  "spec/spec_helper.rb",
39
38
  "specs.watchr"
@@ -44,10 +43,10 @@ Gem::Specification.new do |s|
44
43
  s.rubygems_version = %q{1.3.6}
45
44
  s.summary = %q{A Ruby wrapper for the OAuth 2.0 protocol.}
46
45
  s.test_files = [
47
- "spec/oauth2/client_spec.rb",
46
+ "spec/oauth2/access_token_spec.rb",
47
+ "spec/oauth2/client_spec.rb",
48
48
  "spec/oauth2/strategy/base_spec.rb",
49
49
  "spec/oauth2/strategy/web_server_spec.rb",
50
- "spec/oauth2/uri_spec.rb",
51
50
  "spec/spec_helper.rb"
52
51
  ]
53
52
 
@@ -56,11 +55,14 @@ Gem::Specification.new do |s|
56
55
  s.specification_version = 3
57
56
 
58
57
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58
+ s.add_runtime_dependency(%q<faraday>, ["~> 0.3.0"])
59
59
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
60
60
  else
61
+ s.add_dependency(%q<faraday>, ["~> 0.3.0"])
61
62
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
63
  end
63
64
  else
65
+ s.add_dependency(%q<faraday>, ["~> 0.3.0"])
64
66
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
65
67
  end
66
68
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe OAuth2::AccessToken do
4
+ let(:client) do
5
+ cli = OAuth2::Client.new('abc','def', :site => 'https://api.example.com')
6
+ cli.connection.build do |b|
7
+ b.adapter :test do |stub|
8
+ stub.get('/client?access_token=monkey') { |env| [200, {}, 'get'] }
9
+ stub.post('/client?access_token=monkey') { |env| [200, {}, 'post'] }
10
+ stub.put('/client?access_token=monkey') { |env| [200, {}, 'put'] }
11
+ stub.delete('/client?access_token=monkey') { |env| [200, {}, 'delete'] }
12
+ end
13
+ end
14
+ cli
15
+ end
16
+
17
+ let(:token) { 'monkey' }
18
+
19
+ subject { OAuth2::AccessToken.new(client, token) }
20
+
21
+ describe '#initialize' do
22
+ it 'should assign client and token' do
23
+ subject.client.should == client
24
+ subject.token.should == token
25
+ end
26
+
27
+ %w(get post put delete).each do |http_method|
28
+ it "makes #{http_method.upcase} requests with access token" do
29
+ subject.send(http_method.to_sym, 'client').should == http_method
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,7 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe OAuth2::Client do
4
- subject{ OAuth2::Client.new('abc','def', :site => 'https://api.example.com')}
4
+ subject do
5
+ cli = OAuth2::Client.new('abc','def', :site => 'https://api.example.com')
6
+ cli.connection.build do |b|
7
+ b.adapter :test do |stub|
8
+ stub.get('/success') { |env| [200, {}, 'yay'] }
9
+ stub.get('/unauthorized') { |env| [401, {}, ''] }
10
+ stub.get('/error') { |env| [500, {}, ''] }
11
+ end
12
+ end
13
+ cli
14
+ end
5
15
 
6
16
  describe '#initialize' do
7
17
  it 'should assign id and secret' do
@@ -12,6 +22,10 @@ describe OAuth2::Client do
12
22
  it 'should assign site from the options hash' do
13
23
  subject.site.should == 'https://api.example.com'
14
24
  end
25
+
26
+ it 'should assign Faraday::Connection#host' do
27
+ subject.connection.host.should == 'api.example.com'
28
+ end
15
29
  end
16
30
 
17
31
  %w(authorize access_token).each do |path_type|
@@ -31,7 +45,21 @@ describe OAuth2::Client do
31
45
  end
32
46
  end
33
47
  end
34
-
48
+
49
+ describe "#request" do
50
+ it "returns response body on successful response" do
51
+ subject.request(:get, '/success', {}, {}).should == 'yay'
52
+ end
53
+
54
+ it "raises OAuth2::AccessDenied on 401 response" do
55
+ lambda { subject.request(:get, '/unauthorized', {}, {}) }.should raise_error(OAuth2::AccessDenied)
56
+ end
57
+
58
+ it "raises OAuth2::HTTPError on error response" do
59
+ lambda { subject.request(:get, '/error', {}, {}) }.should raise_error(OAuth2::HTTPError)
60
+ end
61
+ end
62
+
35
63
  it '#web_server should instantiate a WebServer strategy with this client' do
36
64
  subject.web_server.should be_kind_of(OAuth2::Strategy::WebServer)
37
65
  end
@@ -1,8 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe OAuth2::Strategy::WebServer do
4
- let(:client){ OAuth2::Client.new('abc','def', :site => 'http://api.example.com') }
4
+ let(:client) do
5
+ cli = OAuth2::Client.new('abc','def', :site => 'http://api.example.com')
6
+ cli.connection.build do |b|
7
+ b.adapter :test do |stub|
8
+ stub.get('/oauth/access_token?code=sushi&client_id=abc&client_secret=def&type=web_server') do |env|
9
+ [200, {}, 'a=1&access_token=salmon']
10
+ end
11
+ end
12
+ end
13
+ cli
14
+ end
5
15
  subject { client.web_server }
16
+
6
17
  describe '#authorize_url' do
7
18
  it 'should include the client_id' do
8
19
  subject.authorize_url.should be_include('client_id=abc')
@@ -14,7 +25,21 @@ describe OAuth2::Strategy::WebServer do
14
25
 
15
26
  it 'should include passed in options' do
16
27
  cb = 'http://myserver.local/oauth/callback'
17
- subject.authorize_url(:redirect_uri => cb).should be_include("redirect_uri=#{CGI.escape(cb)}")
28
+ subject.authorize_url(:redirect_uri => cb).should be_include("redirect_uri=#{Rack::Utils.escape(cb)}")
29
+ end
30
+ end
31
+
32
+ describe "#access_token" do
33
+ before do
34
+ @access = subject.access_token('sushi')
35
+ end
36
+
37
+ it 'returns AccessToken with same Client' do
38
+ @access.client.should == client
39
+ end
40
+
41
+ it 'returns AccessToken with #token' do
42
+ @access.token.should == 'salmon'
18
43
  end
19
44
  end
20
45
  end
@@ -1,9 +1,12 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
3
4
  require 'oauth2'
4
5
  require 'spec'
5
6
  require 'spec/autorun'
6
7
 
8
+ OAuth2::Client.default_connection_adapter = :test
9
+
7
10
  Spec::Runner.configure do |config|
8
11
 
9
12
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Bleigh
@@ -18,9 +18,23 @@ date: 2010-04-22 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: faraday
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 3
30
+ - 0
31
+ version: 0.3.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
24
38
  requirements:
25
39
  - - ">="
26
40
  - !ruby/object:Gem::Version
@@ -30,7 +44,7 @@ dependencies:
30
44
  - 9
31
45
  version: 1.2.9
32
46
  type: :development
33
- version_requirements: *id001
47
+ version_requirements: *id002
34
48
  description: A Ruby wrapper for the OAuth 2.0 protocol built with a similar style to the original OAuth gem.
35
49
  email: michael@intridea.com
36
50
  executables: []
@@ -52,12 +66,11 @@ files:
52
66
  - lib/oauth2/client.rb
53
67
  - lib/oauth2/strategy/base.rb
54
68
  - lib/oauth2/strategy/web_server.rb
55
- - lib/oauth2/uri.rb
56
69
  - oauth2.gemspec
70
+ - spec/oauth2/access_token_spec.rb
57
71
  - spec/oauth2/client_spec.rb
58
72
  - spec/oauth2/strategy/base_spec.rb
59
73
  - spec/oauth2/strategy/web_server_spec.rb
60
- - spec/oauth2/uri_spec.rb
61
74
  - spec/spec.opts
62
75
  - spec/spec_helper.rb
63
76
  - specs.watchr
@@ -92,8 +105,8 @@ signing_key:
92
105
  specification_version: 3
93
106
  summary: A Ruby wrapper for the OAuth 2.0 protocol.
94
107
  test_files:
108
+ - spec/oauth2/access_token_spec.rb
95
109
  - spec/oauth2/client_spec.rb
96
110
  - spec/oauth2/strategy/base_spec.rb
97
111
  - spec/oauth2/strategy/web_server_spec.rb
98
- - spec/oauth2/uri_spec.rb
99
112
  - spec/spec_helper.rb
@@ -1,14 +0,0 @@
1
- require 'uri'
2
- require 'cgi'
3
-
4
- module URI
5
- class Generic
6
- def query_hash
7
- CGI.parse(self.query || '').inject({}){|hash, (k,v)| hash[k] = (v.size == 1 ? v.first : v); hash}
8
- end
9
-
10
- def query_hash=(hash)
11
- self.query = hash.map{|(k,v)| "#{k}=#{CGI.escape(v)}"}.join('&')
12
- end
13
- end
14
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe URI::Generic do
4
- subject{ URI.parse('http://example.com')}
5
-
6
- describe '#query_hash' do
7
- it 'should be a hash of the query parameters' do
8
- subject.query_hash.should == {}
9
- subject.query = 'abc=def&foo=123'
10
- subject.query_hash.should == {'abc' => 'def', 'foo' => '123'}
11
- end
12
- end
13
-
14
- describe '#query_hash=' do
15
- it 'should set the query' do
16
- subject.query_hash = {'abc' => 'def'}
17
- subject.query.should == 'abc=def'
18
- subject.query_hash = {'abc' => 'foo', 'bar' => 'baz'}
19
- subject.query.should be_include('abc=foo')
20
- subject.query.should be_include('bar=baz')
21
- subject.query.split('&').size.should == 2
22
- end
23
-
24
- it 'should escape stuff' do
25
- subject.query_hash = {'abc' => '$%!!'}
26
- subject.query.should == "abc=#{CGI.escape('$%!!')}"
27
- end
28
- end
29
- end