doorkeeper-mongodb 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -4
  3. data/lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb +1 -1
  4. data/lib/doorkeeper-mongodb/mixins/mongo_mapper/application_mixin.rb +28 -1
  5. data/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +1 -1
  6. data/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +28 -1
  7. data/lib/doorkeeper-mongodb/version.rb +1 -1
  8. data/lib/doorkeeper/orm/mongo_mapper/application.rb +1 -0
  9. data/lib/doorkeeper/orm/mongoid4/application.rb +1 -0
  10. data/lib/doorkeeper/orm/mongoid5/application.rb +1 -0
  11. data/lib/doorkeeper/orm/mongoid6/application.rb +1 -0
  12. data/lib/doorkeeper/orm/mongoid7/application.rb +1 -0
  13. data/spec/controllers/authorizations_controller_spec.rb +34 -2
  14. data/spec/controllers/tokens_controller_spec.rb +59 -7
  15. data/spec/dummy/config/initializers/doorkeeper.rb +16 -0
  16. data/spec/dummy/db/migrate/20111122132257_create_users.rb +3 -1
  17. data/spec/dummy/db/migrate/20120312140401_add_password_to_users.rb +3 -1
  18. data/spec/dummy/db/migrate/20151223192035_create_doorkeeper_tables.rb +3 -1
  19. data/spec/dummy/db/migrate/20151223200000_add_owner_to_application.rb +3 -1
  20. data/spec/dummy/db/migrate/20160320211015_add_previous_refresh_token_to_access_tokens.rb +3 -1
  21. data/spec/dummy/db/migrate/20180210183654_add_confidential_to_application.rb +13 -0
  22. data/spec/dummy/db/schema.rb +2 -1
  23. data/spec/dummy/log/test.log +42919 -1155
  24. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{D9/D9fEdDvxWhngpnQPHr1FTwQHL604FuHmaWxQlh7nok4.cache → -4/-4b6k9VbovmXpob9TEreMNbQqIq2frgEkXbJmaLpXXo.cache} +0 -0
  25. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{ta/taYouTiRnSIHwA3iNj6mcoN57fxG6-AcZRBxXnoZA6A.cache → 3R/3R0IlALSataFe0QXquFlLgPkS12rgPAHsrnaCNZcP5E.cache} +1 -1
  26. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{Wq/WqR0oz-DxQxVQZ9WeV8Ey01S8-hmMym-alX1ZIeGcyw.cache → 45/45HM8KBWEUkiP_490juLH2Z8mPYNvM0SWJEsbZSf-S0.cache} +0 -0
  27. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{fe/feHuxmxsfeyr6pDo0Hsq-ToOVXAjCZnZl9Vt5bvFLYg.cache → 7C/7C_H6iKWpfJDii1YogQrSSaPEK7vd4Yfuqhd3QloNjI.cache} +0 -0
  28. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{b2/b2BfYDhSV9fWL8fEi5lO_H--gisMJqwzT0gmWDyEseo.cache → EJ/EJEkZ1VTObngEDZDbY6RBWwvY8bZfUlUSccPwFdQstU.cache} +0 -0
  29. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{x3/x3quJr1-zURZiHgn1fyavoPEcZ6Gz-F-ZTGHLMCq8F4.cache → Gc/GcCh3Q_ZFDDRrSCrGY-akIqT_d5smO_RcJ7BTDxDx9M.cache} +0 -0
  30. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{Wz/WzU5ImvcBYPRRo1bDcUoMBnYvMHElmP1WmpSoWFGrIA.cache → IZ/IZF3X64f5MivAUq6IkvkQkPKxa06jCGI94vN_imFwv8.cache} +1 -1
  31. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Vw/Vw7y8QAO4DQwNaWGwLo7_3NKa_pdX0vCYAzk7CVykfw.cache +0 -0
  32. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{Gd/Gd13CrC_hyFXI6YkLDcl76UZb5vYjlYqFBmF0_aY2UA.cache → Y7/Y772A7TJ6-gpe-9AmRmcRZpJK6g92kS1nWduuGFPfOM.cache} +0 -0
  33. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/iZ/iZxru9bhjMYVAmekcMRoFdZiEnpEj55tS7566aoOE_Q.cache +0 -0
  34. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/jj/jjeDmVIyET494Gq15Sob1rbi_KO8D1-vwo4vwVIjYWg.cache +0 -0
  35. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{T9/T9SRvIirztZ2z-NS27p6uDCnS-SfsrqePihCkC7Z0bo.cache → r2/r2BLffvW211dRKmFTSEtrrxY9mgSpAbJvAMYgffd0Wo.cache} +1 -1
  36. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{7B/7BQrqCAX5USU9CPwXaBgC9o5N8slSgZi8nbDPdXZWy4.cache → rO/rOJ5Qletb3Q5P_zAPve6Pb0AZLvUfqhr8eTmIMHhTjE.cache} +1 -1
  37. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/{Mc/Mc82fixkCIrNkzovtrFyWVaxz_YMs_0nB0z2aUfm1k4.cache → wy/wy1q9jo24sJvPLH8a36mvtxEc8mWcEevN1zuAo-AJHg.cache} +1 -1
  38. data/spec/lib/config_spec.rb +25 -0
  39. data/spec/lib/oauth/authorization_code_request_spec.rb +18 -3
  40. data/spec/lib/oauth/client/credentials_spec.rb +4 -2
  41. data/spec/lib/oauth/helpers/uri_checker_spec.rb +110 -1
  42. data/spec/lib/oauth/password_access_token_request_spec.rb +3 -3
  43. data/spec/lib/oauth/pre_authorization_spec.rb +12 -7
  44. data/spec/lib/oauth/refresh_token_request_spec.rb +3 -3
  45. data/spec/models/doorkeeper/application_spec.rb +96 -5
  46. data/spec/requests/flows/authorization_code_spec.rb +1 -1
  47. data/spec/requests/flows/password_spec.rb +64 -21
  48. metadata +36 -34
  49. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/cR/cRfgFbJ7Ssu1jWEBQejbIXjvNXL4NcrEwyow92EhVYI.cache +0 -0
  50. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/wi/wiNoTKijy4NStsMmmG4tY5xuViXEiGgA944y09eeqH4.cache +0 -0
  51. data/spec/dummy/tmp/cache/assets/sprockets/v3.0/x0/x0P5YVye0-UGqw0Ogxkk3qBPNTjQ9ehoc5Kf58zrttk.cache +0 -0
