doorkeeper 0.4.2 → 0.5.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of doorkeeper might be problematic. Click here for more details.
- data/.gitignore +2 -0
- data/.travis.yml +5 -1
- data/CHANGELOG.md +29 -0
- data/Gemfile +12 -4
- data/README.md +76 -7
- data/Rakefile +1 -25
- data/app/assets/javascripts/doorkeeper/application.js +0 -7
- data/app/controllers/doorkeeper/application_controller.rb +1 -27
- data/app/controllers/doorkeeper/applications_controller.rb +14 -6
- data/app/controllers/doorkeeper/authorized_applications_controller.rb +1 -1
- data/app/controllers/doorkeeper/token_info_controller.rb +11 -0
- data/app/controllers/doorkeeper/tokens_controller.rb +11 -8
- data/app/validators/redirect_uri_validator.rb +12 -0
- data/app/views/doorkeeper/applications/_form.html.erb +3 -3
- data/app/views/doorkeeper/applications/edit.html.erb +1 -1
- data/app/views/doorkeeper/applications/index.html.erb +4 -4
- data/app/views/doorkeeper/applications/new.html.erb +1 -1
- data/app/views/doorkeeper/applications/show.html.erb +3 -3
- data/app/views/doorkeeper/authorizations/new.html.erb +2 -2
- data/app/views/doorkeeper/authorized_applications/index.html.erb +1 -1
- data/config/locales/en.yml +35 -0
- data/doorkeeper.gemspec +3 -3
- data/gemfiles/gemfile.rails-3.1.x +10 -0
- data/gemfiles/gemfile.rails-3.2.x +10 -0
- data/lib/doorkeeper.rb +10 -3
- data/lib/doorkeeper/config.rb +56 -38
- data/lib/doorkeeper/doorkeeper_for.rb +2 -0
- data/lib/doorkeeper/engine.rb +3 -32
- data/lib/doorkeeper/helpers/controller.rb +29 -0
- data/lib/doorkeeper/helpers/filter.rb +4 -18
- data/{app/models/doorkeeper → lib/doorkeeper/models}/access_grant.rb +7 -7
- data/{app/models/doorkeeper → lib/doorkeeper/models}/access_token.rb +27 -24
- data/lib/doorkeeper/models/accessible.rb +9 -0
- data/lib/doorkeeper/models/active_record/access_grant.rb +5 -0
- data/lib/doorkeeper/models/active_record/access_token.rb +15 -0
- data/lib/doorkeeper/models/active_record/application.rb +18 -0
- data/lib/doorkeeper/models/application.rb +38 -0
- data/lib/doorkeeper/models/expirable.rb +6 -4
- data/lib/doorkeeper/models/mongoid/access_grant.rb +22 -0
- data/lib/doorkeeper/models/mongoid/access_token.rb +35 -0
- data/lib/doorkeeper/models/mongoid/application.rb +22 -0
- data/lib/doorkeeper/models/mongoid/revocable.rb +15 -0
- data/lib/doorkeeper/models/mongoid/scopes.rb +15 -0
- data/lib/doorkeeper/models/ownership.rb +16 -0
- data/lib/doorkeeper/models/revocable.rb +1 -1
- data/lib/doorkeeper/models/scopes.rb +9 -5
- data/lib/doorkeeper/oauth/access_token_request.rb +2 -2
- data/lib/doorkeeper/oauth/authorization.rb +1 -0
- data/lib/doorkeeper/oauth/authorization/code.rb +5 -3
- data/lib/doorkeeper/oauth/client.rb +2 -2
- data/lib/doorkeeper/oauth/client_credentials_request.rb +4 -1
- data/lib/doorkeeper/oauth/helpers/unique_token.rb +2 -5
- data/lib/doorkeeper/oauth/password_access_token_request.rb +2 -5
- data/lib/doorkeeper/oauth/token.rb +36 -0
- data/lib/doorkeeper/rails/routes.rb +77 -0
- data/lib/doorkeeper/rails/routes/mapper.rb +28 -0
- data/lib/doorkeeper/rails/routes/mapping.rb +39 -0
- data/lib/doorkeeper/version.rb +1 -1
- data/lib/generators/doorkeeper/application_owner_generator.rb +15 -0
- data/lib/generators/doorkeeper/install_generator.rb +2 -9
- data/lib/generators/doorkeeper/migration_generator.rb +15 -0
- data/lib/generators/doorkeeper/templates/README +15 -1
- data/lib/generators/doorkeeper/templates/add_owner_to_application_migration.rb +7 -0
- data/lib/generators/doorkeeper/templates/initializer.rb +31 -15
- data/lib/generators/doorkeeper/templates/migration.rb +7 -4
- data/lib/generators/doorkeeper/views_generator.rb +1 -1
- data/script/run_all +3 -0
- data/spec/controllers/applications_controller_spec.rb +1 -1
- data/spec/controllers/authorizations_controller_spec.rb +4 -4
- data/spec/controllers/protected_resources_controller_spec.rb +7 -7
- data/spec/controllers/token_info_controller_spec.rb +54 -0
- data/spec/controllers/tokens_controller_spec.rb +3 -2
- data/spec/dummy/app/controllers/custom_authorizations_controller.rb +7 -0
- data/spec/dummy/app/models/user.rb +16 -5
- data/spec/dummy/config/application.rb +4 -7
- data/spec/dummy/config/boot.rb +3 -7
- data/spec/dummy/config/initializers/doorkeeper.rb +13 -0
- data/spec/dummy/config/mongoid.yml +7 -0
- data/spec/dummy/config/routes.rb +29 -1
- data/spec/dummy/db/migrate/20120312140401_add_password_to_users.rb +1 -1
- data/spec/dummy/db/migrate/20120524202412_create_doorkeeper_tables.rb +6 -4
- data/spec/dummy/db/schema.rb +5 -3
- data/spec/generators/application_owner_generator_spec.rb +23 -0
- data/spec/generators/install_generator_spec.rb +1 -6
- data/spec/generators/migration_generator_spec.rb +20 -0
- data/spec/lib/config_spec.rb +72 -4
- data/spec/lib/models/expirable_spec.rb +8 -11
- data/spec/lib/models/revocable_spec.rb +1 -1
- data/spec/lib/oauth/access_token_request_spec.rb +15 -9
- data/spec/lib/oauth/authorization_request_spec.rb +1 -0
- data/spec/lib/oauth/client_credentials_request_spec.rb +15 -9
- data/spec/lib/oauth/client_spec.rb +5 -8
- data/spec/lib/oauth/helpers/unique_token_spec.rb +2 -20
- data/spec/lib/oauth/password_access_token_request_spec.rb +16 -9
- data/spec/lib/oauth/token_spec.rb +83 -0
- data/spec/models/doorkeeper/access_token_spec.rb +41 -1
- data/spec/models/doorkeeper/application_spec.rb +53 -20
- data/spec/requests/flows/authorization_code_spec.rb +1 -1
- data/spec/requests/flows/client_credentials_spec.rb +2 -0
- data/spec/requests/flows/password_spec.rb +25 -0
- data/spec/requests/flows/refresh_token_spec.rb +5 -2
- data/spec/requests/protected_resources/private_api_spec.rb +10 -3
- data/spec/routing/custom_controller_routes_spec.rb +44 -0
- data/spec/routing/default_routes_spec.rb +32 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/spec_helper_integration.rb +18 -8
- data/spec/support/dependencies/factory_girl.rb +0 -3
- data/spec/support/orm/active_record.rb +11 -0
- data/spec/support/orm/mongoid.rb +26 -0
- data/spec/support/shared/controllers_shared_context.rb +2 -2
- data/spec/support/shared/models_shared_examples.rb +16 -0
- data/spec/validators/redirect_uri_validator_spec.rb +40 -0
- metadata +61 -37
- data/app/helpers/doorkeeper/application_helper.rb +0 -4
- data/app/models/doorkeeper/application.rb +0 -54
- data/config/routes.rb +0 -9
- data/lib/tasks/doorkeeper_tasks.rake +0 -4
- data/spec/support/dependencies/database_cleaner.rb +0 -16
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper_integration'
|
2
|
+
require 'generators/doorkeeper/migration_generator'
|
3
|
+
|
4
|
+
describe 'Doorkeeper::MigrationGenerator' do
|
5
|
+
include GeneratorSpec::TestCase
|
6
|
+
|
7
|
+
tests Doorkeeper::MigrationGenerator
|
8
|
+
destination ::File.expand_path("../tmp/dummy", __FILE__)
|
9
|
+
|
10
|
+
describe "after running the generator" do
|
11
|
+
before :each do
|
12
|
+
prepare_destination
|
13
|
+
run_generator
|
14
|
+
end
|
15
|
+
|
16
|
+
it "creates a migration" do
|
17
|
+
assert_migration "db/migrate/create_doorkeeper_tables.rb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/lib/config_spec.rb
CHANGED
@@ -7,6 +7,7 @@ describe Doorkeeper, "configuration" do
|
|
7
7
|
it "sets the block that is accessible via authenticate_resource_owner" do
|
8
8
|
block = proc do end
|
9
9
|
Doorkeeper.configure do
|
10
|
+
orm DOORKEEPER_ORM
|
10
11
|
resource_owner_authenticator &block
|
11
12
|
end
|
12
13
|
subject.authenticate_resource_owner.should == block
|
@@ -17,6 +18,7 @@ describe Doorkeeper, "configuration" do
|
|
17
18
|
it "sets the block that is accessible via authenticate_admin" do
|
18
19
|
block = proc do end
|
19
20
|
Doorkeeper.configure do
|
21
|
+
orm DOORKEEPER_ORM
|
20
22
|
admin_authenticator &block
|
21
23
|
end
|
22
24
|
subject.authenticate_admin.should == block
|
@@ -30,6 +32,7 @@ describe Doorkeeper, "configuration" do
|
|
30
32
|
|
31
33
|
it "can change the value" do
|
32
34
|
Doorkeeper.configure do
|
35
|
+
orm DOORKEEPER_ORM
|
33
36
|
access_token_expires_in 4.hours
|
34
37
|
end
|
35
38
|
subject.access_token_expires_in.should == 4.hours
|
@@ -37,6 +40,7 @@ describe Doorkeeper, "configuration" do
|
|
37
40
|
|
38
41
|
it "can be set to nil" do
|
39
42
|
Doorkeeper.configure do
|
43
|
+
orm DOORKEEPER_ORM
|
40
44
|
access_token_expires_in nil
|
41
45
|
end
|
42
46
|
subject.access_token_expires_in.should be_nil
|
@@ -45,17 +49,24 @@ describe Doorkeeper, "configuration" do
|
|
45
49
|
|
46
50
|
describe "scopes" do
|
47
51
|
it "has default scopes" do
|
48
|
-
Doorkeeper.configure {
|
52
|
+
Doorkeeper.configure {
|
53
|
+
orm DOORKEEPER_ORM
|
54
|
+
default_scopes :public
|
55
|
+
}
|
49
56
|
subject.default_scopes.should include(:public)
|
50
57
|
end
|
51
58
|
|
52
59
|
it 'has optional scopes' do
|
53
|
-
Doorkeeper.configure {
|
60
|
+
Doorkeeper.configure {
|
61
|
+
orm DOORKEEPER_ORM
|
62
|
+
optional_scopes :write, :update
|
63
|
+
}
|
54
64
|
subject.optional_scopes.should include(:write, :update)
|
55
65
|
end
|
56
66
|
|
57
67
|
it 'has all scopes' do
|
58
68
|
Doorkeeper.configure do
|
69
|
+
orm DOORKEEPER_ORM
|
59
70
|
default_scopes :normal
|
60
71
|
optional_scopes :admin
|
61
72
|
end
|
@@ -69,7 +80,10 @@ describe Doorkeeper, "configuration" do
|
|
69
80
|
end
|
70
81
|
|
71
82
|
it "can change the value" do
|
72
|
-
Doorkeeper.configure {
|
83
|
+
Doorkeeper.configure {
|
84
|
+
orm DOORKEEPER_ORM
|
85
|
+
use_refresh_token
|
86
|
+
}
|
73
87
|
subject.refresh_token_enabled?.should be_true
|
74
88
|
end
|
75
89
|
end
|
@@ -80,8 +94,62 @@ describe Doorkeeper, "configuration" do
|
|
80
94
|
end
|
81
95
|
|
82
96
|
it "can change the value" do
|
83
|
-
Doorkeeper.configure {
|
97
|
+
Doorkeeper.configure {
|
98
|
+
orm DOORKEEPER_ORM
|
99
|
+
client_credentials :from_digest, :from_params
|
100
|
+
}
|
84
101
|
subject.client_credentials_methods.should == [:from_digest, :from_params]
|
85
102
|
end
|
86
103
|
end
|
104
|
+
|
105
|
+
describe 'access_token_credentials' do
|
106
|
+
it 'has defaults order' do
|
107
|
+
subject.access_token_methods.should == [:from_bearer_authorization, :from_access_token_param, :from_bearer_param]
|
108
|
+
end
|
109
|
+
|
110
|
+
it "can change the value" do
|
111
|
+
Doorkeeper.configure {
|
112
|
+
orm DOORKEEPER_ORM
|
113
|
+
access_token_methods :from_access_token_param, :from_bearer_param
|
114
|
+
}
|
115
|
+
subject.access_token_methods.should == [:from_access_token_param, :from_bearer_param]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "enable_application_owner" do
|
120
|
+
it "is disabled by default" do
|
121
|
+
Doorkeeper.configuration.enable_application_owner?.should_not be_true
|
122
|
+
end
|
123
|
+
|
124
|
+
context "when enabled without confirmation" do
|
125
|
+
before do
|
126
|
+
Doorkeeper.configure do
|
127
|
+
orm DOORKEEPER_ORM
|
128
|
+
enable_application_owner
|
129
|
+
end
|
130
|
+
end
|
131
|
+
it "adds support for application owner" do
|
132
|
+
Doorkeeper::Application.new.should respond_to :owner
|
133
|
+
end
|
134
|
+
it "Doorkeeper.configuration.confirm_application_owner? returns false" do
|
135
|
+
Doorkeeper.configuration.confirm_application_owner?.should_not be_true
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "when enabled with confirmation set to true" do
|
140
|
+
before do
|
141
|
+
Doorkeeper.configure do
|
142
|
+
orm DOORKEEPER_ORM
|
143
|
+
enable_application_owner :confirmation => true
|
144
|
+
end
|
145
|
+
end
|
146
|
+
it "adds support for application owner" do
|
147
|
+
Doorkeeper::Application.new.should respond_to :owner
|
148
|
+
end
|
149
|
+
it "Doorkeeper.configuration.confirm_application_owner? returns true" do
|
150
|
+
Doorkeeper.configuration.confirm_application_owner?.should be_true
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
87
155
|
end
|
@@ -31,19 +31,16 @@ describe 'Expirable' do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
describe :
|
35
|
-
it "
|
36
|
-
|
37
|
-
|
38
|
-
subject.time_left.should == 10.seconds
|
39
|
-
end
|
34
|
+
describe :expires_in_seconds do
|
35
|
+
it "should return the amount of time remaining until the token is expired" do
|
36
|
+
subject.stub :expires_in => 2.minutes
|
37
|
+
subject.expires_in_seconds.should == 60
|
40
38
|
end
|
41
39
|
|
42
|
-
it "
|
43
|
-
|
44
|
-
|
45
|
-
subject.time_left.should == 0
|
46
|
-
end
|
40
|
+
it "should return 0 when expired" do
|
41
|
+
subject.stub :expires_in => 30.seconds
|
42
|
+
subject.expires_in_seconds.should == 0
|
47
43
|
end
|
44
|
+
|
48
45
|
end
|
49
46
|
end
|
@@ -12,7 +12,7 @@ describe 'Revocable' do
|
|
12
12
|
describe :revoke do
|
13
13
|
it "updates :revoked_at attribute with current time" do
|
14
14
|
clock = double :now => stub
|
15
|
-
subject.should_receive(:
|
15
|
+
subject.should_receive(:update_column).with(:revoked_at, clock.now)
|
16
16
|
subject.revoke(clock)
|
17
17
|
end
|
18
18
|
end
|
@@ -56,16 +56,19 @@ module Doorkeeper::OAuth
|
|
56
56
|
end
|
57
57
|
|
58
58
|
describe "with a valid authorization code, client and existing expired access token" do
|
59
|
-
|
59
|
+
before do
|
60
|
+
AccessTokenRequest.new(client, params).authorize
|
61
|
+
last_token = Doorkeeper::AccessToken.last
|
62
|
+
# TODO: make this better, maybe with an expire! method?
|
63
|
+
last_token.update_column :created_at, 10.days.ago
|
64
|
+
end
|
60
65
|
|
61
66
|
it "will create a new token" do
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
subject.authorize
|
68
|
-
subject.access_token.should_not eq(expired_access_token)
|
67
|
+
grant = FactoryGirl.create(:access_grant, :application => client)
|
68
|
+
authorization = AccessTokenRequest.new(client, params.merge(:code => grant.token))
|
69
|
+
expect {
|
70
|
+
authorization.authorize
|
71
|
+
}.to change { Doorkeeper::AccessToken.count }.by(1)
|
69
72
|
end
|
70
73
|
end
|
71
74
|
|
@@ -165,7 +168,10 @@ module Doorkeeper::OAuth
|
|
165
168
|
}
|
166
169
|
|
167
170
|
before do
|
168
|
-
Doorkeeper.configure {
|
171
|
+
Doorkeeper.configure {
|
172
|
+
orm DOORKEEPER_ORM
|
173
|
+
use_refresh_token
|
174
|
+
}
|
169
175
|
end
|
170
176
|
|
171
177
|
describe "with a valid authorization code and client" do
|
@@ -25,16 +25,22 @@ module Doorkeeper::OAuth
|
|
25
25
|
subject.response.should be_a(ClientCredentialsRequest::Response)
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
28
|
+
context 'if issue was not created' do
|
29
|
+
before do
|
30
|
+
subject.issuer = stub :create => false, :error => :invalid
|
31
|
+
end
|
33
32
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
its(:authorize) { should be_false }
|
34
|
+
|
35
|
+
it 'has an error response' do
|
36
|
+
subject.authorize
|
37
|
+
subject.response.should be_a(Doorkeeper::OAuth::ErrorResponse)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'delegates the error to issuer' do
|
41
|
+
subject.authorize
|
42
|
+
subject.error.should == :invalid
|
43
|
+
end
|
38
44
|
end
|
39
45
|
|
40
46
|
context 'with scopes' do
|
@@ -4,22 +4,19 @@ require 'active_support/core_ext/string'
|
|
4
4
|
require 'doorkeeper/oauth/client'
|
5
5
|
|
6
6
|
module Doorkeeper::OAuth
|
7
|
-
class Doorkeeper::Application
|
8
|
-
end
|
9
|
-
|
10
7
|
describe Client do
|
11
8
|
describe :find do
|
12
|
-
let(:
|
9
|
+
let(:method) { mock }
|
13
10
|
|
14
11
|
it 'finds the client via uid' do
|
15
12
|
client = stub
|
16
|
-
|
17
|
-
Client.find(uid).should be_a(Client)
|
13
|
+
method.should_receive(:call).with('uid').and_return(client)
|
14
|
+
Client.find('uid', method).should be_a(Client)
|
18
15
|
end
|
19
16
|
|
20
17
|
it 'returns nil if client was not found' do
|
21
|
-
|
22
|
-
Client.find(uid).should be_nil
|
18
|
+
method.should_receive(:call).with('uid').and_return(nil)
|
19
|
+
Client.find('uid', method).should be_nil
|
23
20
|
end
|
24
21
|
end
|
25
22
|
|
@@ -3,36 +3,18 @@ require 'doorkeeper/oauth/helpers/unique_token'
|
|
3
3
|
|
4
4
|
module Doorkeeper::OAuth::Helpers
|
5
5
|
describe UniqueToken do
|
6
|
-
let(:klass) { mock }
|
7
|
-
|
8
6
|
let :generator do
|
9
7
|
lambda { |size| "a" * size }
|
10
8
|
end
|
11
9
|
|
12
|
-
it "finds in the collection with given attribute" do
|
13
|
-
klass.should_receive(:find_by_attribute).and_return(nil)
|
14
|
-
UniqueToken.generate_for(:attribute, klass, :generator => generator)
|
15
|
-
end
|
16
|
-
|
17
10
|
it "is able to customize the generator method" do
|
18
|
-
|
19
|
-
token = UniqueToken.generate_for(:attribute, klass, :generator => generator)
|
11
|
+
token = UniqueToken.generate(:generator => generator)
|
20
12
|
token.should == "a" * 32
|
21
13
|
end
|
22
14
|
|
23
15
|
it "is able to customize the size of the token" do
|
24
|
-
|
25
|
-
token = UniqueToken.generate_for(:attribute, klass, :generator => generator, :size => 2)
|
16
|
+
token = UniqueToken.generate(:generator => generator, :size => 2)
|
26
17
|
token.should == "aa"
|
27
18
|
end
|
28
|
-
|
29
|
-
it "reattempt to create a token if has already found one" do
|
30
|
-
existing_tokens = ["a"*32, nil]
|
31
|
-
attempted_tokens = ["a"*32, "b"]
|
32
|
-
generator = lambda { |size| attempted_tokens.pop }
|
33
|
-
klass.stub(:find_by_attribute) { existing_tokens.pop }
|
34
|
-
token = UniqueToken.generate_for(:attribute, klass, :generator => generator)
|
35
|
-
token.should == "b"
|
36
|
-
end
|
37
19
|
end
|
38
20
|
end
|
@@ -2,7 +2,7 @@ require 'spec_helper_integration'
|
|
2
2
|
|
3
3
|
module Doorkeeper::OAuth
|
4
4
|
describe PasswordAccessTokenRequest do
|
5
|
-
let(:client) {
|
5
|
+
let(:client) { FactoryGirl.create(:application) }
|
6
6
|
let(:owner) { User.create!(:name => "Joe", :password => "sekret") }
|
7
7
|
let(:params) {
|
8
8
|
{
|
@@ -63,7 +63,10 @@ module Doorkeeper::OAuth
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it "creates a refresh token if Doorkeeper is configured to do so" do
|
66
|
-
Doorkeeper.configure {
|
66
|
+
Doorkeeper.configure {
|
67
|
+
orm DOORKEEPER_ORM
|
68
|
+
use_refresh_token
|
69
|
+
}
|
67
70
|
|
68
71
|
Doorkeeper::AccessToken.should_receive(:create!).with({
|
69
72
|
:application_id => client.id,
|
@@ -92,14 +95,17 @@ module Doorkeeper::OAuth
|
|
92
95
|
describe "with an existing expired access token" do
|
93
96
|
subject { PasswordAccessTokenRequest.new(client, owner, params) }
|
94
97
|
|
98
|
+
before do
|
99
|
+
PasswordAccessTokenRequest.new(client, owner, params).authorize
|
100
|
+
last_token = Doorkeeper::AccessToken.last
|
101
|
+
# TODO: make this better, maybe with an expire! method?
|
102
|
+
last_token.update_column :created_at, 10.days.ago
|
103
|
+
end
|
104
|
+
|
95
105
|
it "will create a new token" do
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
subject.should_receive(:create_access_token)
|
100
|
-
subject.access_token.should_receive(:revoke)
|
101
|
-
subject.authorize
|
102
|
-
subject.access_token.should_not eq(expired_access_token)
|
106
|
+
expect {
|
107
|
+
subject.authorize
|
108
|
+
}.to change { Doorkeeper::AccessToken.count }.by(1)
|
103
109
|
end
|
104
110
|
end
|
105
111
|
|
@@ -135,6 +141,7 @@ module Doorkeeper::OAuth
|
|
135
141
|
|
136
142
|
before do
|
137
143
|
Doorkeeper.configure do
|
144
|
+
orm DOORKEEPER_ORM
|
138
145
|
default_scopes :public
|
139
146
|
end
|
140
147
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_support/core_ext/string'
|
3
|
+
require 'doorkeeper/oauth/token'
|
4
|
+
|
5
|
+
module Doorkeeper
|
6
|
+
unless defined?(AccessToken)
|
7
|
+
class AccessToken
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module OAuth
|
12
|
+
describe Token do
|
13
|
+
describe :from_request do
|
14
|
+
let(:request) { stub.as_null_object }
|
15
|
+
|
16
|
+
let(:method) do
|
17
|
+
lambda { |request| return 'token-value' }
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'accepts anything that responds to #call' do
|
21
|
+
method.should_receive(:call).with(request)
|
22
|
+
Token.from_request request, method
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'delegates methods received as symbols to Token class' do
|
26
|
+
Token.should_receive(:from_params).with(request)
|
27
|
+
Token.from_request request, :from_params
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'stops at the first credentials found' do
|
31
|
+
not_called_method = mock
|
32
|
+
not_called_method.should_not_receive(:call)
|
33
|
+
credentials = Token.from_request request, lambda { |r| }, method, not_called_method
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns the credential from extractor method' do
|
37
|
+
credentials = Token.from_request request, method
|
38
|
+
credentials.should == 'token-value'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe :from_access_token_param do
|
43
|
+
it 'returns token from access_token parameter' do
|
44
|
+
request = stub :parameters => { :access_token => 'some-token' }
|
45
|
+
token = Token.from_access_token_param(request)
|
46
|
+
token.should == "some-token"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe :from_bearer_param do
|
51
|
+
it 'returns token from bearer_token parameter' do
|
52
|
+
request = stub :parameters => { :bearer_token => 'some-token' }
|
53
|
+
token = Token.from_bearer_param(request)
|
54
|
+
token.should == "some-token"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe :from_bearer_authorization do
|
59
|
+
it 'returns token from authorization bearer' do
|
60
|
+
request = stub :authorization => "Bearer SomeToken"
|
61
|
+
token = Token.from_bearer_authorization(request)
|
62
|
+
token.should == "SomeToken"
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'does not return token if authorization is not bearer' do
|
66
|
+
request = stub :authorization => "MAC SomeToken"
|
67
|
+
token = Token.from_bearer_authorization(request)
|
68
|
+
token.should be_blank
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe :authenticate do
|
73
|
+
let(:finder) { mock :finder }
|
74
|
+
|
75
|
+
it 'calls the finder if token was found' do
|
76
|
+
token = lambda { |r| 'token' }
|
77
|
+
AccessToken.should_receive(:authenticate).with('token')
|
78
|
+
Token.authenticate stub, token
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|