doorkeeper 5.1.2 → 5.2.2

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.

Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/Appraisals +1 -1
  3. data/CHANGELOG.md +854 -0
  4. data/CONTRIBUTING.md +11 -9
  5. data/Dangerfile +2 -2
  6. data/Dockerfile +29 -0
  7. data/Gemfile +3 -2
  8. data/NEWS.md +1 -819
  9. data/README.md +11 -3
  10. data/RELEASING.md +6 -5
  11. data/app/controllers/doorkeeper/application_controller.rb +1 -1
  12. data/app/controllers/doorkeeper/application_metal_controller.rb +2 -1
  13. data/app/controllers/doorkeeper/applications_controller.rb +5 -3
  14. data/app/controllers/doorkeeper/authorizations_controller.rb +14 -7
  15. data/app/controllers/doorkeeper/authorized_applications_controller.rb +1 -1
  16. data/app/controllers/doorkeeper/tokens_controller.rb +32 -9
  17. data/app/views/doorkeeper/applications/_form.html.erb +0 -6
  18. data/app/views/doorkeeper/applications/show.html.erb +1 -1
  19. data/config/locales/en.yml +8 -2
  20. data/doorkeeper.gemspec +9 -1
  21. data/gemfiles/rails_5_0.gemfile +1 -0
  22. data/gemfiles/rails_5_1.gemfile +1 -0
  23. data/gemfiles/rails_5_2.gemfile +1 -0
  24. data/gemfiles/rails_6_0.gemfile +2 -1
  25. data/gemfiles/rails_master.gemfile +1 -0
  26. data/lib/doorkeeper/config/option.rb +13 -7
  27. data/lib/doorkeeper/config.rb +88 -6
  28. data/lib/doorkeeper/errors.rb +13 -18
  29. data/lib/doorkeeper/grape/helpers.rb +5 -1
  30. data/lib/doorkeeper/helpers/controller.rb +23 -4
  31. data/lib/doorkeeper/models/access_token_mixin.rb +43 -2
  32. data/lib/doorkeeper/oauth/authorization/code.rb +11 -13
  33. data/lib/doorkeeper/oauth/authorization/token.rb +1 -1
  34. data/lib/doorkeeper/oauth/authorization_code_request.rb +18 -9
  35. data/lib/doorkeeper/oauth/base_request.rb +2 -0
  36. data/lib/doorkeeper/oauth/client_credentials/creator.rb +14 -0
  37. data/lib/doorkeeper/oauth/client_credentials/validation.rb +8 -0
  38. data/lib/doorkeeper/oauth/code_request.rb +5 -11
  39. data/lib/doorkeeper/oauth/code_response.rb +2 -2
  40. data/lib/doorkeeper/oauth/error_response.rb +1 -1
  41. data/lib/doorkeeper/oauth/helpers/uri_checker.rb +18 -4
  42. data/lib/doorkeeper/oauth/invalid_request_response.rb +43 -0
  43. data/lib/doorkeeper/oauth/nonstandard.rb +39 -0
  44. data/lib/doorkeeper/oauth/password_access_token_request.rb +7 -2
  45. data/lib/doorkeeper/oauth/pre_authorization.rb +70 -37
  46. data/lib/doorkeeper/oauth/refresh_token_request.rb +13 -10
  47. data/lib/doorkeeper/oauth/token_introspection.rb +23 -13
  48. data/lib/doorkeeper/oauth/token_request.rb +4 -18
  49. data/lib/doorkeeper/orm/active_record/access_grant.rb +1 -1
  50. data/lib/doorkeeper/orm/active_record/access_token.rb +2 -2
  51. data/lib/doorkeeper/orm/active_record/application.rb +15 -69
  52. data/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb +61 -0
  53. data/lib/doorkeeper/orm/active_record.rb +19 -3
  54. data/lib/doorkeeper/request/authorization_code.rb +2 -0
  55. data/lib/doorkeeper/request.rb +6 -11
  56. data/lib/doorkeeper/server.rb +2 -6
  57. data/lib/doorkeeper/stale_records_cleaner.rb +6 -2
  58. data/lib/doorkeeper/version.rb +1 -1
  59. data/lib/doorkeeper.rb +4 -0
  60. data/lib/generators/doorkeeper/previous_refresh_token_generator.rb +6 -6
  61. data/lib/generators/doorkeeper/templates/initializer.rb +110 -33
  62. data/lib/generators/doorkeeper/templates/migration.rb.erb +4 -1
  63. data/spec/controllers/applications_controller_spec.rb +93 -0
  64. data/spec/controllers/authorizations_controller_spec.rb +143 -62
  65. data/spec/controllers/protected_resources_controller_spec.rb +3 -3
  66. data/spec/controllers/tokens_controller_spec.rb +205 -37
  67. data/spec/dummy/config/application.rb +3 -1
  68. data/spec/dummy/config/initializers/doorkeeper.rb +54 -9
  69. data/spec/dummy/db/migrate/20151223192035_create_doorkeeper_tables.rb +1 -1
  70. data/spec/lib/config_spec.rb +43 -1
  71. data/spec/lib/oauth/authorization_code_request_spec.rb +13 -1
  72. data/spec/lib/oauth/base_request_spec.rb +33 -16
  73. data/spec/lib/oauth/client_credentials/creator_spec.rb +3 -0
  74. data/spec/lib/oauth/code_request_spec.rb +27 -28
  75. data/spec/lib/oauth/helpers/uri_checker_spec.rb +17 -2
  76. data/spec/lib/oauth/invalid_request_response_spec.rb +75 -0
  77. data/spec/lib/oauth/pre_authorization_spec.rb +76 -66
  78. data/spec/lib/oauth/refresh_token_request_spec.rb +1 -0
  79. data/spec/lib/oauth/token_request_spec.rb +20 -17
  80. data/spec/lib/server_spec.rb +0 -12
  81. data/spec/models/doorkeeper/access_grant_spec.rb +21 -2
  82. data/spec/models/doorkeeper/access_token_spec.rb +35 -4
  83. data/spec/models/doorkeeper/application_spec.rb +275 -370
  84. data/spec/requests/endpoints/authorization_spec.rb +21 -5
  85. data/spec/requests/endpoints/token_spec.rb +1 -1
  86. data/spec/requests/flows/authorization_code_errors_spec.rb +1 -0
  87. data/spec/requests/flows/authorization_code_spec.rb +93 -27
  88. data/spec/requests/flows/client_credentials_spec.rb +38 -0
  89. data/spec/requests/flows/implicit_grant_errors_spec.rb +22 -10
  90. data/spec/requests/flows/implicit_grant_spec.rb +9 -8
  91. data/spec/requests/flows/password_spec.rb +37 -0
  92. data/spec/requests/flows/refresh_token_spec.rb +1 -1
  93. data/spec/requests/flows/revoke_token_spec.rb +19 -11
  94. data/spec/support/doorkeeper_rspec.rb +1 -1
  95. data/spec/support/helpers/request_spec_helper.rb +14 -2
  96. data/spec/validators/redirect_uri_validator_spec.rb +40 -15
  97. metadata +16 -15
  98. data/.coveralls.yml +0 -1
  99. data/.github/ISSUE_TEMPLATE.md +0 -25
  100. data/.github/PULL_REQUEST_TEMPLATE.md +0 -17
  101. data/.gitignore +0 -20
  102. data/.gitlab-ci.yml +0 -16
  103. data/.hound.yml +0 -3
  104. data/.rspec +0 -1
  105. data/.rubocop.yml +0 -50
  106. data/.travis.yml +0 -35
  107. data/app/validators/redirect_uri_validator.rb +0 -50
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Doorkeeper.configure do
4
- # Change the ORM that doorkeeper will use (needs plugins)
4
+ # Change the ORM that doorkeeper will use (requires ORM extensions installed).
5
+ # Check the list of supported ORMs here: https://github.com/doorkeeper-gem/doorkeeper#orms
5
6
  orm :active_record