@@ -1 +1 @@
1
- I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/application.css?type=text/css&pipeline=self&id=f4b96ba5417e205c1c44db290662b52c49795c768126b78337c466c871a39c34:ET
1
+ I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/application.css?type=text/css&pipeline=self&id=b2c6260117a362189887859543d86381736f0f1e20635ff6d55612e4247e326a:ET
@@ -1 +1 @@
1
- I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/application.css?type=text/css&id=cbc2326f499a8551d3db0c4aa97a8824202d1f1ac295f50e3defa56e042ca94c:ET
1
+ I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/application.css?type=text/css&id=2b4f1a012376fe8df9bad1ef9d1f17322a7d2eea251cfbb256abee7b2be1aa2b:ET
@@ -1 +1 @@
1
- I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/admin/application.css?type=text/css&id=294b60282f08e4364e1ef6f923e8031876e06270421d6ceb4a0a37f700d4d567:ET
1
+ I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/admin/application.css?type=text/css&id=7a99826ec09d953cd0659f6b3f999512519f4d6e3670f951c04ed582ffc92520:ET
@@ -1 +1 @@
1
- I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/admin/application.css?type=text/css&pipeline=self&id=36bb2174827753889f2fd19280eded5dbc7d4e0c4ae9e9c5f869cb5dfd00d31f:ET
1
+ I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/app/assets/stylesheets/doorkeeper/admin/application.css?type=text/css&pipeline=self&id=5dd6dfc621fb6aa8db94bad97ddf0700adfaa6f5f98a8a26b7c25822036b070d:ET
@@ -1 +1 @@
1
- I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/vendor/assets/stylesheets/doorkeeper/bootstrap.min.css?type=text/css&pipeline=self&id=1163952fc3e6a5ef3074948d03b7344741db14c5f67d4bd042622360ac4413e4:ET
1
+ I"�/home/bulaj/projects/gems/doorkeeper-mongodb/doorkeeper/vendor/assets/stylesheets/doorkeeper/bootstrap.min.css?type=text/css&pipeline=self&id=0280591bce4b097f9b7bbf8290c8c59904fd3d9ed44066e86d9cc6f99a562a88:ET
@@ -162,6 +162,31 @@ describe Doorkeeper, 'configuration' do
162
162
  end
