oauth2 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,13 +1,16 @@
1
1
  # OAuth2
2
+
2
3
  [![Gem Version](https://badge.fury.io/rb/oauth2.png)][gem]
3
4
  [![Build Status](https://secure.travis-ci.org/intridea/oauth2.png?branch=master)][travis]
4
5
  [![Dependency Status](https://gemnasium.com/intridea/oauth2.png?travis)][gemnasium]
5
6
  [![Code Climate](https://codeclimate.com/github/intridea/oauth2.png)][codeclimate]
7
+ [![Coverage Status](https://coveralls.io/repos/intridea/oauth2/badge.png?branch=master)][coveralls]
6
8
 
7
9
  [gem]: https://rubygems.org/gems/oauth2
8
10
  [travis]: http://travis-ci.org/intridea/oauth2
9
11
  [gemnasium]: https://gemnasium.com/intridea/oauth2
10
12
  [codeclimate]: https://codeclimate.com/github/intridea/oauth2
13
+ [coveralls]: https://coveralls.io/r/intridea/oauth2
11
14
 
12
15
  A Ruby wrapper for the OAuth 2.0 specification. This is a work in progress,
13
16
  being built first to solve the pragmatic process of connecting to existing
@@ -21,7 +24,7 @@ To ensure the code you're installing hasn't been tampered with, it's
21
24
  recommended that you verify the signature. To do this, you need to add my
22
25
  public key as a trusted certificate (you only need to do this once):
23
26
 
24
- gem cert --add <(curl -Ls https://gist.github.com/sferik/4701180/raw/public_cert.pem)
27
+ gem cert --add <(curl -Ls https://raw.github.com/intridea/oauth2/master/certs/sferik.pem)
25
28
 
26
29
  Then, install the gem with the high security trust policy:
27
30
 
@@ -37,17 +40,19 @@ Then, install the gem with the high security trust policy:
37
40
  [wiki]: https://wiki.github.com/intridea/oauth2
38
41
 
39
42
  ## Usage Examples
40
- require 'oauth2'
41
- client = OAuth2::Client.new('client_id', 'client_secret', :site => 'https://example.org')
42
43
 
43
- client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth2/callback')
44
- # => "https://example.org/oauth/authorization?response_type=code&client_id=client_id&redirect_uri=http://localhost:8080/oauth2/callback"
44
+ ```ruby
45
+ require 'oauth2'
46
+ client = OAuth2::Client.new('client_id', 'client_secret', :site => 'https://example.org')
45
47
 
46
- token = client.auth_code.get_token('authorization_code_value', :redirect_uri => 'http://localhost:8080/oauth2/callback', :headers => {'Authorization' => 'Basic some_password'})
47
- response = token.get('/api/resource', :params => { 'query_foo' => 'bar' })
48
- response.class.name
49
- # => OAuth2::Response
48
+ client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth2/callback')
49
+ # => "https://example.org/oauth/authorization?response_type=code&client_id=client_id&redirect_uri=http://localhost:8080/oauth2/callback"
50
50
 
51
+ token = client.auth_code.get_token('authorization_code_value', :redirect_uri => 'http://localhost:8080/oauth2/callback', :headers => {'Authorization' => 'Basic some_password'})
52
+ response = token.get('/api/resource', :params => { 'query_foo' => 'bar' })
53
+ response.class.name
54
+ # => OAuth2::Response
55
+ ```
51
56
  ## OAuth2::Response
52
57
  The AccessToken methods #get, #post, #put and #delete and the generic #request
53
58
  will return an instance of the #OAuth2::Response class.
@@ -82,23 +87,27 @@ Currently the Authorization Code, Implicit, Resource Owner Password Credentials,
82
87
  authentication grant types have helper strategy classes that simplify client
83
88
  use. They are available via the #auth_code, #implicit, #password, #client_credentials, and #assertion methods respectively.
84
89
 
85
- auth_url = client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
86
- token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback')
90
+ ```ruby
91
+ auth_url = client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
92
+ token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback')
87
93
 
88
- auth_url = client.implicit.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
89
- # get the token params in the callback and
90
- token = OAuth2::AccessToken.from_kvform(client, query_string)
94
+ auth_url = client.implicit.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
95
+ # get the token params in the callback and
96
+ token = OAuth2::AccessToken.from_kvform(client, query_string)
91
97
 
92
- token = client.password.get_token('username', 'password')
98
+ token = client.password.get_token('username', 'password')
93
99
 
94
- token = client.client_credentials.get_token
100
+ token = client.client_credentials.get_token
95
101
 
96
- token = client.assertion.get_token(assertion_params)
102
+ token = client.assertion.get_token(assertion_params)
103
+ ```
97
104
 
98
105
  If you want to specify additional headers to be sent out with the
99
106
  request, add a 'headers' hash under 'params':
100
107
 
101
- token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback', :headers => {'Some' => 'Header'})
108
+ ```ruby
109
+ token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback', :headers => {'Some' => 'Header'})
110
+ ```
102
111
 
103
112
  You can always use the #request method on the OAuth2::Client instance to make
104
113
  requests for tokens for any Authentication grant type.
@@ -89,6 +89,13 @@ module OAuth2
89
89
  new_token
90
90
  end
91
91
 
92
+ # Convert AccessToken to a hash which can be used to rebuild itself with AccessToken.from_hash
93
+ #
94
+ # @return [Hash] a hash of AccessToken property values
95
+ def to_hash
96
+ params.merge({:access_token => token, :refresh_token => refresh_token, :expires_at => expires_at})
97
+ end
98
+
92
99
  # Make a request with the Access Token
93
100
  #
94
101
  # @param [Symbol] verb the HTTP request method
@@ -24,17 +24,18 @@ module OAuth2
24
24
  # on responses with 400+ status codes
25
25
  # @yield [builder] The Faraday connection builder
26
26
  def initialize(client_id, client_secret, opts={}, &block)
27
+ _opts = opts.dup
27
28
  @id = client_id
28
29
  @secret = client_secret
29
- @site = opts.delete(:site)
30
- ssl = opts.delete(:ssl)
30
+ @site = _opts.delete(:site)
31
+ ssl = _opts.delete(:ssl)
31
32
  @options = {:authorize_url => '/oauth/authorize',
32
33
  :token_url => '/oauth/token',
33
34
  :token_method => :post,
34
35
  :connection_opts => {},
35
36
  :connection_build => block,
36
37
  :max_redirects => 5,
37
- :raise_errors => true}.merge(opts)
38
+ :raise_errors => true}.merge(_opts)
38
39
  @options[:connection_opts][:ssl] = ssl if ssl
39
40
  end
40
41
 
@@ -106,7 +107,7 @@ module OAuth2
106
107
  response
107
108
  when 400..599
108
109
  e = Error.new(response)
109
- raise e if opts[:raise_errors] || options[:raise_errors]
110
+ raise e if opts.fetch(:raise_errors, options[:raise_errors])
110
111
  response.error = e
111
112
  response
112
113
  else
@@ -118,8 +119,9 @@ module OAuth2
118
119
  #
119
120
  # @param [Hash] params a Hash of params for the token endpoint
120
121
  # @param [Hash] access token options, to pass to the AccessToken object
122
+ # @param [Class] class of access token for easier subclassing OAuth2::AccessToken
121
123
  # @return [AccessToken] the initalized AccessToken
122
- def get_token(params, access_token_opts={})
124
+ def get_token(params, access_token_opts={}, access_token_class = AccessToken)
123
125
  opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
124
126
  if options[:token_method] == :post
125
127
  headers = params.delete(:headers)
@@ -131,7 +133,7 @@ module OAuth2
131
133
  end
132
134
  response = request(options[:token_method], token_url, opts)
133
135
  raise Error.new(response) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
134
- AccessToken.from_hash(self, response.parsed.merge(access_token_opts))
136
+ access_token_class.from_hash(self, response.parsed.merge(access_token_opts))
135
137
  end
136
138
 
137
139
  # The Authorization Code strategy
@@ -50,7 +50,7 @@ module OAuth2
50
50
 
51
51
  def build_request(params)
52
52
  assertion = build_assertion(params)
53
- {:grant_type => "assertion",
53
+ {:grant_type => "assertion",
54
54
  :assertion_type => "urn:ietf:params:oauth:grant-type:jwt-bearer",
55
55
  :assertion => assertion,
56
56
  :scope => params[:scope]
@@ -2,7 +2,7 @@ module OAuth2
2
2
  class Version
3
3
  MAJOR = 0 unless defined? MAJOR
4
4
  MINOR = 9 unless defined? MINOR
5
- PATCH = 1 unless defined? PATCH
5
+ PATCH = 2 unless defined? PATCH
6
6
  PRE = nil unless defined? PRE
7
7
 
8
8
  class << self
@@ -6,7 +6,7 @@ require 'oauth2/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.add_development_dependency 'bundler', '~> 1.0'
8
8
  spec.add_dependency 'faraday', '~> 0.8'
9
- spec.add_dependency 'httpauth', '~> 0.1'
9
+ spec.add_dependency 'httpauth', '~> 0.2'
10
10
  spec.add_dependency 'multi_json', '~> 1.0'
11
11
  spec.add_dependency 'multi_xml', '~> 0.5'
12
12
  spec.add_dependency 'rack', '~> 1.2'
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.licenses = ['MIT']
23
23
  spec.name = 'oauth2'
24
24
  spec.require_paths = ['lib']
25
- spec.required_rubygems_version = '>= 1.3.6'
25
+ spec.required_rubygems_version = '>= 1.3.5'
26
26
  spec.signing_key = File.expand_path("~/.gem/private_key.pem") if $0 =~ /gem\z/
27
27
  spec.summary = %q{A Ruby wrapper for the OAuth 2.0 protocol.}
28
28
  spec.test_files = Dir.glob("spec/**/*")
@@ -1,9 +1,11 @@
1
- unless ENV['CI']
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter 'spec'
5
- end
6
- end
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
7
9
 
8
10
  require 'oauth2'
9
11
  require 'addressable/uri'
@@ -59,7 +59,7 @@ describe AccessToken do
59
59
  expect(target.options[:header_format]).to eq('Bearer %')
60
60
  expect(target.options[:mode]).to eq(:body)
61
61
  end
62
-
62
+
63
63
  it "initializes with a string expires_at" do
64
64
  hash = {:access_token => token, :expires_at => '1361396829', 'foo' => 'bar'}
65
65
  target = AccessToken.from_hash(client, hash)
@@ -70,7 +70,7 @@ describe AccessToken do
70
70
 
71
71
  describe "#request" do
72
72
  context ":mode => :header" do
73
- before :all do
73
+ before do
74
74
  subject.options[:mode] = :header
75
75
  end
76
76
 
@@ -82,7 +82,7 @@ describe AccessToken do
82
82
  end
83
83
 
84
84
  context ":mode => :query" do
85
- before :all do
85
+ before do
86
86
  subject.options[:mode] = :query
87
87
  end
88
88
 
@@ -94,7 +94,7 @@ describe AccessToken do
94
94
  end
95
95
 
96
96
  context ":mode => :body" do
97
- before :all do
97
+ before do
98
98
  subject.options[:mode] = :body
99
99
  end
100
100
 
@@ -161,4 +161,12 @@ describe AccessToken do
161
161
  end
162
162
  end
163
163
  end
164
+
165
+ describe '#to_hash' do
166
+ it 'return a hash equals to the hash used to initialize access token' do
167
+ hash = {:access_token => token, :refresh_token => 'foobar', :expires_at => Time.now.to_i + 200, 'foo' => 'bar'}
168
+ access_token = AccessToken.from_hash(client, hash.clone)
169
+ expect(access_token.to_hash).to eq(hash)
170
+ end
171
+ end
164
172
  end
@@ -63,12 +63,31 @@ describe OAuth2::Client do
63
63
  expect(client.options[:raise_errors]).to be_true
64
64
  end
65
65
 
66
+ it "allows override of raise_errors option" do
67
+ client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :raise_errors => true) do |builder|
68
+ builder.adapter :test do |stub|
69
+ stub.get('/notfound') {|env| [404, {}, nil]}
70
+ end
71
+ end
72
+ expect(client.options[:raise_errors]).to be_true
73
+ expect{client.request(:get, '/notfound')}.to raise_error(OAuth2::Error)
74
+ response = client.request(:get, '/notfound', :raise_errors => false)
75
+ expect(response.status).to eq(404)
76
+ end
77
+
66
78
  it "allows get/post for access_token_method option" do
67
79
  client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :access_token_method => :get)
68
80
  expect(client.options[:access_token_method]).to eq(:get)
69
81
  client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :access_token_method => :post)
70
82
  expect(client.options[:access_token_method]).to eq(:post)
71
83
  end
84
+
85
+ it 'does not mutate the opts hash argument' do
86
+ opts = { site: 'http://example.com/' }
87
+ opts2 = opts.dup
88
+ OAuth2::Client.new 'abc', 'def', opts
89
+ expect(opts).to eq(opts2)
90
+ end
72
91
  end
73
92
 
74
93
  %w(authorize token).each do |url_type|
@@ -53,7 +53,7 @@ describe OAuth2::Strategy::AuthCode do
53
53
  %w(json formencoded from_facebook).each do |mode|
54
54
  [:get, :post].each do |verb|
55
55
  describe "#get_token (#{mode}, access_token_method=#{verb}" do
56
- before :each do
56
+ before do
57
57
  @mode = mode
58
58
  client.options[:token_method] = verb
59
59
  @access = subject.get_token(code)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -37,7 +37,7 @@ cert_chain:
37
37
  U0xxV3ZRUnNCbHlwSGZoczZKSnVMbHlaUEdoVTNSL3YKU2YzbFZLcEJDV2dS
38
38
  cEdUdnk0NVhWcEIrNTl5MzNQSm1FdVExUFRFT1l2UXlhbzlVS01BQWFBTi83
39
39
  cVdRdGpsMApobHc9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
40
- date: 2013-02-20 00:00:00.000000000 Z
40
+ date: 2013-06-30 00:00:00.000000000 Z
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: bundler
@@ -78,7 +78,7 @@ dependencies:
78
78
  requirements:
79
79
  - - ~>
80
80
  - !ruby/object:Gem::Version
81
- version: '0.1'
81
+ version: '0.2'
82
82
  type: :runtime
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
@@ -86,7 +86,7 @@ dependencies:
86
86
  requirements:
87
87
  - - ~>
88
88
  - !ruby/object:Gem::Version
89
- version: '0.1'
89
+ version: '0.2'
90
90
  - !ruby/object:Gem::Dependency
91
91
  name: multi_json
92
92
  requirement: !ruby/object:Gem::Requirement
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  requirements:
207
207
  - - ! '>='
208
208
  - !ruby/object:Gem::Version
209
- version: 1.3.6
209
+ version: 1.3.5
210
210
  requirements: []
211
211
  rubyforge_project:
212
212
  rubygems_version: 1.8.23
metadata.gz.sig CHANGED
Binary file