6
7
 
7
8
  # This block will be called to check whether the resource owner is authenticated or not.
@@ -9,7 +10,7 @@ Doorkeeper.configure do
9
10
  raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
10
11
  # Put your resource owner authentication logic here.
11
12
  # Example implementation:
12
- # User.find_by_id(session[:user_id]) || redirect_to(new_user_session_url)
13
+ # User.find_by(id: session[:user_id]) || redirect_to(new_user_session_url)
13
14
  end
14
15
 
15
16
  # If you didn't skip applications controller from Doorkeeper routes in your application routes.rb
@@ -39,18 +40,18 @@ Doorkeeper.configure do
39
40
  #
40
41
  # enforce_content_type
41
42
 
42
- # Authorization Code expiration time (default 10 minutes).
43
+ # Authorization Code expiration time (default: 10 minutes).
43
44
  #
44
45
  # authorization_code_expires_in 10.minutes
45
46
 
46
- # Access token expiration time (default 2 hours).
47
- # If you want to disable expiration, set this to nil.
47
+ # Access token expiration time (default: 2 hours).
48
+ # If you want to disable expiration, set this to `nil`.
48
49
  #
49
50
  # access_token_expires_in 2.hours
50
51
 
51
52
  # Assign custom TTL for access tokens. Will be used instead of access_token_expires_in