163
163
  end
164
164
 
165
+ describe 'opt_out_native_route_change' do
166
+ around(:each) do |example|
167
+ Doorkeeper.configure do
168
+ orm DOORKEEPER_ORM
169
+ opt_out_native_route_change
170
+ end
171
+
172
+ Rails.application.reload_routes!
173
+
174
+ subject { Doorkeeper.configuration }
175
+
176
+ example.run
177
+
178
+ Doorkeeper.configure do
179
+ orm DOORKEEPER_ORM
180
+ end
181
+
182
+ Rails.application.reload_routes!
183
+ end
184
+
185
+ it 'sets the native authorization code route /:code' do
186
+ expect(subject.native_authorization_code_route).to eq('/:code')
187
+ end
188
+ end
189
+
165
190
  describe 'client_credentials' do
166
191
  it 'has defaults order' do
167
192
  expect(subject.client_credentials_methods).to eq([:from_basic, :from_params])
@@ -81,9 +81,9 @@ module Doorkeeper::OAuth
81
81
  expect { subject.authorize }.to_not change { Doorkeeper::AccessToken.count }
82
82
  end
83
83
 
84
- it "calls BaseRequest callback methods" do
85
- expect_any_instance_of(BaseRequest).to receive(:before_successful_response).once
86
- expect_any_instance_of(BaseRequest).to receive(:after_successful_response).once
84
+ it "calls configured request callback methods" do
85
+ expect(Doorkeeper.configuration.before_successful_strategy_response).to receive(:call).with(subject).once
86
+ expect(Doorkeeper.configuration.after_successful_strategy_response).to receive(:call).with(subject, instance_of(Doorkeeper::OAuth::TokenResponse)).once
87
87
  subject.authorize
88
88
  end
89
89
 
@@ -104,5 +104,20 @@ module Doorkeeper::OAuth
104
104
  expect(subject.error).to eq(:invalid_grant)
105
105
  end
106
106
  end
107
+
108
+ context "when redirect_uri is the native one" do
109
+ let(:redirect_uri) { 'urn:ietf:wg:oauth:2.0:oob' }
110
+
111
+ it "invalidates when redirect_uri of the grant is not native" do
112
+ subject.validate
113
+ expect(subject.error).to eq(:invalid_grant)
114
+ end
115
+
116
+ it "validates when redirect_uri of the grant is also native" do
117
+ allow(grant).to receive(:redirect_uri) { redirect_uri }
118
+ subject.validate
119
+ expect(subject.error).to eq(nil)
120
+ end
121
+ end
107
122
  end
108
123
  end
@@ -7,9 +7,11 @@ class Doorkeeper::OAuth::Client
7
7
  let(:client_id) { 'some-uid' }
8
8
  let(:client_secret) { 'some-secret' }
9
9
 
10
- it 'is blank when any of the credentials is blank' do
10
+ it 'is blank when the uid in credentials is blank' do
11
+ expect(Credentials.new(nil, nil)).to be_blank
11
12
  expect(Credentials.new(nil, 'something')).to be_blank
