oauth2 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CONTRIBUTING.md +6 -3
- data/README.md +2 -11
- data/Rakefile +21 -1
- data/lib/oauth2/access_token.rb +16 -15
- data/lib/oauth2/client.rb +13 -11
- data/lib/oauth2/response.rb +8 -12
- data/lib/oauth2/strategy/assertion.rb +6 -8
- data/lib/oauth2/strategy/auth_code.rb +3 -3
- data/lib/oauth2/strategy/client_credentials.rb +12 -4
- data/lib/oauth2/strategy/implicit.rb +3 -3
- data/lib/oauth2/strategy/password.rb +2 -2
- data/lib/oauth2/version.rb +4 -7
- data/oauth2.gemspec +8 -9
- data/spec/helper.rb +5 -1
- data/spec/oauth2/access_token_spec.rb +38 -38
- data/spec/oauth2/client_spec.rb +58 -59
- data/spec/oauth2/response_spec.rb +20 -20
- data/spec/oauth2/strategy/assertion_spec.rb +13 -14
- data/spec/oauth2/strategy/auth_code_spec.rb +22 -22
- data/spec/oauth2/strategy/base_spec.rb +2 -2
- data/spec/oauth2/strategy/client_credentials_spec.rb +28 -17
- data/spec/oauth2/strategy/implicit_spec.rb +8 -8
- data/spec/oauth2/strategy/password_spec.rb +13 -13
- metadata +52 -82
- metadata.gz.sig +0 -0
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
describe OAuth2::Response do
|
4
|
-
describe
|
5
|
-
let(:status) {200}
|
6
|
-
let(:headers) {{'foo' => 'bar'}}
|
7
|
-
let(:body) {'foo'}
|
4
|
+
describe '#initialize' do
|
5
|
+
let(:status) { 200 }
|
6
|
+
let(:headers) { {'foo' => 'bar'} }
|
7
|
+
let(:body) { 'foo' }
|
8
8
|
|
9
|
-
it
|
9
|
+
it 'returns the status, headers and body' do
|
10
10
|
response = double('response', :headers => headers,
|
11
11
|
:status => status,
|
12
12
|
:body => body)
|
@@ -17,30 +17,30 @@ describe OAuth2::Response do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
describe
|
21
|
-
let(:response)
|
20
|
+
describe '.register_parser' do
|
21
|
+
let(:response) do
|
22
22
|
double('response', :headers => {'Content-Type' => 'application/foo-bar'},
|
23
23
|
:status => 200,
|
24
24
|
:body => 'baz')
|
25
|
-
|
25
|
+
end
|
26
26
|
before do
|
27
27
|
OAuth2::Response.register_parser(:foobar, 'application/foo-bar') do |body|
|
28
28
|
"foobar #{body}"
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
32
|
+
it 'adds to the content types and parsers' do
|
33
33
|
expect(OAuth2::Response::PARSERS.keys).to include(:foobar)
|
34
34
|
expect(OAuth2::Response::CONTENT_TYPES.keys).to include('application/foo-bar')
|
35
35
|
end
|
36
36
|
|
37
|
-
it
|
37
|
+
it 'is able to parse that content type automatically' do
|
38
38
|
expect(OAuth2::Response.new(response).parsed).to eq('foobar baz')
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
describe
|
43
|
-
it
|
42
|
+
describe '#parsed' do
|
43
|
+
it 'parses application/x-www-form-urlencoded body' do
|
44
44
|
headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
|
45
45
|
body = 'foo=bar&answer=42'
|
46
46
|
response = double('response', :headers => headers, :body => body)
|
@@ -50,7 +50,7 @@ describe OAuth2::Response do
|
|
50
50
|
expect(subject.parsed['answer']).to eq('42')
|
51
51
|
end
|
52
52
|
|
53
|
-
it
|
53
|
+
it 'parses application/json body' do
|
54
54
|
headers = {'Content-Type' => 'application/json'}
|
55
55
|
body = MultiJson.encode(:foo => 'bar', :answer => 42)
|
56
56
|
response = double('response', :headers => headers, :body => body)
|
@@ -66,26 +66,26 @@ describe OAuth2::Response do
|
|
66
66
|
|
67
67
|
response = double('response', :headers => headers, :body => body)
|
68
68
|
|
69
|
-
MultiJson.
|
70
|
-
MultiJson.
|
71
|
-
Rack::Utils.
|
69
|
+
expect(MultiJson).not_to receive(:decode)
|
70
|
+
expect(MultiJson).not_to receive(:load)
|
71
|
+
expect(Rack::Utils).not_to receive(:parse_query)
|
72
72
|
|
73
73
|
subject = Response.new(response)
|
74
74
|
expect(subject.parsed).to be_nil
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
context
|
79
|
-
it
|
78
|
+
context 'xml parser registration' do
|
79
|
+
it 'tries to load multi_xml and use it' do
|
80
80
|
expect(OAuth2::Response::PARSERS[:xml]).not_to be_nil
|
81
81
|
end
|
82
82
|
|
83
|
-
it
|
83
|
+
it 'is able to parse xml' do
|
84
84
|
headers = {'Content-Type' => 'text/xml'}
|
85
85
|
body = '<?xml version="1.0" standalone="yes" ?><foo><bar>baz</bar></foo>'
|
86
86
|
|
87
87
|
response = double('response', :headers => headers, :body => body)
|
88
|
-
expect(OAuth2::Response.new(response).parsed).to eq(
|
88
|
+
expect(OAuth2::Response.new(response).parsed).to eq('foo' => {'bar' => 'baz'})
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
@@ -7,10 +7,10 @@ describe OAuth2::Strategy::Assertion do
|
|
7
7
|
b.adapter :test do |stub|
|
8
8
|
stub.post('/oauth/token') do |env|
|
9
9
|
case @mode
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
when 'formencoded'
|
11
|
+
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, 'expires_in=600&access_token=salmon&refresh_token=trout']
|
12
|
+
when 'json'
|
13
|
+
[200, {'Content-Type' => 'application/json'}, '{"expires_in":600,"access_token":"salmon","refresh_token":"trout"}']
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -18,13 +18,13 @@ describe OAuth2::Strategy::Assertion do
|
|
18
18
|
cli
|
19
19
|
end
|
20
20
|
|
21
|
-
let(:params) { {:hmac_secret => 'foo'}}
|
21
|
+
let(:params) { {:hmac_secret => 'foo'} }
|
22
22
|
|
23
|
-
subject {client.assertion}
|
23
|
+
subject { client.assertion }
|
24
24
|
|
25
|
-
describe
|
26
|
-
it
|
27
|
-
expect{subject.authorize_url}.to raise_error(NotImplementedError)
|
25
|
+
describe '#authorize_url' do
|
26
|
+
it 'raises NotImplementedError' do
|
27
|
+
expect { subject.authorize_url }.to raise_error(NotImplementedError)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -35,23 +35,22 @@ describe OAuth2::Strategy::Assertion do
|
|
35
35
|
@access = subject.get_token(params)
|
36
36
|
end
|
37
37
|
|
38
|
-
it
|
38
|
+
it 'returns AccessToken with same Client' do
|
39
39
|
expect(@access.client).to eq(client)
|
40
40
|
end
|
41
41
|
|
42
|
-
it
|
42
|
+
it 'returns AccessToken with #token' do
|
43
43
|
expect(@access.token).to eq('salmon')
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
46
|
+
it 'returns AccessToken with #expires_in' do
|
47
47
|
expect(@access.expires_in).to eq(600)
|
48
48
|
end
|
49
49
|
|
50
|
-
it
|
50
|
+
it 'returns AccessToken with #expires_at' do
|
51
51
|
expect(@access.expires_at).not_to be_nil
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
end
|
57
|
-
|
@@ -1,31 +1,31 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
describe OAuth2::Strategy::AuthCode do
|
4
|
-
let(:code) {'sushi'}
|
5
|
-
let(:kvform_token) {'expires_in=600&access_token=salmon&refresh_token=trout&extra_param=steve'}
|
6
|
-
let(:facebook_token) {kvform_token.gsub('_in', '')}
|
7
|
-
let(:json_token) {MultiJson.encode(:expires_in => 600, :access_token => 'salmon', :refresh_token => 'trout', :extra_param => 'steve')}
|
4
|
+
let(:code) { 'sushi' }
|
5
|
+
let(:kvform_token) { 'expires_in=600&access_token=salmon&refresh_token=trout&extra_param=steve' }
|
6
|
+
let(:facebook_token) { kvform_token.gsub('_in', '') }
|
7
|
+
let(:json_token) { MultiJson.encode(:expires_in => 600, :access_token => 'salmon', :refresh_token => 'trout', :extra_param => 'steve') }
|
8
8
|
|
9
9
|
let(:client) do
|
10
10
|
OAuth2::Client.new('abc', 'def', :site => 'http://api.example.com') do |builder|
|
11
11
|
builder.adapter :test do |stub|
|
12
12
|
stub.get("/oauth/token?client_id=abc&client_secret=def&code=#{code}&grant_type=authorization_code") do |env|
|
13
13
|
case @mode
|
14
|
-
when
|
14
|
+
when 'formencoded'
|
15
15
|
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, kvform_token]
|
16
|
-
when
|
16
|
+
when 'json'
|
17
17
|
[200, {'Content-Type' => 'application/json'}, json_token]
|
18
|
-
when
|
18
|
+
when 'from_facebook'
|
19
19
|
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, facebook_token]
|
20
20
|
end
|
21
21
|
end
|
22
|
-
stub.post('/oauth/token',
|
22
|
+
stub.post('/oauth/token', 'client_id' => 'abc', 'client_secret' => 'def', 'code' => 'sushi', 'grant_type' => 'authorization_code') do |env|
|
23
23
|
case @mode
|
24
|
-
when
|
24
|
+
when 'formencoded'
|
25
25
|
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, kvform_token]
|
26
|
-
when
|
26
|
+
when 'json'
|
27
27
|
[200, {'Content-Type' => 'application/json'}, json_token]
|
28
|
-
when
|
28
|
+
when 'from_facebook'
|
29
29
|
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, facebook_token]
|
30
30
|
end
|
31
31
|
end
|
@@ -33,18 +33,18 @@ describe OAuth2::Strategy::AuthCode do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
subject {client.auth_code}
|
36
|
+
subject { client.auth_code }
|
37
37
|
|
38
|
-
describe
|
39
|
-
it
|
38
|
+
describe '#authorize_url' do
|
39
|
+
it 'includes the client_id' do
|
40
40
|
expect(subject.authorize_url).to include('client_id=abc')
|
41
41
|
end
|
42
42
|
|
43
|
-
it
|
43
|
+
it 'includes the type' do
|
44
44
|
expect(subject.authorize_url).to include('response_type=code')
|
45
45
|
end
|
46
46
|
|
47
|
-
it
|
47
|
+
it 'includes passed in options' do
|
48
48
|
cb = 'http://myserver.local/oauth/callback'
|
49
49
|
expect(subject.authorize_url(:redirect_uri => cb)).to include("redirect_uri=#{Rack::Utils.escape(cb)}")
|
50
50
|
end
|
@@ -59,27 +59,27 @@ describe OAuth2::Strategy::AuthCode do
|
|
59
59
|
@access = subject.get_token(code)
|
60
60
|
end
|
61
61
|
|
62
|
-
it
|
62
|
+
it 'returns AccessToken with same Client' do
|
63
63
|
expect(@access.client).to eq(client)
|
64
64
|
end
|
65
65
|
|
66
|
-
it
|
66
|
+
it 'returns AccessToken with #token' do
|
67
67
|
expect(@access.token).to eq('salmon')
|
68
68
|
end
|
69
69
|
|
70
|
-
it
|
70
|
+
it 'returns AccessToken with #refresh_token' do
|
71
71
|
expect(@access.refresh_token).to eq('trout')
|
72
72
|
end
|
73
73
|
|
74
|
-
it
|
74
|
+
it 'returns AccessToken with #expires_in' do
|
75
75
|
expect(@access.expires_in).to eq(600)
|
76
76
|
end
|
77
77
|
|
78
|
-
it
|
78
|
+
it 'returns AccessToken with #expires_at' do
|
79
79
|
expect(@access.expires_at).to be_kind_of(Integer)
|
80
80
|
end
|
81
81
|
|
82
|
-
it
|
82
|
+
it 'returns AccessToken with params accessible via []' do
|
83
83
|
expect(@access['extra_param']).to eq('steve')
|
84
84
|
end
|
85
85
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
describe OAuth2::Strategy::Base do
|
4
|
-
it
|
5
|
-
expect{OAuth2::Strategy::Base.new(OAuth2::Client.new('abc', 'def'))}.not_to raise_error
|
4
|
+
it 'initializes with a Client' do
|
5
|
+
expect { OAuth2::Strategy::Base.new(OAuth2::Client.new('abc', 'def')) }.not_to raise_error
|
6
6
|
end
|
7
7
|
end
|
@@ -7,21 +7,21 @@ describe OAuth2::Strategy::ClientCredentials do
|
|
7
7
|
let(:client) do
|
8
8
|
OAuth2::Client.new('abc', 'def', :site => 'http://api.example.com') do |builder|
|
9
9
|
builder.adapter :test do |stub|
|
10
|
-
stub.post('/oauth/token',
|
11
|
-
client_id, client_secret =
|
12
|
-
client_id == 'abc' && client_secret == 'def'
|
10
|
+
stub.post('/oauth/token', 'grant_type' => 'client_credentials') do |env|
|
11
|
+
client_id, client_secret = Base64.decode64(env[:request_headers]['Authorization'].split(' ', 2)[1]).split(':', 2)
|
12
|
+
client_id == 'abc' && client_secret == 'def' || fail(Faraday::Adapter::Test::Stubs::NotFound)
|
13
13
|
case @mode
|
14
|
-
when
|
14
|
+
when 'formencoded'
|
15
15
|
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, kvform_token]
|
16
|
-
when
|
16
|
+
when 'json'
|
17
17
|
[200, {'Content-Type' => 'application/json'}, json_token]
|
18
18
|
end
|
19
19
|
end
|
20
|
-
stub.post('/oauth/token',
|
20
|
+
stub.post('/oauth/token', 'client_id' => 'abc', 'client_secret' => 'def', 'grant_type' => 'client_credentials') do |env|
|
21
21
|
case @mode
|
22
|
-
when
|
22
|
+
when 'formencoded'
|
23
23
|
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, kvform_token]
|
24
|
-
when
|
24
|
+
when 'json'
|
25
25
|
[200, {'Content-Type' => 'application/json'}, json_token]
|
26
26
|
end
|
27
27
|
end
|
@@ -29,11 +29,22 @@ describe OAuth2::Strategy::ClientCredentials do
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
subject {client.client_credentials}
|
32
|
+
subject { client.client_credentials }
|
33
33
|
|
34
|
-
describe
|
35
|
-
it
|
36
|
-
expect{subject.authorize_url}.to raise_error(NotImplementedError)
|
34
|
+
describe '#authorize_url' do
|
35
|
+
it 'raises NotImplementedError' do
|
36
|
+
expect { subject.authorize_url }.to raise_error(NotImplementedError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#authorization' do
|
41
|
+
it 'generates an Authorization header value for HTTP Basic Authentication' do
|
42
|
+
[
|
43
|
+
['abc', 'def', 'Basic YWJjOmRlZg=='],
|
44
|
+
['xxx', 'secret', 'Basic eHh4OnNlY3JldA==']
|
45
|
+
].each do |client_id, client_secret, expected|
|
46
|
+
expect(subject.authorization(client_id, client_secret)).to eq(expected)
|
47
|
+
end
|
37
48
|
end
|
38
49
|
end
|
39
50
|
|
@@ -45,23 +56,23 @@ describe OAuth2::Strategy::ClientCredentials do
|
|
45
56
|
@access = subject.get_token({}, auth_scheme == 'default' ? {} : {'auth_scheme' => auth_scheme})
|
46
57
|
end
|
47
58
|
|
48
|
-
it
|
59
|
+
it 'returns AccessToken with same Client' do
|
49
60
|
expect(@access.client).to eq(client)
|
50
61
|
end
|
51
62
|
|
52
|
-
it
|
63
|
+
it 'returns AccessToken with #token' do
|
53
64
|
expect(@access.token).to eq('salmon')
|
54
65
|
end
|
55
66
|
|
56
|
-
it
|
67
|
+
it 'returns AccessToken without #refresh_token' do
|
57
68
|
expect(@access.refresh_token).to be_nil
|
58
69
|
end
|
59
70
|
|
60
|
-
it
|
71
|
+
it 'returns AccessToken with #expires_in' do
|
61
72
|
expect(@access.expires_in).to eq(600)
|
62
73
|
end
|
63
74
|
|
64
|
-
it
|
75
|
+
it 'returns AccessToken with #expires_at' do
|
65
76
|
expect(@access.expires_at).not_to be_nil
|
66
77
|
end
|
67
78
|
end
|
@@ -3,26 +3,26 @@ require 'helper'
|
|
3
3
|
describe OAuth2::Strategy::Implicit do
|
4
4
|
let(:client) { OAuth2::Client.new('abc', 'def', :site => 'http://api.example.com') }
|
5
5
|
|
6
|
-
subject {client.implicit}
|
6
|
+
subject { client.implicit }
|
7
7
|
|
8
|
-
describe
|
9
|
-
it
|
8
|
+
describe '#authorize_url' do
|
9
|
+
it 'includes the client_id' do
|
10
10
|
expect(subject.authorize_url).to include('client_id=abc')
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
13
|
+
it 'includes the type' do
|
14
14
|
expect(subject.authorize_url).to include('response_type=token')
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'includes passed in options' do
|
18
18
|
cb = 'http://myserver.local/oauth/callback'
|
19
19
|
expect(subject.authorize_url(:redirect_uri => cb)).to include("redirect_uri=#{Rack::Utils.escape(cb)}")
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
describe
|
24
|
-
it
|
25
|
-
expect{subject.get_token}.to raise_error(NotImplementedError)
|
23
|
+
describe '#get_token' do
|
24
|
+
it 'raises NotImplementedError' do
|
25
|
+
expect { subject.get_token }.to raise_error(NotImplementedError)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -7,21 +7,21 @@ describe OAuth2::Strategy::Password do
|
|
7
7
|
b.adapter :test do |stub|
|
8
8
|
stub.post('/oauth/token') do |env|
|
9
9
|
case @mode
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
when 'formencoded'
|
11
|
+
[200, {'Content-Type' => 'application/x-www-form-urlencoded'}, 'expires_in=600&access_token=salmon&refresh_token=trout']
|
12
|
+
when 'json'
|
13
|
+
[200, {'Content-Type' => 'application/json'}, '{"expires_in":600,"access_token":"salmon","refresh_token":"trout"}']
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
18
18
|
cli
|
19
19
|
end
|
20
|
-
subject {client.password}
|
20
|
+
subject { client.password }
|
21
21
|
|
22
|
-
describe
|
23
|
-
it
|
24
|
-
expect{subject.authorize_url}.to raise_error(NotImplementedError)
|
22
|
+
describe '#authorize_url' do
|
23
|
+
it 'raises NotImplementedError' do
|
24
|
+
expect { subject.authorize_url }.to raise_error(NotImplementedError)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -32,23 +32,23 @@ describe OAuth2::Strategy::Password do
|
|
32
32
|
@access = subject.get_token('username', 'password')
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it 'returns AccessToken with same Client' do
|
36
36
|
expect(@access.client).to eq(client)
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
39
|
+
it 'returns AccessToken with #token' do
|
40
40
|
expect(@access.token).to eq('salmon')
|
41
41
|
end
|
42
42
|
|
43
|
-
it
|
43
|
+
it 'returns AccessToken with #refresh_token' do
|
44
44
|
expect(@access.refresh_token).to eq('trout')
|
45
45
|
end
|
46
46
|
|
47
|
-
it
|
47
|
+
it 'returns AccessToken with #expires_in' do
|
48
48
|
expect(@access.expires_in).to eq(600)
|
49
49
|
end
|
50
50
|
|
51
|
-
it
|
51
|
+
it 'returns AccessToken with #expires_at' do
|
52
52
|
expect(@access.expires_at).not_to be_nil
|
53
53
|
end
|
54
54
|
end
|