52
53
  # option if defined. In case the block returns `nil` value Doorkeeper fallbacks to
53
- # `access_token_expires_in` configuration option value. If you really need to issue a
54
+ # +access_token_expires_in+ configuration option value. If you really need to issue a
54
55
  # non-expiring access token (which is not recommended) then you need to return
55
56
  # Float::INFINITY from this block.
56
57
  #
@@ -65,12 +66,13 @@ Doorkeeper.configure do
65
66
  # end
66
67
 
67
68
  # Use a custom class for generating the access token.
68
- # See https://github.com/doorkeeper-gem/doorkeeper#custom-access-token-generator
69
+ # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-access-token-generator
69
70
  #
70
71
  # access_token_generator '::Doorkeeper::JWT'
71
72
 
72
- # The controller Doorkeeper::ApplicationController inherits from.
73
- # Defaults to ActionController::Base.
73
+ # The controller +Doorkeeper::ApplicationController+ inherits from.
74
+ # Defaults to +ActionController::Base+ unless +api_only+ is set, which changes the default to
75
+ # +ActionController::API+. The return value of this option must be a stringified class name.
74
76
  # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-base-controller
75
77
  #
76
78
  # base_controller 'ApplicationController'
@@ -128,11 +130,10 @@ Doorkeeper.configure do
128
130
  #
129
131
  # hash_application_secrets using: '::Doorkeeper::SecretStoring::BCrypt'
130
132
 
131
- # When the above option is enabled,
132
- # and a hashed token or secret is not found,
133
- # you can allow to fall back to another strategy.
134
- # For users upgrading doorkeeper and wishing to enable hashing,
135
- # you will probably want to enable the fallback to plain tokens.
133
+ # When the above option is enabled, and a hashed token or secret is not found,
134
+ # you can allow to fall back to another strategy. For users upgrading
135
+ # doorkeeper and wishing to enable hashing, you will probably want to enable
136
+ # the fallback to plain tokens.
136
137
  #
137
138
  # This will ensure that old access tokens and secrets
138
139
  # will remain valid even if the hashing above is enabled.
@@ -141,8 +142,8 @@ Doorkeeper.configure do
141
142
 
142
143
  # Issue access tokens with refresh token (disabled by default), you may also
143
144
  # pass a block which accepts `context` to customize when to give a refresh
144
- # token or not. Similar to `custom_access_token_expires_in`, `context` has
145
- # the properties:
145
+ # token or not. Similar to +custom_access_token_expires_in+, `context` has
146
+ # the following properties:
146
147
  #
147
148
  # `client` - the OAuth client application (see Doorkeeper::OAuth::Client)
148
149
  # `grant_type` - the grant type of the request (see Doorkeeper::OAuth)
@@ -151,7 +152,7 @@ Doorkeeper.configure do
151
152
  # use_refresh_token
152
153
 
153
154
  # Provide support for an owner to be assigned to each registered application (disabled by default)
154
- # Optional parameter confirmation: true (default false) if you want to enforce ownership of
155
+ # Optional parameter confirmation: true (default: false) if you want to enforce ownership of
155
156
  # a registered application
156
157
  # NOTE: you must also run the rails g doorkeeper:application_owner generator
157
158
  # to provide the necessary support
@@ -160,22 +161,22 @@ Doorkeeper.configure do
160
161
 
161
162
  # Define access token scopes for your provider
162
163
  # For more information go to
163
- # https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
164
+ # https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes
164
165
  #
165
166
  # default_scopes :public
166
167
  # optional_scopes :write, :update
167
168
 
168
- # Define scopes_by_grant_type to restrict only certain scopes for grant_type
169
+ # Allows to restrict only certain scopes for grant_type.
169
170
  # By default, all the scopes will be available for all the grant types.
170
171
  #
171
172
  # Keys to this hash should be the name of grant_type and
172
173
  # values should be the array of scopes for that grant type.
173
- # Note: scopes should be from configured_scopes(i.e. deafult or optional)
174
+ # Note: scopes should be from configured_scopes (i.e. default or optional)
174
175
  #
175
176
  # scopes_by_grant_type password: [:write], client_credentials: [:update]
176
177
 
177
178
  # Forbids creating/updating applications with arbitrary scopes that are
178
- # not in configuration, i.e. `default_scopes` or `optional_scopes`.
179
+ # not in configuration, i.e. +default_scopes+ or +optional_scopes+.
179
180
  # (disabled by default)
180
181
  #
181
182
  # enforce_configured_scopes