12
- expect(Credentials.new('something', nil)).to be_blank
13
+ expect(Credentials.new('something', nil)).to be_present
14
+ expect(Credentials.new('something', 'something')).to be_present
13
15
  end
14
16
 
15
17
  describe :from_request do
@@ -5,6 +5,11 @@ require 'doorkeeper/oauth/helpers/uri_checker'
5
5
  module Doorkeeper::OAuth::Helpers
6
6
  describe URIChecker do
7
7
  describe '.valid?' do
8
+ it 'is valid for native uris' do
9
+ uri = 'urn:ietf:wg:oauth:2.0:oob'
10
+ expect(URIChecker.valid?(uri)).to be_truthy
11
+ end
12
+
8
13
  it 'is valid for valid uris' do
9
14
  uri = 'http://app.co'
10
15
  expect(URIChecker.valid?(uri)).to be_truthy
@@ -69,6 +74,44 @@ module Doorkeeper::OAuth::Helpers
69
74
  client_uri = 'http://example.com?app.co=test'
70
75
  expect(URIChecker.matches?(uri, client_uri)).to be_falsey
71
76
  end
77
+
78
+ context "client registered query params" do
79
+ it "doesn't allow query being absent" do
80
+ uri = 'http://app.co'
81
+ client_uri = 'http://app.co/?vendorId=AJ4L7XXW9'
82
+ expect(URIChecker.matches?(uri, client_uri)).to be_falsey
83
+ end
84
+
85
+ it "is false if query values differ but key same" do
86
+ uri = 'http://app.co/?vendorId=pancakes'
87
+ client_uri = 'http://app.co/?vendorId=waffles'
88
+ expect(URIChecker.matches?(uri, client_uri)).to be_falsey
89
+ end
90
+
91
+ it "is false if query values same but key differs" do
92
+ uri = 'http://app.co/?foo=pancakes'
93
+ client_uri = 'http://app.co/?bar=pancakes'
94
+ expect(URIChecker.matches?(uri, client_uri)).to be_falsey
95
+ end
96
+
97
+ it "is false if query present and match, but unknown queries present" do
98
+ uri = 'http://app.co/?vendorId=pancakes&unknown=query'
99
+ client_uri = 'http://app.co/?vendorId=waffles'
100
+ expect(URIChecker.matches?(uri, client_uri)).to be_falsey
101
+ end
102
+
103
+ it "is true if queries are present and matche" do
104
+ uri = 'http://app.co/?vendorId=AJ4L7XXW9&foo=bar'
105
+ client_uri = 'http://app.co/?vendorId=AJ4L7XXW9&foo=bar'
106
+ expect(URIChecker.matches?(uri, client_uri)).to be_truthy
107
+ end
108
+
109
+ it "is true if queries are present, match and in different order" do
110
+ uri = 'http://app.co/?bing=bang&foo=bar'
111
+ client_uri = 'http://app.co/?foo=bar&bing=bang'
112
+ expect(URIChecker.matches?(uri, client_uri)).to be_truthy
113
+ end
114
+ end
72
115
  end
73
116
 
74
117
  describe '.valid_for_authorization?' do
@@ -101,9 +144,75 @@ module Doorkeeper::OAuth::Helpers
101
144
  end
102
145
 
103
146
  it 'is false if invalid' do
104
- uri = client_uri = 'http://app.co/aaa?waffles=abc'
147
+ uri = 'http://app.co/aaa?pankcakes=abc'
148
+ client_uri = 'http://app.co/aaa?waffles=abc'
105
149
  expect(URIChecker.valid_for_authorization?(uri, client_uri)).to be false
106
150
  end
