doorkeeper 5.8.2 → 5.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +71 -3
  3. data/README.md +2 -3
  4. data/app/controllers/doorkeeper/authorizations_controller.rb +38 -4
  5. data/app/controllers/doorkeeper/authorized_applications_controller.rb +23 -0
  6. data/app/controllers/doorkeeper/tokens_controller.rb +7 -9
  7. data/app/views/doorkeeper/applications/show.html.erb +5 -5
  8. data/app/views/doorkeeper/authorizations/new.html.erb +1 -1
  9. data/app/views/doorkeeper/authorizations/show.html.erb +1 -1
  10. data/config/locales/en.yml +12 -11
  11. data/lib/doorkeeper/config/validations.rb +2 -2
  12. data/lib/doorkeeper/config.rb +31 -8
  13. data/lib/doorkeeper/engine.rb +2 -4
  14. data/lib/doorkeeper/errors.rb +15 -2
  15. data/lib/doorkeeper/helpers/controller.rb +7 -4
  16. data/lib/doorkeeper/models/access_grant_mixin.rb +10 -6
  17. data/lib/doorkeeper/models/access_token_mixin.rb +30 -13
  18. data/lib/doorkeeper/models/concerns/expiration_time_sql_math.rb +8 -0
  19. data/lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb +0 -1
  20. data/lib/doorkeeper/models/concerns/revocable.rb +8 -1
  21. data/lib/doorkeeper/models/concerns/write_to_primary.rb +57 -0
  22. data/lib/doorkeeper/oauth/authorization/code.rb +3 -1
  23. data/lib/doorkeeper/oauth/authorization_code_request.rb +1 -0
  24. data/lib/doorkeeper/oauth/client/credentials.rb +58 -6
  25. data/lib/doorkeeper/oauth/client_credentials/creator.rb +7 -3
  26. data/lib/doorkeeper/oauth/client_credentials/issuer.rb +1 -1
  27. data/lib/doorkeeper/oauth/client_credentials_request.rb +1 -0
  28. data/lib/doorkeeper/oauth/error_response.rb +21 -1
  29. data/lib/doorkeeper/oauth/forbidden_token_response.rb +1 -7
  30. data/lib/doorkeeper/oauth/helpers/uri_checker.rb +14 -0
  31. data/lib/doorkeeper/oauth/pre_authorization.rb +27 -0
  32. data/lib/doorkeeper/oauth/refresh_token_request.rb +19 -7
  33. data/lib/doorkeeper/oauth/scopes.rb +3 -2
  34. data/lib/doorkeeper/oauth/token.rb +95 -2
  35. data/lib/doorkeeper/oauth/token_introspection.rb +7 -3
  36. data/lib/doorkeeper/orm/active_record/mixins/access_grant.rb +1 -0
  37. data/lib/doorkeeper/orm/active_record/mixins/access_token.rb +25 -7
  38. data/lib/doorkeeper/orm/active_record/mixins/application.rb +6 -0
  39. data/lib/doorkeeper/orm/active_record/redirect_uri_validator.rb +5 -1
  40. data/lib/doorkeeper/orm/active_record/stale_records_cleaner.rb +10 -2
  41. data/lib/doorkeeper/orm/active_record.rb +8 -12
  42. data/lib/doorkeeper/rails/routes.rb +1 -1
  43. data/lib/doorkeeper/request.rb +19 -7
  44. data/lib/doorkeeper/version.rb +2 -2
  45. data/lib/doorkeeper.rb +14 -0
  46. data/lib/generators/doorkeeper/remove_applications_secret_not_null_constraint_generator.rb +2 -2
  47. data/lib/generators/doorkeeper/templates/initializer.rb +28 -2
  48. metadata +5 -11
@@ -102,14 +102,18 @@ module Doorkeeper
102
102
 
103
103
  # 2.2. Introspection Response
104
104
  def success_response
105
- customize_response(
105
+ response = {
106
106
  active: true,
107
107
  scope: @token.scopes_string,
108
108
  client_id: @token.try(:application).try(:uid),
109
109
  token_type: @token.token_type,
110
- exp: @token.expires_at.to_i,
111
110
  iat: @token.created_at.to_i,
112
- )
111
+ }
112
+ # `exp` is OPTIONAL per RFC 7662 §2.2; omit it for non-expiring tokens
113
+ # so clients don't interpret `0` as "expired at 1970-01-01".
114
+ response[:exp] = @token.expires_at.to_i if @token.expires_at
115
+
116
+ customize_response(response)
113
117
  end