@@ -196,15 +197,6 @@ Doorkeeper.configure do
196
197
  #
197
198
  # access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param
198
199
 
199
- # Change the native redirect uri for client apps
200
- # When clients register with the following redirect uri, they won't be redirected to
201
- # any server and the authorizationcode will be displayed within the provider
202
- # The value can be any string. Use nil to disable this feature. When disabled, clients
203
- # must providea valid URL
204
- # (Similar behaviour: https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi)
205
- #
206
- # native_redirect_uri 'urn:ietf:wg:oauth:2.0:oob'
207
-
208
200
  # Forces the usage of the HTTPS protocol in non-native redirect uris (enabled
209
201
  # by default in non-development environments). OAuth2 delegates security in
210
202
  # communication to the HTTPS protocol so it is wise to keep this enabled.
@@ -246,7 +238,7 @@ Doorkeeper.configure do
246
238
  # is invalid, expired, revoked or has invalid scopes.
247
239
  #
248
240
  # If you want to render error response yourself (i.e. rescue exceptions),
249
- # set handle_auth_errors to `:raise` and rescue Doorkeeper::Errors::InvalidToken
241
+ # set +handle_auth_errors+ to `:raise` and rescue Doorkeeper::Errors::InvalidToken
250
242
  # or following specific errors:
251
243
  #
252
244
  # Doorkeeper::Errors::TokenForbidden, Doorkeeper::Errors::TokenExpired,
@@ -290,6 +282,37 @@ Doorkeeper.configure do
290
282
  #
291
283
  # grant_flows %w[authorization_code client_credentials]
292
284
 
285
+ # Allows to customize OAuth grant flows that +each+ application support.
286
+ # You can configure a custom block (or use a class respond to `#call`) that must
287
+ # return `true` in case Application instance supports requested OAuth grant flow
288
+ # during the authorization request to the server. This configuration +doesn't+
289
+ # set flows per application, it only allows to check if application supports
290
+ # specific grant flow.
291
+ #
292
+ # For example you can add an additional database column to `oauth_applications` table,
293
+ # say `t.array :grant_flows, default: []`, and store allowed grant flows that can
294
+ # be used with this application there. Then when authorization requested Doorkeeper
295
+ # will call this block to check if specific Application (passed with client_id and/or
296
+ # client_secret) is allowed to perform the request for the specific grant type
297
+ # (authorization, password, client_credentials, etc).
298
+ #
299
+ # Example of the block:
300
+ #
301
+ # ->(flow, client) { client.grant_flows.include?(flow) }
302
+ #
303
+ # In case this option invocation result is `false`, Doorkeeper server returns
304
+ # :unauthorized_client error and stops the request.
305
+ #
306
+ # @param allow_grant_flow_for_client [Proc] Block or any object respond to #call
307
+ # @return [Boolean] `true` if allow or `false` if forbid the request
308
+ #
309
+ # allow_grant_flow_for_client do |grant_flow, client|
310
+ # # `grant_flows` is an Array column with grant
311
+ # # flows that application supports
312
+ #
313
+ # client.grant_flows.include?(grant_flow)
314
+ # end
315
+
293
316
  # Hook into the strategies' request & response life-cycle in case your
294
317
  # application needs advanced customization or logging:
295
318
  #
@@ -305,7 +328,7 @@ Doorkeeper.configure do
305
328
  # or add any other functionality.
306
329
  #
307
330
  # before_successful_authorization do |controller|
308
- # Rails.logger.info(params.inspect)
331
+ # Rails.logger.info(controller.request.params.inspect)
309
332
  # end
310
333
  #
311
334
  # after_successful_authorization do |controller|
@@ -323,7 +346,61 @@ Doorkeeper.configure do
323
346
  # client.superapp? or resource_owner.admin?
324
347
  # end
325
348
 