151
+
152
+ it 'calls .matches?' do
153
+ uri = 'http://app.co/aaa?pankcakes=abc'
154
+ client_uri = 'http://app.co/aaa?waffles=abc'
155
+ expect(URIChecker).to receive(:matches?).with(uri, client_uri).once
156
+ URIChecker.valid_for_authorization?(uri, client_uri)
157
+ end
158
+
159
+ it 'calls .valid?' do
160
+ uri = 'http://app.co/aaa?pankcakes=abc'
161
+ client_uri = 'http://app.co/aaa?waffles=abc'
162
+ expect(URIChecker).to receive(:valid?).with(uri).once
163
+ URIChecker.valid_for_authorization?(uri, client_uri)
164
+ end
165
+ end
166
+
167
+ describe '.query_matches?' do
168
+ it 'is true if no queries' do
169
+ expect(URIChecker.query_matches?('', '')).to be_truthy
170
+ expect(URIChecker.query_matches?(nil, nil)).to be_truthy
171
+ end
172
+
173
+ it 'is true if same query' do
174
+ expect(URIChecker.query_matches?('foo', 'foo')).to be_truthy
175
+ end
176
+
177
+ it 'is false if different query' do
178
+ expect(URIChecker.query_matches?('foo', 'bar')).to be_falsey
179
+ end
180
+
181
+ it 'is true if same queries' do
182
+ expect(URIChecker.query_matches?('foo&bar', 'foo&bar')).to be_truthy
183
+ end
184
+
185
+ it 'is true if same queries, different order' do
186
+ expect(URIChecker.query_matches?('foo&bar', 'bar&foo')).to be_truthy
187
+ end
188
+
189
+ it 'is false if one different query' do
190
+ expect(URIChecker.query_matches?('foo&bang', 'foo&bing')).to be_falsey
191
+ end
192
+
193
+ it 'is true if same query with same value' do
194
+ expect(URIChecker.query_matches?('foo=bar', 'foo=bar')).to be_truthy
195
+ end
196
+
197
+ it 'is true if same queries with same values' do
198
+ expect(URIChecker.query_matches?('foo=bar&bing=bang', 'foo=bar&bing=bang')).to be_truthy
199
+ end
200
+
201
+ it 'is true if same queries with same values, different order' do
202
+ expect(URIChecker.query_matches?('foo=bar&bing=bang', 'bing=bang&foo=bar')).to be_truthy
203
+ end
204
+
205
+ it 'is false if same query with different value' do
206
+ expect(URIChecker.query_matches?('foo=bar', 'foo=bang')).to be_falsey
207
+ end
208
+
209
+ it 'is false if some queries missing' do
210
+ expect(URIChecker.query_matches?('foo=bar', 'foo=bar&bing=bang')).to be_falsey
211
+ end
212
+
213
+ it 'is false if some queries different value' do
214
+ expect(URIChecker.query_matches?('foo=bar&bing=bang', 'foo=bar&bing=banana')).to be_falsey
215
+ end
107
216
  end
108
217
  end
109
218
  end
@@ -67,9 +67,9 @@ module Doorkeeper::OAuth
67
67
  end.to_not change { Doorkeeper::AccessToken.count }
68
68
  end
69
69
 
70
- it "calls BaseRequest callback methods" do
71
- expect_any_instance_of(BaseRequest).to receive(:before_successful_response).once
72
- expect_any_instance_of(BaseRequest).to receive(:after_successful_response).once
70
+ it "calls configured request callback methods" do
71
+ expect(Doorkeeper.configuration.before_successful_strategy_response).to receive(:call).with(subject).once
72
+ expect(Doorkeeper.configuration.after_successful_strategy_response).to receive(:call).with(subject, instance_of(Doorkeeper::OAuth::TokenResponse)).once
73
73
  subject.authorize
74
74
  end
75
75
 
@@ -123,14 +123,19 @@ module Doorkeeper::OAuth
123
123
  expect(subject.scopes).to eq(Scopes.from_string('default'))
124
124
  end
125
125
 
126
- it 'accepts test uri' do
127
- subject.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
128
- expect(subject).to be_authorizable
129
- end
126
+ context 'with native redirect uri' do
127
+ let(:native_redirect_uri) { 'urn:ietf:wg:oauth:2.0:oob' }
130
128
 