114
118
 
115
119
  # If the introspection call is properly authorized but the token is not
@@ -9,6 +9,7 @@ module Doorkeeper::Orm::ActiveRecord::Mixins
9
9
  self.strict_loading_by_default = false if respond_to?(:strict_loading_by_default)
10
10
 
11
11
  include ::Doorkeeper::AccessGrantMixin
12
+ include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessGrant
12
13
 
13
14
  belongs_to :application, class_name: Doorkeeper.config.application_class.to_s,
14
15
  optional: true,
@@ -9,6 +9,7 @@ module Doorkeeper::Orm::ActiveRecord::Mixins
9
9
  self.strict_loading_by_default = false if respond_to?(:strict_loading_by_default)
10
10
 
11
11
  include ::Doorkeeper::AccessTokenMixin
12
+ include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessToken
12
13
 
13
14
  belongs_to :application, class_name: Doorkeeper.config.application_class.to_s,
14
15
  inverse_of: :access_tokens,
@@ -40,6 +41,28 @@ module Doorkeeper::Orm::ActiveRecord::Mixins
40
41
  by_resource_owner(resource_owner).where(revoked_at: nil)
41
42
  end
42
43
 
44
+ # Determines if refresh tokens should be revoked only when the new access token is used,
45
+ # rather than immediately upon refresh. This is based on the presence of the
46
+ # `previous_refresh_token` column in the database.
47
+ #
48
+ # When true (column exists):
49
+ # - Refresh tokens are NOT immediately revoked
50
+ # - New access token stores the old refresh token value in `previous_refresh_token`
51
+ # - Old refresh token is revoked later when the new access token is first used
52
+ # - Multiple concurrent refresh requests can succeed (no database locks)
53
+ # - Better database performance and lower latency
54
+ #
55
+ # When false (column does not exist):
56
+ # - Refresh tokens are immediately revoked using database locks
57
+ # - Only one concurrent refresh request can succeed
58
+ # - May experience database lock contention under high load
59
+ #
60
+ # To enable the revoke-on-use feature and improve performance:
61
+ # rails generate doorkeeper:previous_refresh_token
62
+ # rails db:migrate
63
+ #
64
+ # @return [Boolean] true if previous_refresh_token column exists
65
+ #
43
66
  def refresh_token_revoked_on_use?
44
67
  column_names.include?("previous_refresh_token")
45
68
  end
@@ -51,15 +74,10 @@ module Doorkeeper::Orm::ActiveRecord::Mixins
51
74
  if supports_expiration_time_math?
52
75
  # have not reached the expiration time or it never expires
53
76
  relation.where("#{expiration_time_sql} > ?", Time.now.utc).or(
54
- relation.where(expires_in: nil)
77
+ relation.where(expires_in: nil),
55
78
  )
56
79
  else
57
- ::Kernel.warn <<~WARNING.squish
58
- [DOORKEEPER] Doorkeeper doesn't support expiration time math for your database adapter (#{adapter_name}).
59
- Please add a class method `custom_expiration_time_sql` for your AccessToken class/mixin to provide a custom
60
- SQL expression to calculate access token expiration time. See lib/doorkeeper/orm/active_record/mixins/access_token.rb
61
- for more details.
62
- WARNING
80
+ ::Kernel.warn(::Doorkeeper::Models::ExpirationTimeSqlMath::WARNING_MESSAGE)
63
81
 
64
82
  relation
65
83
  end
@@ -9,6 +9,12 @@ module Doorkeeper::Orm::ActiveRecord::Mixins
9
9
  self.strict_loading_by_default = false if respond_to?(:strict_loading_by_default)
10
10
 
11
11
  include ::Doorkeeper::ApplicationMixin
12
+ # `enable_application_owner?` is read once, at parent-class autoload
13
+ # time (#1831): with the feature off the model exposes no `:owner`
14
+ # association — avoiding a misleading reflection on schemas that lack
15
+ # the owner columns. The flag is therefore a load-time switch; turning
16
+ # it on later requires defining a fresh model class.
17
+ include ::Doorkeeper::Models::Ownership if Doorkeeper.config.enable_application_owner?
12
18
 