326
- # WWW-Authenticate Realm (default "Doorkeeper").
349
+ # Configure custom constraints for the Token Introspection request.
350
+ # By default this configuration option allows to introspect a token by another
351
+ # token of the same application, OR to introspect the token that belongs to
352
+ # authorized client (from authenticated client) OR when token doesn't
353
+ # belong to any client (public token). Otherwise requester has no access to the
354
+ # introspection and it will return response as stated in the RFC.
355
+ #
356
+ # Block arguments:
357
+ #
358
+ # @param token [Doorkeeper::AccessToken]
359
+ # token to be introspected
360
+ #
361
+ # @param authorized_client [Doorkeeper::Application]
362
+ # authorized client (if request is authorized using Basic auth with
363
+ # Client Credentials for example)
364
+ #
365
+ # @param authorized_token [Doorkeeper::AccessToken]
366
+ # Bearer token used to authorize the request
367
+ #
368
+ # In case the block returns `nil` or `false` introspection responses with 401 status code
369
+ # when using authorized token to introspect, or you'll get 200 with { "active": false } body
370
+ # when using authorized client to introspect as stated in the
371
+ # RFC 7662 section 2.2. Introspection Response.
372
+ #
373
+ # Using with caution:
374
+ # Keep in mind that these three parameters pass to block can be nil as following case:
375
+ # `authorized_client` is nil if and only if `authorized_token` is present, and vice versa.
376
+ # `token` will be nil if and only if `authorized_token` is present.
377
+ # So remember to use `&` or check if it is present before calling method on
378
+ # them to make sure you doesn't get NoMethodError exception.
379
+ #
380
+ # You can define your custom check:
381
+ #
382
+ # allow_token_introspection do |token, authorized_client, authorized_token|
383
+ # if authorized_token
384
+ # # customize: require `introspection` scope
385
+ # authorized_token.application == token&.application ||
386
+ # authorized_token.scopes.include?("introspection")
387
+ # elsif token.application
388
+ # # `protected_resource` is a new database boolean column, for example
389
+ # authorized_client == token.application || authorized_client.protected_resource?
390
+ # else
391
+ # # public token (when token.application is nil, token doesn't belong to any application)
392
+ # true
393
+ # end
394
+ # end
395
+ #
396
+ # Or you can completely disable any token introspection:
397
+ #
398
+ # allow_token_introspection false
399
+ #
400
+ # If you need to block the request at all, then configure your routes.rb or web-server
401
+ # like nginx to forbid the request.
402
+
403
+ # WWW-Authenticate Realm (default: "Doorkeeper").
327
404
  #
328
405
  # realm "Doorkeeper"
329
406
  end
@@ -24,7 +24,7 @@ class CreateDoorkeeperTables < ActiveRecord::Migration<%= migration_version %>
24
24
  t.text :redirect_uri, null: false
25
25
  t.datetime :created_at, null: false
26
26
  t.datetime :revoked_at
27
- t.string :scopes
27
+ t.string :scopes, null: false, default: ''
28
28
  end
29
29
 
30
30
  add_index :oauth_access_grants, :token, unique: true
@@ -36,6 +36,9 @@ class CreateDoorkeeperTables < ActiveRecord::Migration<%= migration_version %>
36
36
 
37
37
  create_table :oauth_access_tokens do |t|
38
38
  t.references :resource_owner, index: true
39
+
40
+ # Remove `null: false` if you are planning to use Password
41
+ # Credentials Grant flow that doesn't require an application.
39
42
  t.references :application, null: false
40
43
 
41
44
  # If you use a custom token generator you may need to change this column
@@ -26,6 +26,10 @@ module Doorkeeper
26
26
 
27
27
  expect(json_response).to include("id", "name", "uid", "secret", "redirect_uri", "scopes")
28
28
 
29
+ application = Application.last
30
+ secret_from_response = json_response["secret"]
31
+ expect(application.secret_matches?(secret_from_response)).to be_truthy
32
+
29
33
  expect(json_response["name"]).to eq("Example")
30
34
  expect(json_response["redirect_uri"]).to eq("https://example.com")
31
35
  end
@@ -44,6 +48,21 @@ module Doorkeeper
44
48
  expect(json_response).to include("errors")
45
49
  end
46
50
 
51
+ it "returns validations on wrong create params (unspecified scheme)" do
52
+ expect do
53
+ post :create, params: {
54
+ doorkeeper_application: {
55
+ name: "Example",
56
+ redirect_uri: "app.com:80",
57
+ }, format: :json,
58
+ }
59
+ end.not_to(change { Doorkeeper::Application.count })
60
+
61
+ expect(response).to have_http_status(422)
62
+
63
+ expect(json_response).to include("errors")
64
+ end
65
+
47
66
  it "returns application info" do
48
67
  application = FactoryBot.create(:application, name: "Change me")
49
68
 
@@ -121,6 +140,72 @@ module Doorkeeper
121
140
  end
122
141
 
123
142
  context "when admin is authenticated" do