131
- it 'matches the redirect uri against client\'s one' do
132
- subject.redirect_uri = 'http://nothesame.com'
133
- expect(subject).not_to be_authorizable
129
+ it 'accepts redirect_uri when it matches with the client' do
130
+ subject.redirect_uri = native_redirect_uri
131
+ allow(subject.client).to receive(:redirect_uri) { native_redirect_uri }
132
+ expect(subject).to be_authorizable
133
+ end
134
+
135
+ it 'invalidates redirect_uri when it does\'n match with the client' do
136
+ subject.redirect_uri = native_redirect_uri
137
+ expect(subject).not_to be_authorizable
138
+ end
134
139
  end
135
140
 
136
141
  it 'stores the state' do
@@ -44,9 +44,9 @@ module Doorkeeper::OAuth
44
44
  expect { subject.authorize }.to change { refresh_token.revoked? }.from(false).to(true)
45
45
  end
46
46
 
47
- it "calls BaseRequest callback methods" do
48
- expect_any_instance_of(BaseRequest).to receive(:before_successful_response).once
49
- expect_any_instance_of(BaseRequest).to receive(:after_successful_response).once
47
+ it "calls configured request callback methods" do
48
+ expect(Doorkeeper.configuration.before_successful_strategy_response).to receive(:call).with(subject).once
49
+ expect(Doorkeeper.configuration.after_successful_strategy_response).to receive(:call).with(subject, instance_of(Doorkeeper::OAuth::TokenResponse)).once
50
50
  subject.authorize
51
51
  end
52
52
 
@@ -49,6 +49,11 @@ module Doorkeeper
49
49
  expect(new_application).not_to be_valid
50
50
  end
51
51
 
52
+ it 'is invalid without determining confidentiality' do
53
+ new_application.confidential = nil
54
+ expect(new_application).not_to be_valid
55
+ end
56
+
52
57
  it 'generates uid on create' do
53
58
  expect(new_application.uid).to be_nil
54
59
  new_application.save
@@ -201,11 +206,97 @@ module Doorkeeper
201
206
  end
202
207
  end
203
208
 
