1 # frozen_string_literal: true
3 Doorkeeper.configure do
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
8 # This block will be called to check whether the resource owner is authenticated or not.
9 resource_owner_authenticator do
13 # If you didn't skip applications controller from Doorkeeper routes in your application routes.rb
14 # file then you need to declare this block in order to restrict access to the web interface for
15 # adding oauth authorized applications. In other case it will return 403 Forbidden response
16 # every time somebody will try to access the admin web interface.
18 admin_authenticator do
22 # You can use your own model classes if you need to extend (or even override) default
23 # Doorkeeper models such as `Application`, `AccessToken` and `AccessGrant.
25 # Be default Doorkeeper ActiveRecord ORM uses it's own classes:
27 # access_token_class "Doorkeeper::AccessToken"
28 # access_grant_class "Doorkeeper::AccessGrant"
29 # application_class "Doorkeeper::Application"
31 # Don't forget to include Doorkeeper ORM mixins into your custom models:
33 # * ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken - for access token
34 # * ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant - for access grant
35 # * ::Doorkeeper::Orm::ActiveRecord::Mixins::Application - for application (OAuth2 clients)
39 # access_token_class "MyAccessToken"
41 # class MyAccessToken < ApplicationRecord
42 # include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken
44 # self.table_name = "hey_i_wanna_my_name"
51 # Enables polymorphic Resource Owner association for Access Tokens and Access Grants.
52 # By default this option is disabled.
54 # Make sure you properly setup you database and have all the required columns (run
55 # `bundle exec rails generate doorkeeper:enable_polymorphic_resource_owner` and execute Rails
58 # If this option enabled, Doorkeeper will store not only Resource Owner primary key
59 # value, but also it's type (class name). See "Polymorphic Associations" section of
60 # Rails guides: https://guides.rubyonrails.org/association_basics.html#polymorphic-associations
62 # [NOTE] If you apply this option on already existing project don't forget to manually
63 # update `resource_owner_type` column in the database and fix migration template as it will
64 # set NOT NULL constraint for Access Grants table.
66 # use_polymorphic_resource_owner
68 # If you are planning to use Doorkeeper in Rails 5 API-only application, then you might
69 # want to use API mode that will skip all the views management and change the way how
70 # Doorkeeper responds to a requests.
74 # Enforce token request content type to application/x-www-form-urlencoded.
75 # It is not enabled by default to not break prior versions of the gem.
79 # Authorization Code expiration time (default: 10 minutes).
81 # authorization_code_expires_in 10.minutes
83 # Access token expiration time (default: 2 hours).
84 # If you want to disable expiration, set this to `nil`.
86 access_token_expires_in nil
88 # Assign custom TTL for access tokens. Will be used instead of access_token_expires_in
89 # option if defined. In case the block returns `nil` value Doorkeeper fallbacks to
90 # +access_token_expires_in+ configuration option value. If you really need to issue a
91 # non-expiring access token (which is not recommended) then you need to return
92 # Float::INFINITY from this block.
94 # `context` has the following properties available:
96 # `client` - the OAuth client application (see Doorkeeper::OAuth::Client)
97 # `grant_type` - the grant type of the request (see Doorkeeper::OAuth)
98 # `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes)
100 # custom_access_token_expires_in do |context|
101 # context.client.application.additional_settings.implicit_oauth_expiration
104 # Use a custom class for generating the access token.
105 # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-access-token-generator
107 # access_token_generator '::Doorkeeper::JWT'
109 # The controller +Doorkeeper::ApplicationController+ inherits from.
110 # Defaults to +ActionController::Base+ unless +api_only+ is set, which changes the default to
111 # +ActionController::API+. The return value of this option must be a stringified class name.
112 # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-base-controller
114 base_controller "ApplicationController"
116 # Reuse access token for the same resource owner within an application (disabled by default).
118 # This option protects your application from creating new tokens before old valid one becomes
119 # expired so your database doesn't bloat. Keep in mind that when this option is `on` Doorkeeper
120 # doesn't updates existing token expiration time, it will create a new token instead.
121 # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383
123 # You can not enable this option together with +hash_token_secrets+.
127 # In case you enabled `reuse_access_token` option Doorkeeper will try to find matching
128 # token using `matching_token_for` Access Token API that searches for valid records
129 # in batches in order not to pollute the memory with all the database records. By default
130 # Doorkeeper uses batch size of 10 000 records. You can increase or decrease this value
131 # depending on your needs and server capabilities.
133 # token_lookup_batch_size 10_000
135 # Set a limit for token_reuse if using reuse_access_token option
137 # This option limits token_reusability to some extent.
138 # If not set then access_token will be reused unless it expires.
139 # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/1189
141 # This option should be a percentage(i.e. (0,100])
143 # token_reuse_limit 100
145 # Only allow one valid access token obtained via client credentials
146 # per client. If a new access token is obtained before the old one
147 # expired, the old one gets revoked (disabled by default)
149 # When enabling this option, make sure that you do not expect multiple processes
150 # using the same credentials at the same time (e.g. web servers spanning
151 # multiple machines and/or processes).
153 # revoke_previous_client_credentials_token
155 # Hash access and refresh tokens before persisting them.
156 # This will disable the possibility to use +reuse_access_token+
157 # since plain values can no longer be retrieved.
159 # Note: If you are already a user of doorkeeper and have existing tokens
160 # in your installation, they will be invalid without enabling the additional
161 # setting `fallback_to_plain_secrets` below.
165 # Hash application secrets before persisting them.
167 hash_application_secrets
169 # When the above option is enabled, and a hashed token or secret is not found,
170 # you can allow to fall back to another strategy. For users upgrading
171 # doorkeeper and wishing to enable hashing, you will probably want to enable
172 # the fallback to plain tokens.
174 # This will ensure that old access tokens and secrets
175 # will remain valid even if the hashing above is enabled.
177 # fallback_to_plain_secrets
179 # Issue access tokens with refresh token (disabled by default), you may also
180 # pass a block which accepts `context` to customize when to give a refresh
181 # token or not. Similar to +custom_access_token_expires_in+, `context` has
182 # the following properties:
184 # `client` - the OAuth client application (see Doorkeeper::OAuth::Client)
185 # `grant_type` - the grant type of the request (see Doorkeeper::OAuth)
186 # `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes)
190 # Provide support for an owner to be assigned to each registered application (disabled by default)
191 # Optional parameter confirmation: true (default: false) if you want to enforce ownership of
192 # a registered application
193 # NOTE: you must also run the rails g doorkeeper:application_owner generator
194 # to provide the necessary support
196 enable_application_owner :confirmation => true
198 # Define access token scopes for your provider
199 # For more information go to
200 # https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes
202 # default_scopes :public
203 optional_scopes(*Oauth::SCOPES)
205 # Allows to restrict only certain scopes for grant_type.
206 # By default, all the scopes will be available for all the grant types.
208 # Keys to this hash should be the name of grant_type and
209 # values should be the array of scopes for that grant type.
210 # Note: scopes should be from configured_scopes (i.e. default or optional)
212 # scopes_by_grant_type password: [:write], client_credentials: [:update]
214 # Forbids creating/updating applications with arbitrary scopes that are
215 # not in configuration, i.e. +default_scopes+ or +optional_scopes+.
216 # (disabled by default)
218 enforce_configured_scopes
220 # Change the way client credentials are retrieved from the request object.
221 # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
222 # falls back to the `:client_id` and `:client_secret` params from the `params` object.
223 # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated
224 # for more information on customization
226 # client_credentials :from_basic, :from_params
228 # Change the way access token is authenticated from the request object.
229 # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
230 # falls back to the `:access_token` or `:bearer_token` params from the `params` object.
231 # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated
232 # for more information on customization
234 access_token_methods :from_bearer_authorization
236 # Forces the usage of the HTTPS protocol in non-native redirect uris (enabled
237 # by default in non-development environments). OAuth2 delegates security in
238 # communication to the HTTPS protocol so it is wise to keep this enabled.
240 # Callable objects such as proc, lambda, block or any object that responds to
241 # #call can be used in order to allow conditional checks (to allow non-SSL
242 # redirects to localhost for example).
244 force_ssl_in_redirect_uri do |uri|
245 !Rails.env.development? && uri.host != "127.0.0.1"
248 # Specify what redirect URI's you want to block during Application creation.
249 # Any redirect URI is whitelisted by default.
251 # You can use this option in order to forbid URI's with 'javascript' scheme
254 # forbid_redirect_uri { |uri| uri.scheme.to_s.downcase == 'javascript' }
256 # Allows to set blank redirect URIs for Applications in case Doorkeeper configured
257 # to use URI-less OAuth grant flows like Client Credentials or Resource Owner
258 # Password Credentials. The option is on by default and checks configured grant
259 # types, but you **need** to manually drop `NOT NULL` constraint from `redirect_uri`
260 # column for `oauth_applications` database table.
262 # You can completely disable this feature with:
264 # allow_blank_redirect_uri false
266 # Or you can define your custom check:
268 # allow_blank_redirect_uri do |grant_flows, client|
272 # Specify how authorization errors should be handled.
273 # By default, doorkeeper renders json errors when access token
274 # is invalid, expired, revoked or has invalid scopes.
276 # If you want to render error response yourself (i.e. rescue exceptions),
277 # set +handle_auth_errors+ to `:raise` and rescue Doorkeeper::Errors::InvalidToken
278 # or following specific errors:
280 # Doorkeeper::Errors::TokenForbidden, Doorkeeper::Errors::TokenExpired,
281 # Doorkeeper::Errors::TokenRevoked, Doorkeeper::Errors::TokenUnknown
283 # handle_auth_errors :raise
285 # Customize token introspection response.
286 # Allows to add your own fields to default one that are required by the OAuth spec
287 # for the introspection response. It could be `sub`, `aud` and so on.
288 # This configuration option can be a proc, lambda or any Ruby object responds
289 # to `.call` method and result of it's invocation must be a Hash.
291 # custom_introspection_response do |token, context|
293 # "sub": "Z5O3upPC88QrAjx00dis",
294 # "aud": "https://protected.example.net/resource",
295 # "username": User.find(token.resource_owner_id).username
301 # custom_introspection_response CustomIntrospectionResponder
303 # Specify what grant flows are enabled in array of Strings. The valid
304 # strings and the flows they enable are:
306 # "authorization_code" => Authorization Code Grant Flow
307 # "implicit" => Implicit Grant Flow
308 # "password" => Resource Owner Password Credentials Grant Flow
309 # "client_credentials" => Client Credentials Grant Flow
311 # If not specified, Doorkeeper enables authorization_code and
312 # client_credentials.
314 # implicit and password grant flows have risks that you should understand
316 # http://tools.ietf.org/html/rfc6819#section-4.4.2
317 # http://tools.ietf.org/html/rfc6819#section-4.4.3
319 grant_flows %w[authorization_code]
321 # Allows to customize OAuth grant flows that +each+ application support.
322 # You can configure a custom block (or use a class respond to `#call`) that must
323 # return `true` in case Application instance supports requested OAuth grant flow
324 # during the authorization request to the server. This configuration +doesn't+
325 # set flows per application, it only allows to check if application supports
326 # specific grant flow.
328 # For example you can add an additional database column to `oauth_applications` table,
329 # say `t.array :grant_flows, default: []`, and store allowed grant flows that can
330 # be used with this application there. Then when authorization requested Doorkeeper
331 # will call this block to check if specific Application (passed with client_id and/or
332 # client_secret) is allowed to perform the request for the specific grant type
333 # (authorization, password, client_credentials, etc).
335 # Example of the block:
337 # ->(flow, client) { client.grant_flows.include?(flow) }
339 # In case this option invocation result is `false`, Doorkeeper server returns
340 # :unauthorized_client error and stops the request.
342 # @param allow_grant_flow_for_client [Proc] Block or any object respond to #call
343 # @return [Boolean] `true` if allow or `false` if forbid the request
345 # allow_grant_flow_for_client do |grant_flow, client|
346 # # `grant_flows` is an Array column with grant
347 # # flows that application supports
349 # client.grant_flows.include?(grant_flow)
352 # If you need arbitrary Resource Owner-Client authorization you can enable this option
353 # and implement the check your need. Config option must respond to #call and return
354 # true in case resource owner authorized for the specific application or false in other
357 # Be default all Resource Owners are authorized to any Client (application).
359 # authorize_resource_owner_for_client do |client, resource_owner|
360 # resource_owner.admin? || client.owners_whitelist.include?(resource_owner)
363 # Hook into the strategies' request & response life-cycle in case your
364 # application needs advanced customization or logging:
366 # before_successful_strategy_response do |request|
367 # puts "BEFORE HOOK FIRED! #{request}"
370 # after_successful_strategy_response do |request, response|
371 # puts "AFTER HOOK FIRED! #{request}, #{response}"
374 # Hook into Authorization flow in order to implement Single Sign Out
375 # or add any other functionality. Inside the block you have an access
376 # to `controller` (authorizations controller instance) and `context`
377 # (Doorkeeper::OAuth::Hooks::Context instance) which provides pre auth
378 # or auth objects with issued token based on hook type (before or after).
380 # before_successful_authorization do |controller, context|
381 # Rails.logger.info(controller.request.params.inspect)
383 # Rails.logger.info(context.pre_auth.inspect)
386 # after_successful_authorization do |controller, context|
387 # controller.session[:logout_urls] <<
388 # Doorkeeper::Application
389 # .find_by(controller.request.params.slice(:redirect_uri))
392 # Rails.logger.info(context.auth.inspect)
393 # Rails.logger.info(context.issued_token)
396 # Under some circumstances you might want to have applications auto-approved,
397 # so that the user skips the authorization step.
398 # For example if dealing with a trusted application.
400 # skip_authorization do |resource_owner, client|
401 # client.superapp? or resource_owner.admin?
404 # Configure custom constraints for the Token Introspection request.
405 # By default this configuration option allows to introspect a token by another
406 # token of the same application, OR to introspect the token that belongs to
407 # authorized client (from authenticated client) OR when token doesn't
408 # belong to any client (public token). Otherwise requester has no access to the
409 # introspection and it will return response as stated in the RFC.
413 # @param token [Doorkeeper::AccessToken]
414 # token to be introspected
416 # @param authorized_client [Doorkeeper::Application]
417 # authorized client (if request is authorized using Basic auth with
418 # Client Credentials for example)
420 # @param authorized_token [Doorkeeper::AccessToken]
421 # Bearer token used to authorize the request
423 # In case the block returns `nil` or `false` introspection responses with 401 status code
424 # when using authorized token to introspect, or you'll get 200 with { "active": false } body
425 # when using authorized client to introspect as stated in the
426 # RFC 7662 section 2.2. Introspection Response.
428 # Using with caution:
429 # Keep in mind that these three parameters pass to block can be nil as following case:
430 # `authorized_client` is nil if and only if `authorized_token` is present, and vice versa.
431 # `token` will be nil if and only if `authorized_token` is present.
432 # So remember to use `&` or check if it is present before calling method on
433 # them to make sure you doesn't get NoMethodError exception.
435 # You can define your custom check:
437 # allow_token_introspection do |token, authorized_client, authorized_token|
438 # if authorized_token
439 # # customize: require `introspection` scope
440 # authorized_token.application == token&.application ||
441 # authorized_token.scopes.include?("introspection")
442 # elsif token.application
443 # # `protected_resource` is a new database boolean column, for example
444 # authorized_client == token.application || authorized_client.protected_resource?
446 # # public token (when token.application is nil, token doesn't belong to any application)
451 # Or you can completely disable any token introspection:
453 # allow_token_introspection false
455 # If you need to block the request at all, then configure your routes.rb or web-server
456 # like nginx to forbid the request.
458 # WWW-Authenticate Realm (default: "Doorkeeper").