143
+ context "when application secrets are hashed" do
144
+ before do
145
+ allow(Doorkeeper.configuration).to receive(:application_secret_strategy).and_return(Doorkeeper::SecretStoring::Sha256Hash)
146
+ end
147
+
148
+ it "shows the application secret after creating a new application" do
149
+ expect do
150
+ post :create, params: {
151
+ doorkeeper_application: {
152
+ name: "Example",
153
+ redirect_uri: "https://example.com",
154
+ },
155
+ }
156
+ end.to change { Doorkeeper::Application.count }.by(1)
157
+
158
+ application = Application.last
159
+
160
+ secret_from_flash = flash[:application_secret]
161
+ expect(secret_from_flash).not_to be_empty
162
+ expect(application.secret_matches?(secret_from_flash)).to be_truthy
163
+ expect(response).to redirect_to(controller.main_app.oauth_application_url(application.id))
164
+
165
+ get :show, params: { id: application.id, format: :html }
166
+
167
+ # We don't know the application secret here (because its hashed) so we can not assert its text on the page
168
+ # Instead, we read it from the page and then check if it matches the application secret
169
+ code_element = %r{<code.*id="secret".*>(.*)<\/code>}.match(response.body)
170
+ secret_from_page = code_element[1]
171
+
172
+ expect(response.body).to have_selector("code#application_id", text: application.uid)
173
+ expect(response.body).to have_selector("code#secret")
174
+ expect(secret_from_page).not_to be_empty
175
+ expect(application.secret_matches?(secret_from_page)).to be_truthy
176
+ end
177
+
178
+ it "does not show an application secret when application did already exist" do
179
+ application = FactoryBot.create(:application)
180
+ get :show, params: { id: application.id, format: :html }
181
+
182
+ expect(response.body).to have_selector("code#application_id", text: application.uid)
183
+ expect(response.body).to have_selector("code#secret", text: "")
184
+ end
185
+
186
+ it "returns the application details in a json response" do
187
+ expect do
188
+ post :create, params: {
189
+ doorkeeper_application: {
190
+ name: "Example",
191
+ redirect_uri: "https://example.com",
192
+ }, format: :json,
193
+ }
194
+ end.to(change { Doorkeeper::Application.count })
195
+
196
+ expect(response).to be_successful
197
+
198
+ expect(json_response).to include("id", "name", "uid", "secret", "redirect_uri", "scopes")
199
+
200
+ application = Application.last
201
+ secret_from_response = json_response["secret"]
202
+ expect(application.secret_matches?(secret_from_response)).to be_truthy
203
+
204
+ expect(json_response["name"]).to eq("Example")
205
+ expect(json_response["redirect_uri"]).to eq("https://example.com")
206
+ end
207
+ end
208
+
124
209
  render_views
125
210
 
126
211
  before do
@@ -151,6 +236,14 @@ module Doorkeeper
151
236
  expect(response).to be_redirect
152
237
  end
153
238
 
239
+ it "shows application details" do
240
+ application = FactoryBot.create(:application)
241
+ get :show, params: { id: application.id, format: :html }
242
+
243
+ expect(response.body).to have_selector("code#application_id", text: application.uid)
244
+ expect(response.body).to have_selector("code#secret", text: application.plaintext_secret)
245
+ end
246
+
154
247
  it "does not allow mass assignment of uid or secret" do
155
248
  application = FactoryBot.create(:application)