204
- describe :authenticate do
205
- it 'finds the application via uid/secret' do
206
- app = FactoryBot.create :application
207
- authenticated = Application.by_uid_and_secret(app.uid, app.secret)
208
- expect(authenticated).to eq(app)
209
+ describe :by_uid_and_secret do
210
+ context "when application is private/confidential" do
211
+ it "finds the application via uid/secret" do
212
+ app = FactoryBot.create :application
213
+ authenticated = Application.by_uid_and_secret(app.uid, app.secret)
214
+ expect(authenticated).to eq(app)
215
+ end
216
+ context "when secret is wrong" do
217
+ it "should not find the application" do
218
+ app = FactoryBot.create :application
219
+ authenticated = Application.by_uid_and_secret(app.uid, 'bad')
220
+ expect(authenticated).to eq(nil)
221
+ end
222
+ end
223
+ end
224
+
225
+ context "when application is public/non-confidential" do
226
+ context "when secret is blank" do
227
+ it "should find the application" do
228
+ app = FactoryBot.create :application, confidential: false
229
+ authenticated = Application.by_uid_and_secret(app.uid, nil)
230
+ expect(authenticated).to eq(app)
231
+ end
232
+ end
233
+ context "when secret is wrong" do
234
+ it "should not find the application" do
235
+ app = FactoryBot.create :application, confidential: false
236
+ authenticated = Application.by_uid_and_secret(app.uid, 'bad')
237
+ expect(authenticated).to eq(nil)
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ describe :confidential? do
244
+ subject { FactoryBot.create(:application, confidential: confidential).confidential? }
245
+
246
+ context 'when application is private/confidential' do
247
+ let(:confidential) { true }
248
+ it { expect(subject).to eq(true) }
249
+ end
250
+
251
+ context 'when application is public/non-confidential' do
252
+ let(:confidential) { false }
253
+ it { expect(subject).to eq(false) }
254
+ end
255
+ end
256
+
257
+ describe :confidential do
258
+ subject { FactoryBot.create(:application, confidential: confidential).confidential }
259
+
260
+ context 'when application is private/confidential' do
261
+ let(:confidential) { true }
262
+ it { expect(subject).to eq(true) }
263
+ end
264
+
265
+ context 'when application is public/non-confidential' do
266
+ let(:confidential) { false }
267
+ it { expect(subject).to eq(false) }
268
+ end
269
+
270
+ context 'when the application does not support confidentiality' do
271
+ let(:confidential) { false }
272
+
273
+ before { allow(Application).to receive(:supports_confidentiality?).and_return(false) }
274
+
275
+ it 'warns of the CVE' do
276
+ expect(ActiveSupport::Deprecation).to receive(:warn).with(
277
+ 'You are susceptible to security bug ' \
278
+ 'CVE-2018-1000211. Please follow instructions outlined in ' \
279
+ 'Doorkeeper::CVE_2018_1000211_WARNING'
280
+ )
281
+ Application.new.confidential
282
+ end
283
+
284
+ it { expect(subject).to eq(true) }
285
+ end
286
+ end
287
+
288
+ describe :supports_confidentiality? do
289
+ context 'when no column' do
290
+ it 'returns false' do
291
+ expect(Application).to receive(:column_names).and_return(%w[foo bar])
292
+ expect(Application.supports_confidentiality?).to eq(false)
293
+ end
294
+ end
295
+ context 'when column' do
296
+ it 'returns true' do
297
+ expect(Application).to receive(:column_names).and_return(%w[foo bar confidential])
298
+ expect(Application.supports_confidentiality?).to eq(true)
299
+ end
209
300
  end
210
301
  end
211
302
  end
@@ -53,7 +53,7 @@ feature 'Authorization Code Flow' do
53
53
  should_not_have_json 'error'
54
54
 
55
55
  should_have_json 'access_token', Doorkeeper::AccessToken.first.token
56
- should_have_json 'token_type', 'bearer'
56
+ should_have_json 'token_type', 'Bearer'
57
57
  should_have_json_within 'expires_in', Doorkeeper::AccessToken.first.expires_in, 1
58
58
  end
59
59
 
@@ -10,46 +10,89 @@ describe 'Resource Owner Password Credentials Flow not set up' do
10
10
  it 'doesn\'t issue new token' do
11
11
  expect do
12
12
  post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
13
- end.to_not change { Doorkeeper::AccessToken.count }
13
+ end.to_not(change { Doorkeeper::AccessToken.count })
14
14
  end
15
15
  end
16
16
  end
17
17
 
18
18
  describe 'Resource Owner Password Credentials Flow' do
19
+ let(:client_attributes) { {} }
20
+
19
21
  before do
20
22
  config_is_set(:grant_flows, ["password"])
21
23
  config_is_set(:resource_owner_from_credentials) { User.authenticate! params[:username], params[:password] }
22
- client_exists
24
+ client_exists(client_attributes)
23
25
  create_resource_owner
24
26
  end
25
27
 
26
28
  context 'with valid user credentials' do
27
- it 'should issue new token with confidential client' do
28
- expect do
29
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
30
- end.to change { Doorkeeper::AccessToken.count }.by(1)
29
+ context "with non-confidential/public client" do
30
+ let(:client_attributes) { { confidential: false } }
31
31
 
32
- token = Doorkeeper::AccessToken.first
32
+ context "when client_secret absent" do
33
+ it "should issue new token" do
34
+ expect do
35
+ post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
36
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
33
37
 