13
19
  has_many :access_grants,
14
20
  foreign_key: :application_id,
@@ -34,8 +34,12 @@ module Doorkeeper
34
34
  Doorkeeper::OAuth::NonStandard::IETF_WG_OAUTH2_OOB_METHODS.include?(uri)
35
35
  end
36
36
 
37
+ # Script schemes are refused before the host application's rule is
38
+ # consulted, so a `forbid_redirect_uri` that rejects them too does not
39
+ # report the error twice.
37
40
  def forbidden_uri?(uri)
38
- Doorkeeper.config.forbid_redirect_uri.call(uri)
41
+ Doorkeeper::OAuth::Helpers::URIChecker.script_scheme?(uri) ||
42
+ Doorkeeper.config.forbid_redirect_uri.call(uri)
39
43
  end
40
44
 
41
45
  def unspecified_scheme?(uri)
@@ -24,11 +24,19 @@ module Doorkeeper
24
24
  # Clears expired records
25
25
  def clean_expired(ttl)
26
26
  table = @base_scope.arel_table
27
+ model_class = @base_scope.is_a?(::ActiveRecord::Relation) ? @base_scope.klass : @base_scope
27
28
 
28
- @base_scope
29
+ scope = @base_scope
29
30
  .where.not(expires_in: nil)
30
31
  .where(table[:created_at].lt(Time.current - ttl))
31
- .in_batches(&:delete_all)
32
+
33
+ if model_class.respond_to?(:supports_expiration_time_math?) && model_class.supports_expiration_time_math?
34
+ scope = scope.where("#{model_class.expiration_time_sql} < ?", Time.current)
35
+ else
36
+ ::Kernel.warn(::Doorkeeper::Models::ExpirationTimeSqlMath::WARNING_MESSAGE)
37
+ end
38
+
39
+ scope.in_batches(&:delete_all)
32
40
  end
33
41
  end
34
42
  end
@@ -28,18 +28,14 @@ module Doorkeeper
28
28
  autoload :Application, "doorkeeper/orm/active_record/mixins/application"
29
29
  end
30
30
 
31
- def self.run_hooks
32
- initialize_configured_associations
33
- end
34
-
35
- def self.initialize_configured_associations
36
- if Doorkeeper.config.enable_application_owner?
37
- Doorkeeper.config.application_model.include ::Doorkeeper::Models::Ownership
38
- end
39
-
40
- Doorkeeper.config.access_grant_model.include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessGrant
41
- Doorkeeper.config.access_token_model.include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessToken
42
- end
31
+ # Kept as a no-op so `Doorkeeper.run_orm_hooks` (and any plugin that
32
+ # checks `respond_to?(:run_hooks)`) stays quiet. The model concerns
33
+ # that used to be wired up here are now included from each Mixin's
34
+ # `included` block, which runs at parent-class autoload time — well
35
+ # after `Doorkeeper.configure` has applied user settings, and without
36
+ # touching `ActiveSupport.on_load(:active_record)` (whose re-entrant
37
+ # firing during `ApplicationRecord` autoload caused #1828).
38
+ def self.run_hooks; end
43
39
  end
44
40
  end
45
41
  end
@@ -54,7 +54,7 @@ module Doorkeeper
54
54
  controller: mapping[:controllers],
55
55
  ) do
56
56
  routes.get native_authorization_code_route, action: :show, on: :member
57
- routes.get '/', action: :new, on: :member
57
+ routes.get "/", action: :new, on: :member
58
58
  end
59
59
  end
60
60
 
@@ -3,18 +3,30 @@
3
3
  module Doorkeeper
4
4
  module Request
5
5
  class << self
6
+ # Resolves the strategy that answers the given response_type at the
7
+ # authorization endpoint.
8
+ #
9
+ # A response_type no registered authorization flow handles is refused
10
+ # rather than turned into a class name: `constantize` would otherwise
11
+ # resolve any constant under Doorkeeper::Request, token endpoint
12
+ # strategies (`password`, `client_credentials`, `refresh_token`,
13
+ # `authorization_code`) included, none of which can answer an
14
+ # authorization request.
6
15
  def authorization_strategy(response_type)
16
+ response_type = response_type.to_s
17
+
7
18
  grant_flow = authorization_flows.detect do |flow|
8
19
  flow.matches_response_type?(response_type)
