oauth2-client 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +10 -0
- data/.travis.yml +1 -0
- data/Gemfile +7 -12
- data/Gemfile.lock +17 -9
- data/{LICENSE → LICENSE.md} +1 -1
- data/README.md +109 -19
- data/examples/github_client.rb +42 -0
- data/examples/google_client.rb +17 -15
- data/examples/yammer_client.rb +96 -0
- data/lib/oauth2/client.rb +12 -0
- data/lib/oauth2/connection.rb +8 -12
- data/lib/oauth2/grant/authorization_code.rb +7 -8
- data/lib/oauth2/version.rb +1 -1
- data/oauth2-client.gemspec +23 -13
- data/spec/examples/github_client_spec.rb +59 -0
- data/spec/examples/google_client_spec.rb +54 -78
- data/spec/examples/yammer_client_spec.rb +75 -0
- data/spec/oauth2/client_spec.rb +4 -4
- data/spec/oauth2/connection_spec.rb +56 -43
- data/spec/oauth2/grant/authorization_code_spec.rb +7 -2
- data/spec/oauth2/grant/client_credentials_spec.rb +4 -1
- data/spec/oauth2/grant/device_spec.rb +7 -2
- data/spec/oauth2/grant/implicit_spec.rb +4 -1
- data/spec/oauth2/grant/password_spec.rb +4 -1
- data/spec/oauth2/grant/refresh_token_spec.rb +4 -1
- data/spec/spec_helper.rb +18 -1
- metadata +73 -18
- data/TODO +0 -10
- data/spec/.DS_Store +0 -0
- data/spec/mocks/oauth_client.yml +0 -60
data/spec/oauth2/client_spec.rb
CHANGED
@@ -147,10 +147,10 @@ describe OAuth2::Client do
|
|
147
147
|
|
148
148
|
context "with custom connection options" do
|
149
149
|
it "returns custom connection" do
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
150
|
+
custom_http = Struct.new('CustomHttpClient', :url, :connection_options)
|
151
|
+
conn_options = { :connection_client => custom_http }
|
152
|
+
oauth_client = OAuth2::Client.new('example.com', @client_id, @client_secret, conn_options)
|
153
|
+
expect(oauth_client.send(:connection)).to be_instance_of custom_http
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
@@ -4,26 +4,28 @@ require 'ostruct'
|
|
4
4
|
describe OAuth2::HttpConnection do
|
5
5
|
|
6
6
|
subject do
|
7
|
-
@conn = OAuth2::HttpConnection.new('https://
|
7
|
+
@conn = OAuth2::HttpConnection.new('https://example.com')
|
8
8
|
end
|
9
9
|
|
10
|
-
context "with user options" do
|
10
|
+
context "with user specified options" do
|
11
11
|
before do
|
12
|
-
@
|
13
|
-
:
|
14
|
-
|
12
|
+
@conn_opts = {
|
13
|
+
:headers => {
|
14
|
+
:accept => 'application/json',
|
15
|
+
:user_agent => 'OAuth2 Test Client'
|
16
|
+
},
|
15
17
|
:ssl => {:verify => false},
|
16
18
|
:max_redirects => 2
|
17
|
-
}
|
19
|
+
}
|
20
|
+
@conn = OAuth2::HttpConnection.new('https://example.com', @conn_opts)
|
18
21
|
end
|
19
22
|
|
20
23
|
describe "connection options" do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
# end
|
24
|
+
it "sets user options" do
|
25
|
+
OAuth2::HttpConnection.default_options.keys.each do |key|
|
26
|
+
expect(@conn.instance_variable_get(:"@#{key}")).to eq @conn_opts[key]
|
27
|
+
end
|
28
|
+
end
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
@@ -66,7 +68,7 @@ describe OAuth2::HttpConnection do
|
|
66
68
|
|
67
69
|
describe "#host" do
|
68
70
|
it "returns the host server" do
|
69
|
-
expect(subject.host).to eq '
|
71
|
+
expect(subject.host).to eq 'example.com'
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
@@ -104,13 +106,13 @@ describe OAuth2::HttpConnection do
|
|
104
106
|
describe "#absolute_url" do
|
105
107
|
context "with no parameters" do
|
106
108
|
it "returns a uri without path" do
|
107
|
-
expect(subject.absolute_url).to eq "https://
|
109
|
+
expect(subject.absolute_url).to eq "https://example.com"
|
108
110
|
end
|
109
111
|
end
|
110
112
|
|
111
113
|
context "with parameters" do
|
112
114
|
it "returns a uri with path" do
|
113
|
-
expect(subject.absolute_url('/oauth/v2/authorize')).to eq "https://
|
115
|
+
expect(subject.absolute_url('/oauth/v2/authorize')).to eq "https://example.com/oauth/v2/authorize"
|
114
116
|
end
|
115
117
|
end
|
116
118
|
end
|
@@ -150,7 +152,7 @@ describe OAuth2::HttpConnection do
|
|
150
152
|
@http_redirect = OpenStruct.new(
|
151
153
|
:code => '301',
|
152
154
|
:body => 'redirect',
|
153
|
-
:header => {'Location' => "http://
|
155
|
+
:header => {'Location' => "http://example.com/members"}
|
154
156
|
)
|
155
157
|
end
|
156
158
|
|
@@ -162,55 +164,66 @@ describe OAuth2::HttpConnection do
|
|
162
164
|
|
163
165
|
context "when method is get" do
|
164
166
|
it "returns an http response" do
|
165
|
-
path = '/oauth/authorize'
|
166
167
|
params = {:client_id => '001337', :client_secret => 'abcxyz'}
|
167
|
-
method = :get
|
168
168
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
169
|
+
stub_get('/oauth/authorize').with(
|
170
|
+
:query => params,
|
171
|
+
:header => {
|
172
|
+
'Accept' => 'application/json',
|
173
|
+
'User-Agent' => "OAuth2 Ruby Gem #{OAuth2::Version}",
|
174
|
+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'
|
175
|
+
}
|
176
|
+
)
|
177
|
+
response = subject.send_request(:get, '/oauth/authorize', :params => params)
|
174
178
|
expect(response.code).to eq '200'
|
175
179
|
end
|
176
180
|
end
|
177
181
|
|
178
182
|
context "when method is delete" do
|
179
183
|
it "returns an http response" do
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
184
|
+
stub_delete('/users/1').with(
|
185
|
+
:header => {
|
186
|
+
'Accept' => 'application/json',
|
187
|
+
'User-Agent' => "OAuth2 Ruby Gem #{OAuth2::Version}",
|
188
|
+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'
|
189
|
+
}
|
190
|
+
)
|
191
|
+
response = subject.send_request(:delete, '/users/1')
|
186
192
|
expect(response.code).to eq '200'
|
187
193
|
end
|
188
194
|
end
|
189
195
|
|
190
196
|
context "when method is post" do
|
191
197
|
it "returns an http response" do
|
192
|
-
path = '/users'
|
193
198
|
params = {:first_name => 'john', :last_name => 'smith'}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
199
|
+
stub_post('/users').with(
|
200
|
+
:body => params,
|
201
|
+
:header => {
|
202
|
+
'Accept' => 'application/json',
|
203
|
+
'User-Agent' => "OAuth2 Ruby Gem #{OAuth2::Version}",
|
204
|
+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
|
205
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
206
|
+
}
|
207
|
+
)
|
208
|
+
response =subject.send_request(:post, '/users', :params => params)
|
200
209
|
expect(response.code).to eq '200'
|
201
210
|
end
|
202
211
|
end
|
203
212
|
|
204
213
|
context "when method is put" do
|
205
214
|
it "returns an http response" do
|
206
|
-
path = '/users/1'
|
207
215
|
params = {:first_name => 'jane', :last_name => 'doe'}
|
208
|
-
query = Addressable::URI.form_encode(params)
|
209
|
-
headers = {'Content-Type' => 'application/x-www-form-urlencoded' }.merge(subject.default_headers)
|
210
|
-
|
211
|
-
Net::HTTP.any_instance.should_receive(:put).with(path, query, headers).and_return(@http_ok)
|
212
216
|
|
213
|
-
|
217
|
+
stub_put('/users/1').with(
|
218
|
+
:body => params,
|
219
|
+
:header => {
|
220
|
+
'Accept' => 'application/json',
|
221
|
+
'User-Agent' => "OAuth2 Ruby Gem #{OAuth2::Version}",
|
222
|
+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
|
223
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
224
|
+
}
|
225
|
+
)
|
226
|
+
response = subject.send_request(:put, '/users/1', :params => params)
|
214
227
|
|
215
228
|
expect(response.code).to eq '200'
|
216
229
|
end
|
@@ -253,7 +266,7 @@ describe OAuth2::HttpConnection do
|
|
253
266
|
http_303 = OpenStruct.new(
|
254
267
|
:code => '303',
|
255
268
|
:body => 'redirect',
|
256
|
-
:header => {'Location' => "http://
|
269
|
+
:header => {'Location' => "http://example.com/members"}
|
257
270
|
)
|
258
271
|
path = '/users'
|
259
272
|
params = {:first_name => 'jane', :last_name => 'doe'}
|
@@ -75,14 +75,19 @@ describe OAuth2::Grant::AuthorizationCode do
|
|
75
75
|
|
76
76
|
describe "#fetch_authorization_url" do
|
77
77
|
it "returns response authorization page from oauth server" do
|
78
|
-
subject.should_receive(:make_request).with(:get, "/oauth2/authorize", {
|
78
|
+
subject.should_receive(:make_request).with(:get, "/oauth2/authorize", {
|
79
|
+
:params=> {:response_type=>"code", :client_id=>"s6BhdRkqt3"}
|
80
|
+
})
|
79
81
|
subject.fetch_authorization_url
|
80
82
|
end
|
81
83
|
end
|
82
84
|
|
83
85
|
describe "#get_token" do
|
84
86
|
it "exchanges authorization code for access token" do
|
85
|
-
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
87
|
+
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
88
|
+
:params => {:scope => "abc xyz", :state => "state", :code=>"G3Y6jU3a", :grant_type => "authorization_code"},
|
89
|
+
:authenticate => :headers
|
90
|
+
})
|
86
91
|
subject.get_token('G3Y6jU3a', :params => {:scope => 'abc xyz', :state => 'state'})
|
87
92
|
end
|
88
93
|
end
|
@@ -21,7 +21,10 @@ describe OAuth2::Grant::ClientCredentials do
|
|
21
21
|
|
22
22
|
describe "#get_token" do
|
23
23
|
it "exchanges authorization code for access token" do
|
24
|
-
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
24
|
+
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
25
|
+
:params => {:grant_type=>"client_credentials"},
|
26
|
+
:authenticate=>:headers
|
27
|
+
})
|
25
28
|
subject.get_token
|
26
29
|
end
|
27
30
|
end
|
@@ -21,14 +21,19 @@ describe OAuth2::Grant::DeviceCode do
|
|
21
21
|
|
22
22
|
describe "#get_code" do
|
23
23
|
it "gets user code" do
|
24
|
-
subject.should_receive(:make_request).with(:post, "/oauth2/device/code", {
|
24
|
+
subject.should_receive(:make_request).with(:post, "/oauth2/device/code", {
|
25
|
+
:params => {:client_id=>"s6BhdRkqt3"}
|
26
|
+
})
|
25
27
|
subject.get_code
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
29
31
|
describe "#get_token" do
|
30
32
|
it "gets access token" do
|
31
|
-
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
33
|
+
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
34
|
+
:params => {:code=>"G3Y6jU3a", :grant_type=>"http://oauth.net/grant_type/device/1.0"},
|
35
|
+
:authenticate => :headers
|
36
|
+
})
|
32
37
|
subject.get_token('G3Y6jU3a')
|
33
38
|
end
|
34
39
|
end
|
@@ -29,7 +29,10 @@ describe OAuth2::Grant::Implicit do
|
|
29
29
|
|
30
30
|
describe "#get_token" do
|
31
31
|
it "gets access token" do
|
32
|
-
subject.should_receive(:make_request).with(:get, "/oauth2/token", {
|
32
|
+
subject.should_receive(:make_request).with(:get, "/oauth2/token", {
|
33
|
+
:params => {:scope=>"xyz", :state=>"abc xyz", :response_type=>"token", :client_id=>"s6BhdRkqt3"},
|
34
|
+
:authenticate => :headers
|
35
|
+
})
|
33
36
|
subject.get_token(:params => {:scope => 'xyz', :state => 'abc xyz'})
|
34
37
|
end
|
35
38
|
end
|
@@ -21,7 +21,10 @@ describe OAuth2::Grant::Password do
|
|
21
21
|
|
22
22
|
describe "#get_token" do
|
23
23
|
it "gets access token" do
|
24
|
-
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
24
|
+
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
25
|
+
:params => {:grant_type=>"password", :username=>"benutzername", :password=>"passwort"},
|
26
|
+
:authenticate=>:headers
|
27
|
+
})
|
25
28
|
subject.get_token('benutzername', 'passwort')
|
26
29
|
end
|
27
30
|
end
|
@@ -20,7 +20,10 @@ describe OAuth2::Grant::RefreshToken do
|
|
20
20
|
|
21
21
|
describe "#get_token" do
|
22
22
|
it "gets access token" do
|
23
|
-
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
23
|
+
subject.should_receive(:make_request).with(:post, "/oauth2/token", {
|
24
|
+
:params => {:grant_type=>"refresh_token", :refresh_token=>"2YotnFZFEjr1zCsicMWpAA"},
|
25
|
+
:authenticate => :headers
|
26
|
+
})
|
24
27
|
subject.get_token('2YotnFZFEjr1zCsicMWpAA')
|
25
28
|
end
|
26
29
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
$:.unshift File.expand_path('../../examples', __FILE__)
|
2
2
|
|
3
3
|
# require 'simplecov'
|
4
|
-
# SimpleCov.start
|
4
|
+
# SimpleCov.start
|
5
5
|
|
6
6
|
require 'rspec'
|
7
7
|
require 'rspec/autorun'
|
8
|
+
require 'webmock/rspec'
|
8
9
|
require 'oauth2'
|
9
10
|
|
10
11
|
RSpec.configure do |config|
|
@@ -12,4 +13,20 @@ RSpec.configure do |config|
|
|
12
13
|
config.expect_with :rspec do |c|
|
13
14
|
c.syntax = :expect
|
14
15
|
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def stub_delete(path)
|
19
|
+
stub_request(:delete, 'https://example.com' + path)
|
20
|
+
end
|
21
|
+
|
22
|
+
def stub_get(path)
|
23
|
+
stub_request(:get, 'https://example.com' + path)
|
24
|
+
end
|
25
|
+
|
26
|
+
def stub_post(path)
|
27
|
+
stub_request(:post, 'https://example.com' + path)
|
28
|
+
end
|
29
|
+
|
30
|
+
def stub_put(path)
|
31
|
+
stub_request(:put, 'https://example.com' + path)
|
15
32
|
end
|
metadata
CHANGED
@@ -1,18 +1,72 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oauth2-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Kevin Mutyaba
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
-
dependencies:
|
14
|
-
|
15
|
-
|
11
|
+
date: 2013-03-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bcrypt-ruby
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: addressable
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
description: Create quick and dirty OAuth2 clients
|
16
70
|
email: tiabasnk@gmail.com
|
17
71
|
executables: []
|
18
72
|
extensions: []
|
@@ -22,12 +76,13 @@ files:
|
|
22
76
|
- .travis.yml
|
23
77
|
- Gemfile
|
24
78
|
- Gemfile.lock
|
25
|
-
- LICENSE
|
79
|
+
- LICENSE.md
|
26
80
|
- README.md
|
27
81
|
- Rakefile
|
28
|
-
- TODO
|
29
82
|
- doc/README
|
83
|
+
- examples/github_client.rb
|
30
84
|
- examples/google_client.rb
|
85
|
+
- examples/yammer_client.rb
|
31
86
|
- lib/oauth2.rb
|
32
87
|
- lib/oauth2/client.rb
|
33
88
|
- lib/oauth2/connection.rb
|
@@ -43,9 +98,9 @@ files:
|
|
43
98
|
- lib/oauth2/helper.rb
|
44
99
|
- lib/oauth2/version.rb
|
45
100
|
- oauth2-client.gemspec
|
46
|
-
- spec
|
101
|
+
- spec/examples/github_client_spec.rb
|
47
102
|
- spec/examples/google_client_spec.rb
|
48
|
-
- spec/
|
103
|
+
- spec/examples/yammer_client_spec.rb
|
49
104
|
- spec/oauth2/client_spec.rb
|
50
105
|
- spec/oauth2/connection_spec.rb
|
51
106
|
- spec/oauth2/grant/authorization_code_spec.rb
|
@@ -56,28 +111,28 @@ files:
|
|
56
111
|
- spec/oauth2/grant/password_spec.rb
|
57
112
|
- spec/oauth2/grant/refresh_token_spec.rb
|
58
113
|
- spec/spec_helper.rb
|
59
|
-
homepage:
|
60
|
-
licenses:
|
114
|
+
homepage: http://tiabas.github.com/oauth2-client/
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
61
118
|
post_install_message:
|
62
119
|
rdoc_options: []
|
63
120
|
require_paths:
|
64
121
|
- lib
|
65
122
|
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
123
|
requirements:
|
68
124
|
- - ! '>='
|
69
125
|
- !ruby/object:Gem::Version
|
70
126
|
version: '0'
|
71
127
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
128
|
requirements:
|
74
129
|
- - ! '>='
|
75
130
|
- !ruby/object:Gem::Version
|
76
|
-
version:
|
131
|
+
version: 1.3.6
|
77
132
|
requirements: []
|
78
133
|
rubyforge_project:
|
79
|
-
rubygems_version:
|
134
|
+
rubygems_version: 2.0.0
|
80
135
|
signing_key:
|
81
|
-
specification_version:
|
82
|
-
summary: OAuth2 Ruby
|
136
|
+
specification_version: 4
|
137
|
+
summary: OAuth2 client wrapper in Ruby
|
83
138
|
test_files: []
|