34
- expect(token.application_id).to eq @client.id
35
- should_have_json 'access_token', token.token
38
+ token = Doorkeeper::AccessToken.first
39
+
40
+ expect(token.application_id).to eq @client.id
41
+ should_have_json 'access_token', token.token
42
+ end
43
+ end
44
+
45
+ context "when client_secret present" do
46
+ it "should issue new token" do
47
+ expect do
48
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
49
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
50
+
51
+ token = Doorkeeper::AccessToken.first
52
+
53
+ expect(token.application_id).to eq @client.id
54
+ should_have_json 'access_token', token.token
55
+ end
56
+
57
+ context "when client_secret incorrect" do
58
+ it "should not issue new token" do
59
+ expect do
60
+ post password_token_endpoint_url(client_id: @client.uid, client_secret: 'foobar', resource_owner: @resource_owner)
61
+ end.not_to(change { Doorkeeper::AccessToken.count })
62
+
63
+ expect(response).not_to be_ok
64
+ end
65
+ end
66
+ end
36
67
  end
37
68
 
38
- it 'should issue new token with public client (only client_id present)' do
39
- expect do
40
- post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
41
- end.to change { Doorkeeper::AccessToken.count }.by(1)
69
+ context "with confidential/private client" do
70
+ it "should issue new token" do
71
+ expect do
72
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
73
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
42
74
 
43
- token = Doorkeeper::AccessToken.first
75
+ token = Doorkeeper::AccessToken.first
44
76
 
45
- expect(token.application_id).to eq @client.id
46
- should_have_json 'access_token', token.token
77
+ expect(token.application_id).to eq @client.id
78
+ should_have_json 'access_token', token.token
79
+ end
80
+
81
+ context "when client_secret absent" do
82
+ it "should not issue new token" do
83
+ expect do
84
+ post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
85
+ end.not_to(change { Doorkeeper::AccessToken.count })
86
+
87
+ expect(response).not_to be_ok
88
+ end
89
+ end
47
90
  end
48
91
 
49
92
  it 'should issue new token without client credentials' do
50
93
  expect do
51
94
  post password_token_endpoint_url(resource_owner: @resource_owner)
52
- end.to change { Doorkeeper::AccessToken.count }.by(1)
95
+ end.to(change { Doorkeeper::AccessToken.count }.by(1))
53
96
 
54
97
  token = Doorkeeper::AccessToken.first
55
98
 
@@ -124,13 +167,13 @@ describe 'Resource Owner Password Credentials Flow' do
124
167
  post password_token_endpoint_url(client: @client,
125
168
  resource_owner_username: @resource_owner.name,
126
169
  resource_owner_password: 'wrongpassword')
127
- end.to_not change { Doorkeeper::AccessToken.count }
170
+ end.to_not(change { Doorkeeper::AccessToken.count })
128
171
  end
129
172
 
130
173
  it 'should not issue new token without credentials' do
131
174
  expect do
132
175
  post password_token_endpoint_url(client: @client)
133
- end.to_not change { Doorkeeper::AccessToken.count }
176
+ end.to_not(change { Doorkeeper::AccessToken.count })
134
177
  end
135
178
  end
136
179
 
@@ -140,7 +183,7 @@ describe 'Resource Owner Password Credentials Flow' do
140
183
  post password_token_endpoint_url(client_id: @client.uid,
141
184
  client_secret: 'bad_secret',
142
185
  resource_owner: @resource_owner)
143
- end.to_not change { Doorkeeper::AccessToken.count }
186
+ end.to_not(change { Doorkeeper::AccessToken.count })
144
187
  end
145
188
  end
146
189
 
@@ -148,7 +191,7 @@ describe 'Resource Owner Password Credentials Flow' do
148
191
  it 'should not issue new token with bad client id' do
149
192
  expect do
150
193
  post password_token_endpoint_url(client_id: 'bad_id', resource_owner: @resource_owner)
151
- end.to_not change { Doorkeeper::AccessToken.count }
194
+ end.to_not(change { Doorkeeper::AccessToken.count })
152
195
  end
153
196
  end
154
197
  end