156
249
  put :update, params: {
@@ -14,23 +14,23 @@ describe Doorkeeper::AuthorizationsController, "implicit grant flow" do
14
14
  end
15
15
  end
16
16
 
17
- def translated_error_message(key)
18
- I18n.translate key, scope: %i[doorkeeper errors messages]
19
- end
20
-
21
17
  let(:client) { FactoryBot.create :application }
22
18
  let(:user) { User.create!(name: "Joe", password: "sekret") }
23
- let(:access_token) { FactoryBot.build :access_token, resource_owner_id: user.id, application_id: client.id }
19
+ let(:access_token) { FactoryBot.build :access_token, resource_owner_id: user.id, application_id: client.id, scopes: "default" }
24
20
 
25
21
  before do
26
22
  Doorkeeper.configure do
23
+ default_scopes :default
24
+
27
25
  custom_access_token_expires_in(lambda do |context|
28
26
  context.grant_type == Doorkeeper::OAuth::IMPLICIT ? 1234 : nil
29
27
  end)
30
28
  end
31
29
 
32
30
  allow(Doorkeeper.configuration).to receive(:grant_flows).and_return(["implicit"])
33
- allow(controller).to receive(:current_resource_owner).and_return(user)
31
+ allow(Doorkeeper.configuration).to receive(:authenticate_resource_owner).and_return(->(_) { authenticator_method })
32
+ allow(controller).to receive(:authenticator_method).and_return(user)
33
+ expect(controller).to receive(:authenticator_method).at_most(:once)
34
34
  end
35
35
 
36
36
  describe "POST #create" do
@@ -106,80 +106,146 @@ describe Doorkeeper::AuthorizationsController, "implicit grant flow" do
106
106
  end
107
107
 
108
108
  describe "POST #create with errors" do
109
- before do
110
- default_scopes_exist :public
109
+ context "when missing client_id" do
110
+ before do
111
+ post :create, params: {
112
+ client_id: "",
113
+ response_type: "token",
114
+ redirect_uri: client.redirect_uri,
115
+ }
116
+ end
111
117
 
112
- post :create, params: {
113
- client_id: client.uid,
114
- response_type: "token",
115
- scope: "invalid",
116
- redirect_uri: client.redirect_uri,
117
- }
118
- end
118
+ let(:response_json_body) { JSON.parse(response.body) }
119
119
 
120
- it "redirects after authorization" do
121
- expect(response).to be_redirect
122
- end
120
+ it "renders 400 error" do
121
+ expect(response.status).to eq 400
122
+ end
123
123
 
124
- it "redirects to client redirect uri" do
125
- expect(response.location).to match(/^#{client.redirect_uri}/)
126
- end
124
+ it "includes error name" do
125
+ expect(response_json_body["error"]).to eq("invalid_request")
126
+ end
127
127
 
128
- it "does not include access token in fragment" do
129
- expect(response.query_params["access_token"]).to be_nil
130
- end
128
+ it "includes error description" do
129
+ expect(response_json_body["error_description"]).to eq(
130
+ translated_invalid_request_error_message(:missing_param, :client_id)
131
+ )
132
+ end
131
133
 
132
- it "includes error in fragment" do
133
- expect(response.query_params["error"]).to eq("invalid_scope")
134
+ it "does not issue any access token" do
135
+ expect(Doorkeeper::AccessToken.all).to be_empty
136
+ end
134
137
  end
135
138
 
136
- it "includes error description in fragment" do
137
- expect(response.query_params["error_description"]).to eq(translated_error_message(:invalid_scope))
138
- end
139
+ context "when other error happens" do
140
+ before do
141
+ default_scopes_exist :public
142
+
143
+ post :create, params: {
144
+ client_id: client.uid,
145
+ response_type: "token",
146
+ scope: "invalid",
147
+ redirect_uri: client.redirect_uri,
148
+ }
149
+ end
150
+
151
+ it "redirects after authorization" do
152
+ expect(response).to be_redirect
153
+ end
154
+
155
+ it "redirects to client redirect uri" do
156
+ expect(response.location).to match(/^#{client.redirect_uri}/)
157
+ end
158
+
159
+ it "does not include access token in fragment" do
160
+ expect(response.query_params["access_token"]).to be_nil
161
+ end
162
+
163
+ it "includes error in fragment" do
164
+ expect(response.query_params["error"]).to eq("invalid_scope")
165
+ end
166
+
167
+ it "includes error description in fragment" do
168
+ expect(response.query_params["error_description"]).to eq(translated_error_message(:invalid_scope))
169
+ end
139
170
 
140
- it "does not issue any access token" do
141
- expect(Doorkeeper::AccessToken.all).to be_empty
171
+ it "does not issue any access token" do
172
+ expect(Doorkeeper::AccessToken.all).to be_empty
173
+ end
142
174
  end
143
175
  end
144
176
 
145
177
  describe "POST #create in API mode with errors" do
146
- before do
147
- allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
148
- default_scopes_exist :public
178
+ context "when missing client_id" do
179
+ before do
180
+ allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
149
181
 
150
- post :create, params: {
151
- client_id: client.uid,
152
- response_type: "token",
153
- scope: "invalid",
154
- redirect_uri: client.redirect_uri,
155
- }
156
- end
182
+ post :create, params: {
183
+ client_id: "",
184
+ response_type: "token",
185
+ redirect_uri: client.redirect_uri,
186
+ }
187
+ end
157
188
 
158
- let(:response_json_body) { JSON.parse(response.body) }
159
- let(:redirect_uri) { response_json_body["redirect_uri"] }
189
+ let(:response_json_body) { JSON.parse(response.body) }
160
190
 
161
- it "renders 400 error" do
162
- expect(response.status).to eq 400
163
- end
191
+ it "renders 400 error" do
192
+ expect(response.status).to eq 400
193
+ end
164
194
 
165
- it "includes correct redirect URI" do
166
- expect(redirect_uri).to match(/^#{client.redirect_uri}/)
167
- end
195
+ it "includes error name" do
196
+ expect(response_json_body["error"]).to eq("invalid_request")
197
+ end
168
198
 
169
- it "does not include access token in fragment" do
170
- expect(redirect_uri.match(/access_token=([a-f0-9]+)&?/)).to be_nil
171
- end
199
+ it "includes error description" do
200
+ expect(response_json_body["error_description"]).to eq(
201
+ translated_invalid_request_error_message(:missing_param, :client_id)
202
+ )
203
+ end
172
204
 
173
- it "includes error in redirect uri" do
174
- expect(redirect_uri.match(/error=([a-z_]+)&?/)[1]).to eq "invalid_scope"
205
+ it "does not issue any access token" do
206
+ expect(Doorkeeper::AccessToken.all).to be_empty
207
+ end
175
208
  end
176
209
 
177
- it "includes error description in redirect uri" do
178
- expect(redirect_uri.match(/error_description=(.+)&?/)[1]).to_not be_nil
179
- end
210
+ context "when other error happens" do
211
+ before do
212
+ allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
213
+ default_scopes_exist :public
180
214
 
181
- it "does not issue any access token" do
182
- expect(Doorkeeper::AccessToken.all).to be_empty
215
+ post :create, params: {
216
+ client_id: client.uid,
217
+ response_type: "token",
218
+ scope: "invalid",
219
+ redirect_uri: client.redirect_uri,
220
+ }
221
+ end
222
+
223
+ let(:response_json_body) { JSON.parse(response.body) }
224
+ let(:redirect_uri) { response_json_body["redirect_uri"] }
225
+
226
+ it "renders 400 error" do
227
+ expect(response.status).to eq 400
228
+ end
229
+
230
+ it "includes correct redirect URI" do
231
+ expect(redirect_uri).to match(/^#{client.redirect_uri}/)
232
+ end
233
+
234
+ it "does not include access token in fragment" do
235
+ expect(redirect_uri.match(/access_token=([a-f0-9]+)&?/)).to be_nil
236
+ end
237
+
238
+ it "includes error in redirect uri" do
239
+ expect(redirect_uri.match(/error=([a-z_]+)&?/)[1]).to eq "invalid_scope"
240
+ end
241
+
242
+ it "includes error description in redirect uri" do
243
+ expect(redirect_uri.match(/error_description=(.+)&?/)[1]).to_not be_nil
244
+ end
245
+
246
+ it "does not issue any access token" do
247
+ expect(Doorkeeper::AccessToken.all).to be_empty
248
+ end
183
249
  end
184
250
  end
185
251
 
@@ -368,7 +434,7 @@ describe Doorkeeper::AuthorizationsController, "implicit grant flow" do
368
434
  expect(json_response["redirect_uri"]).to eq(client.redirect_uri)
369
435
  expect(json_response["state"]).to be_nil
370
436
  expect(json_response["response_type"]).to eq("token")
371
- expect(json_response["scope"]).to eq("")
437
+ expect(json_response["scope"]).to eq("default")
372
438
  end
373
439
  end
374
440
 
@@ -445,12 +511,12 @@ describe Doorkeeper::AuthorizationsController, "implicit grant flow" do
445
511
  end
446
512
 
447
513
  it "includes error in body" do
448
- expect(response_json_body["error"]).to eq("unsupported_response_type")
514
+ expect(response_json_body["error"]).to eq("invalid_request")
449
515
  end
450
516
 
451
517
  it "includes error description in body" do
452
518
  expect(response_json_body["error_description"])
453
- .to eq(translated_error_message(:unsupported_response_type))
519
+ .to eq(translated_invalid_request_error_message(:missing_param, :client_id))
454
520
  end
455
521
 
456
522
  it "does not issue any token" do
@@ -513,6 +579,8 @@ describe Doorkeeper::AuthorizationsController, "implicit grant flow" do
513
579
 
514
580
  describe "authorize response memoization" do
515
581
  it "memoizes the result of the authorization" do
582
+ pre_auth = double(:pre_auth, authorizable?: true)
583
+ allow(controller).to receive(:pre_auth) { pre_auth }
516
584
  strategy = double(:strategy, authorize: true)
517
585
  expect(strategy).to receive(:authorize).once
518
586
  allow(controller).to receive(:strategy) { strategy }
@@ -524,4 +592,17 @@ describe Doorkeeper::AuthorizationsController, "implicit grant flow" do
524
592
  post :create
525
593
  end
526
594
  end
595
+
596
+ describe "strong parameters" do
597
+ it "ignores non-scalar scope parameter" do
598
+ get :new, params: {
599
+ client_id: client.uid,
600
+ response_type: "token",
601
+ redirect_uri: client.redirect_uri,
602
+ scope: { "0" => "profile" },
603
+ }
604
+
605
+ expect(response).to be_successful
606
+ end
607
+ end
527
608
  end