9
20
  end
10
21
 
11
- if grant_flow
12
- grant_flow.response_type_strategy
13
- else
14
- # [NOTE]: this will be removed in a newer versions of Doorkeeper.
15
- # For retro-compatibility only
16
- build_fallback_strategy_class(response_type)
17
- end
22
+ raise Errors::InvalidTokenStrategy unless grant_flow
23
+
24
+ # [NOTE]: this will be removed in a newer versions of Doorkeeper.
25
+ # For retro-compatibility only: flows declared through the deprecated
26
+ # Config#calculate_authorization_response_types hook are built as
27
+ # GrantFlow::FallbackFlow and carry no strategy, so the class is looked
28
+ # up by name - but only for a response type the server does declare.
29
+ grant_flow.response_type_strategy || build_fallback_strategy_class(response_type)
18
30
  end
19
31
 
20
32
  def token_strategy(grant_type)
@@ -4,8 +4,8 @@ module Doorkeeper
4
4
  module VERSION
5
5
  # Semantic versioning
6
6
  MAJOR = 5
7
- MINOR = 8
8
- TINY = 2
7
+ MINOR = 9
8
+ TINY = 9
9
9
  PRE = nil
10
10
 
11
11
  # Full version number
data/lib/doorkeeper.rb CHANGED
@@ -101,6 +101,10 @@ module Doorkeeper
101
101
  autoload :ResourceOwnerable, "doorkeeper/models/concerns/resource_ownerable"
102
102
  autoload :Revocable, "doorkeeper/models/concerns/revocable"
103
103
  autoload :SecretStorable, "doorkeeper/models/concerns/secret_storable"
104
+
105
+ module Concerns
106
+ autoload :WriteToPrimary, "doorkeeper/models/concerns/write_to_primary"
107
+ end
104
108
  end
105
109
 
106
110
  module Orm
@@ -150,6 +154,16 @@ module Doorkeeper
150
154
  end
151
155
  end
152
156
 
157
+ def setup_filter_parameters
158
+ return unless defined?(::Rails) && ::Rails.application && configured?
159
+
160
+ parameters = %w[client_secret authentication_token access_token refresh_token]
161
+ parameters << "code" if configuration.grant_flows.include?("authorization_code")
162
+ filter = /^(#{Regexp.union(parameters)})$/
163
+ filter_params = ::Rails.application.config.filter_parameters
164
+ filter_params << filter unless filter_params.include?(filter)
165
+ end
166
+
153
167
  def setup_orm_adapter
154
168
  @orm_adapter = "doorkeeper/orm/#{configuration.orm}".classify.constantize
155
169
  rescue NameError => e
@@ -7,12 +7,12 @@ module Doorkeeper
7
7
  # Generates migration with which drops NOT NULL constraint and allows not
8
8
  # to bloat the database with redundant secret value.
9
9
  #
10
- class RemoveApplicationSecretNotNullConstraint < ::Rails::Generators::Base
10
+ class RemoveApplicationsSecretNotNullConstraintGenerator < ::Rails::Generators::Base
11
11
  include ::Rails::Generators::Migration
12
12
  source_root File.expand_path("templates", __dir__)
13
13
  desc "Removes NOT NULL constraint for OAuth2 applications."
14
14
 
15
- def enable_polymorphic_resource_owner
15
+ def remove_applications_secret_not_null_constraint
16
16
  migration_template(
17
17
  "remove_applications_secret_not_null_constraint.rb.erb",
18
18
  "db/migrate/remove_applications_secret_not_null_constraint.rb",
@@ -5,6 +5,21 @@ Doorkeeper.configure do
5
5
  # Check the list of supported ORMs here: https://github.com/doorkeeper-gem/doorkeeper#orms
6
6
  orm :active_record
7
7
 
8
+ # Enable support for multiple database configurations with read replicas.
9
+ # When enabled, Doorkeeper will wrap database write operations to ensure they
10
+ # use the primary (writable) database when automatic role switching is enabled.
11
+ #
12
+ # For ActiveRecord (Rails 6.1+), this uses `ActiveRecord::Base.connected_to(role: :writing)`.
13
+ # Other ORM extensions can implement their own primary database targeting logic.
14
+ #
15
+ # enable_multiple_database_roles
16
+ #
17
+ # This prevents `ActiveRecord::ReadOnlyError` when using read replicas with Rails
18
+ # automatic role switching. Enable this if your application uses multiple databases
19
+ # with automatic role switching for read replicas.
20
+ #
21
+ # See: https://guides.rubyonrails.org/active_record_multiple_databases.html#activating-automatic-role-switching
22
+
8
23
  # This block will be called to check whether the resource owner is authenticated or not.
9
24
  resource_owner_authenticator do
10
25
  raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
@@ -268,6 +283,14 @@ Doorkeeper.configure do
268
283
  # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated
269
284
  # for more information on customization
270
285
  #
286
+ # NOTE: configuring a callable extractor opts the request out of the RFC 6749
287
+ # §2.3 checks that reject a request authenticating the client with more than
288
+ # one method, or presenting more than one client identity. A callable may
289
+ # legitimately overlap the built-in extractors, and the extractors after the
290
+ # matching one are never called, so what they would have returned is unknown.
291
+ # Prefer the symbol extractors unless you need something they cannot express;
292
+ # a callable has to reject such a request itself.
293
+ #
271
294
  # client_credentials :from_basic, :from_params
272
295
 
273
296
  # Change the way access token is authenticated from the request object.
@@ -293,10 +316,13 @@ Doorkeeper.configure do
293
316
  # Specify what redirect URI's you want to block during Application creation.
294
317
  # Any redirect URI is allowed by default.
295
318
  #
296
- # You can use this option in order to forbid URI's with 'javascript' scheme
319
+ # Redirect URIs with a script scheme (`javascript`, `vbscript`, `data`) are
320
+ # always rejected, regardless of this option.
321
+ #
322
+ # You can use this option in order to forbid URI's of a particular host
297
323
  # for example.
298
324
  #
299
- # forbid_redirect_uri { |uri| uri.scheme.to_s.downcase == 'javascript' }
325
+ # forbid_redirect_uri { |uri| uri.host == 'example.com' }
300
326
 
301
327
  # Allows to set blank redirect URIs for Applications in case Doorkeeper configured
302
328
  # to use URI-less OAuth grant flows like Client Credentials or Resource Owner
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doorkeeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.8.2
4
+ version: 5.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felipe Elias Philipp
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2025-04-04 00:00:00.000000000 Z
14
+ date: 2026-09-24 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: railties
@@ -230,6 +230,7 @@ files:
230
230
  - lib/doorkeeper/models/concerns/revocable.rb
231
231
  - lib/doorkeeper/models/concerns/scopes.rb
232
232
  - lib/doorkeeper/models/concerns/secret_storable.rb
233
+ - lib/doorkeeper/models/concerns/write_to_primary.rb
233
234
  - lib/doorkeeper/oauth.rb
234
235
  - lib/doorkeeper/oauth/authorization/code.rb
235
236
  - lib/doorkeeper/oauth/authorization/context.rb
@@ -329,14 +330,7 @@ metadata:
329
330
  bug_tracker_uri: https://github.com/doorkeeper-gem/doorkeeper/issues
330
331
  documentation_uri: https://doorkeeper.gitbook.io/guides/
331
332
  funding_uri: https://opencollective.com/doorkeeper-gem
332
- post_install_message: "Starting from 5.5.0 RC1 Doorkeeper requires client authentication
333
- for Resource Owner Password Grant\nas stated in the OAuth RFC. You have to create
334
- a new OAuth client (Doorkeeper::Application) if you didn't\nhave it before and use
335
- client credentials in HTTP Basic auth if you previously used this grant flow without\nclient
336
- authentication. \n\nTo opt out of this you could set the \"skip_client_authentication_for_password_grant\"
337
- configuration option\nto \"true\", but note that this is in violation of the OAuth
338
- spec and represents a security risk.\n\nRead https://github.com/doorkeeper-gem/doorkeeper/issues/561#issuecomment-612857163
339
- for more details."
333
+ post_install_message:
340
334
  rdoc_options: []
341
335
  require_paths:
342
336
  - lib
@@ -351,7 +345,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
351
345
  - !ruby/object:Gem::Version
352
346
  version: '0'
353
347
  requirements: []
354
- rubygems_version: 3.5.15
348
+ rubygems_version: 3.5.16
355
349
  signing_key:
356
350
  specification_version: 4
357
351
  summary: OAuth 2 provider for Rails and Grape