1 # frozen_string_literal: true
 
   3 require_relative "../../lib/oauth"
 
   5 Doorkeeper.configure do
 
   6   # Change the ORM that doorkeeper will use (requires ORM extensions installed).
 
   7   # Check the list of supported ORMs here: https://github.com/doorkeeper-gem/doorkeeper#orms
 
  10   # This block will be called to check whether the resource owner is authenticated or not.
 
  11   resource_owner_authenticator do
 
  15   # If you didn't skip applications controller from Doorkeeper routes in your application routes.rb
 
  16   # file then you need to declare this block in order to restrict access to the web interface for
 
  17   # adding oauth authorized applications. In other case it will return 403 Forbidden response
 
  18   # every time somebody will try to access the admin web interface.
 
  20   admin_authenticator do
 
  24   # You can use your own model classes if you need to extend (or even override) default
 
  25   # Doorkeeper models such as `Application`, `AccessToken` and `AccessGrant.
 
  27   # Be default Doorkeeper ActiveRecord ORM uses it's own classes:
 
  29   # access_token_class "Doorkeeper::AccessToken"
 
  30   # access_grant_class "Doorkeeper::AccessGrant"
 
  31   # application_class "Doorkeeper::Application"
 
  33   # Don't forget to include Doorkeeper ORM mixins into your custom models:
 
  35   #   *  ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken - for access token
 
  36   #   *  ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessGrant - for access grant
 
  37   #   *  ::Doorkeeper::Orm::ActiveRecord::Mixins::Application - for application (OAuth2 clients)
 
  41   # access_token_class "MyAccessToken"
 
  43   # class MyAccessToken < ApplicationRecord
 
  44   #   include ::Doorkeeper::Orm::ActiveRecord::Mixins::AccessToken
 
  46   #   self.table_name = "hey_i_wanna_my_name"
 
  53   application_class "Oauth2Application" unless Settings.status == "database_offline"
 
  55   # Enables polymorphic Resource Owner association for Access Tokens and Access Grants.
 
  56   # By default this option is disabled.
 
  58   # Make sure you properly setup you database and have all the required columns (run
 
  59   # `bundle exec rails generate doorkeeper:enable_polymorphic_resource_owner` and execute Rails
 
  62   # If this option enabled, Doorkeeper will store not only Resource Owner primary key
 
  63   # value, but also it's type (class name). See "Polymorphic Associations" section of
 
  64   # Rails guides: https://guides.rubyonrails.org/association_basics.html#polymorphic-associations
 
  66   # [NOTE] If you apply this option on already existing project don't forget to manually
 
  67   # update `resource_owner_type` column in the database and fix migration template as it will
 
  68   # set NOT NULL constraint for Access Grants table.
 
  70   # use_polymorphic_resource_owner
 
  72   # If you are planning to use Doorkeeper in Rails 5 API-only application, then you might
 
  73   # want to use API mode that will skip all the views management and change the way how
 
  74   # Doorkeeper responds to a requests.
 
  78   # Enforce token request content type to application/x-www-form-urlencoded.
 
  79   # It is not enabled by default to not break prior versions of the gem.
 
  83   # Authorization Code expiration time (default: 10 minutes).
 
  85   # authorization_code_expires_in 10.minutes
 
  87   # Access token expiration time (default: 2 hours).
 
  88   # If you want to disable expiration, set this to `nil`.
 
  90   access_token_expires_in nil
 
  92   # Assign custom TTL for access tokens. Will be used instead of access_token_expires_in
 
  93   # option if defined. In case the block returns `nil` value Doorkeeper fallbacks to
 
  94   # +access_token_expires_in+ configuration option value. If you really need to issue a
 
  95   # non-expiring access token (which is not recommended) then you need to return
 
  96   # Float::INFINITY from this block.
 
  98   # `context` has the following properties available:
 
 100   #   * `client` - the OAuth client application (see Doorkeeper::OAuth::Client)
 
 101   #   * `grant_type` - the grant type of the request (see Doorkeeper::OAuth)
 
 102   #   * `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes)
 
 103   #   * `resource_owner` - authorized resource owner instance (if present)
 
 105   # custom_access_token_expires_in do |context|
 
 106   #   context.client.additional_settings.implicit_oauth_expiration
 
 109   # Use a custom class for generating the access token.
 
 110   # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-access-token-generator
 
 112   # access_token_generator '::Doorkeeper::JWT'
 
 114   # The controller +Doorkeeper::ApplicationController+ inherits from.
 
 115   # Defaults to +ActionController::Base+ unless +api_only+ is set, which changes the default to
 
 116   # +ActionController::API+. The return value of this option must be a stringified class name.
 
 117   # See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-controllers
 
 119   base_controller "ApplicationController"
 
 121   # Reuse access token for the same resource owner within an application (disabled by default).
 
 123   # This option protects your application from creating new tokens before old valid one becomes
 
 124   # expired so your database doesn't bloat. Keep in mind that when this option is `on` Doorkeeper
 
 125   # doesn't updates existing token expiration time, it will create a new token instead.
 
 126   # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383
 
 128   # You can not enable this option together with +hash_token_secrets+.
 
 132   # In case you enabled `reuse_access_token` option Doorkeeper will try to find matching
 
 133   # token using `matching_token_for` Access Token API that searches for valid records
 
 134   # in batches in order not to pollute the memory with all the database records. By default
 
 135   # Doorkeeper uses batch size of 10 000 records. You can increase or decrease this value
 
 136   # depending on your needs and server capabilities.
 
 138   # token_lookup_batch_size 10_000
 
 140   # Set a limit for token_reuse if using reuse_access_token option
 
 142   # This option limits token_reusability to some extent.
 
 143   # If not set then access_token will be reused unless it expires.
 
 144   # Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/1189
 
 146   # This option should be a percentage(i.e. (0,100])
 
 148   # token_reuse_limit 100
 
 150   # Only allow one valid access token obtained via client credentials
 
 151   # per client. If a new access token is obtained before the old one
 
 152   # expired, the old one gets revoked (disabled by default)
 
 154   # When enabling this option, make sure that you do not expect multiple processes
 
 155   # using the same credentials at the same time (e.g. web servers spanning
 
 156   # multiple machines and/or processes).
 
 158   # revoke_previous_client_credentials_token
 
 160   # Hash access and refresh tokens before persisting them.
 
 161   # This will disable the possibility to use +reuse_access_token+
 
 162   # since plain values can no longer be retrieved.
 
 164   # Note: If you are already a user of doorkeeper and have existing tokens
 
 165   # in your installation, they will be invalid without adding 'fallback: :plain'.
 
 168   # By default, token secrets will be hashed using the
 
 169   # +Doorkeeper::Hashing::SHA256+ strategy.
 
 171   # If you wish to use another hashing implementation, you can override
 
 172   # this strategy as follows:
 
 174   hash_token_secrets :using => "::Doorkeeper::SecretStoring::Plain",
 
 175                      :fallback => "::Doorkeeper::SecretStoring::Sha256Hash"
 
 177   # Keep in mind that changing the hashing function will invalidate all existing
 
 178   # secrets, if there are any.
 
 180   # Hash application secrets before persisting them.
 
 182   hash_application_secrets
 
 184   # By default, applications will be hashed
 
 185   # with the +Doorkeeper::SecretStoring::SHA256+ strategy.
 
 187   # If you wish to use bcrypt for application secret hashing, uncomment
 
 190   # hash_application_secrets using: '::Doorkeeper::SecretStoring::BCrypt'
 
 192   # When the above option is enabled, and a hashed token or secret is not found,
 
 193   # you can allow to fall back to another strategy. For users upgrading
 
 194   # doorkeeper and wishing to enable hashing, you will probably want to enable
 
 195   # the fallback to plain tokens.
 
 197   # This will ensure that old access tokens and secrets
 
 198   # will remain valid even if the hashing above is enabled.
 
 200   # This can be done by adding 'fallback: plain', e.g. :
 
 202   # hash_application_secrets using: '::Doorkeeper::SecretStoring::BCrypt', fallback: :plain
 
 204   # Issue access tokens with refresh token (disabled by default), you may also
 
 205   # pass a block which accepts `context` to customize when to give a refresh
 
 206   # token or not. Similar to +custom_access_token_expires_in+, `context` has
 
 207   # the following properties:
 
 209   # `client` - the OAuth client application (see Doorkeeper::OAuth::Client)
 
 210   # `grant_type` - the grant type of the request (see Doorkeeper::OAuth)
 
 211   # `scopes` - the requested scopes (see Doorkeeper::OAuth::Scopes)
 
 215   # Provide support for an owner to be assigned to each registered application (disabled by default)
 
 216   # Optional parameter confirmation: true (default: false) if you want to enforce ownership of
 
 217   # a registered application
 
 218   # NOTE: you must also run the rails g doorkeeper:application_owner generator
 
 219   # to provide the necessary support
 
 221   enable_application_owner :confirmation => true
 
 223   # Define access token scopes for your provider
 
 224   # For more information go to
 
 225   # https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes
 
 227   # default_scopes  :public
 
 228   optional_scopes(*Oauth::SCOPES, *Oauth::PRIVILEGED_SCOPES)
 
 230   # Allows to restrict only certain scopes for grant_type.
 
 231   # By default, all the scopes will be available for all the grant types.
 
 233   # Keys to this hash should be the name of grant_type and
 
 234   # values should be the array of scopes for that grant type.
 
 235   # Note: scopes should be from configured_scopes (i.e. default or optional)
 
 237   # scopes_by_grant_type password: [:write], client_credentials: [:update]
 
 239   # Forbids creating/updating applications with arbitrary scopes that are
 
 240   # not in configuration, i.e. +default_scopes+ or +optional_scopes+.
 
 241   # (disabled by default)
 
 243   enforce_configured_scopes
 
 245   # Change the way client credentials are retrieved from the request object.
 
 246   # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
 
 247   # falls back to the `:client_id` and `:client_secret` params from the `params` object.
 
 248   # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated
 
 249   # for more information on customization
 
 251   # client_credentials :from_basic, :from_params
 
 253   # Change the way access token is authenticated from the request object.
 
 254   # By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
 
 255   # falls back to the `:access_token` or `:bearer_token` params from the `params` object.
 
 256   # Check out https://github.com/doorkeeper-gem/doorkeeper/wiki/Changing-how-clients-are-authenticated
 
 257   # for more information on customization
 
 259   access_token_methods :from_bearer_authorization
 
 261   # Forces the usage of the HTTPS protocol in non-native redirect uris (enabled
 
 262   # by default in non-development environments). OAuth2 delegates security in
 
 263   # communication to the HTTPS protocol so it is wise to keep this enabled.
 
 265   # Callable objects such as proc, lambda, block or any object that responds to
 
 266   # #call can be used in order to allow conditional checks (to allow non-SSL
 
 267   # redirects to localhost for example).
 
 269   force_ssl_in_redirect_uri do |uri|
 
 270     !Rails.env.development? && uri.host != "127.0.0.1"
 
 273   # Specify what redirect URI's you want to block during Application creation.
 
 274   # Any redirect URI is whitelisted by default.
 
 276   # You can use this option in order to forbid URI's with 'javascript' scheme
 
 279   # forbid_redirect_uri { |uri| uri.scheme.to_s.downcase == 'javascript' }
 
 281   # Allows to set blank redirect URIs for Applications in case Doorkeeper configured
 
 282   # to use URI-less OAuth grant flows like Client Credentials or Resource Owner
 
 283   # Password Credentials. The option is on by default and checks configured grant
 
 284   # types, but you **need** to manually drop `NOT NULL` constraint from `redirect_uri`
 
 285   # column for `oauth_applications` database table.
 
 287   # You can completely disable this feature with:
 
 289   # allow_blank_redirect_uri false
 
 291   # Or you can define your custom check:
 
 293   # allow_blank_redirect_uri do |grant_flows, client|
 
 297   # Specify how authorization errors should be handled.
 
 298   # By default, doorkeeper renders json errors when access token
 
 299   # is invalid, expired, revoked or has invalid scopes.
 
 301   # If you want to render error response yourself (i.e. rescue exceptions),
 
 302   # set +handle_auth_errors+ to `:raise` and rescue Doorkeeper::Errors::InvalidToken
 
 303   # or following specific errors:
 
 305   #   Doorkeeper::Errors::TokenForbidden, Doorkeeper::Errors::TokenExpired,
 
 306   #   Doorkeeper::Errors::TokenRevoked, Doorkeeper::Errors::TokenUnknown
 
 308   # handle_auth_errors :raise
 
 310   # Customize token introspection response.
 
 311   # Allows to add your own fields to default one that are required by the OAuth spec
 
 312   # for the introspection response. It could be `sub`, `aud` and so on.
 
 313   # This configuration option can be a proc, lambda or any Ruby object responds
 
 314   # to `.call` method and result of it's invocation must be a Hash.
 
 316   # custom_introspection_response do |token, context|
 
 318   #     "sub": "Z5O3upPC88QrAjx00dis",
 
 319   #     "aud": "https://protected.example.net/resource",
 
 320   #     "username": User.find(token.resource_owner_id).username
 
 326   # custom_introspection_response CustomIntrospectionResponder
 
 328   # Specify what grant flows are enabled in array of Strings. The valid
 
 329   # strings and the flows they enable are:
 
 331   # "authorization_code" => Authorization Code Grant Flow
 
 332   # "implicit"           => Implicit Grant Flow
 
 333   # "password"           => Resource Owner Password Credentials Grant Flow
 
 334   # "client_credentials" => Client Credentials Grant Flow
 
 336   # If not specified, Doorkeeper enables authorization_code and
 
 337   # client_credentials.
 
 339   # implicit and password grant flows have risks that you should understand
 
 341   #   http://tools.ietf.org/html/rfc6819#section-4.4.2
 
 342   #   http://tools.ietf.org/html/rfc6819#section-4.4.3
 
 344   grant_flows %w[authorization_code]
 
 346   # Allows to customize OAuth grant flows that +each+ application support.
 
 347   # You can configure a custom block (or use a class respond to `#call`) that must
 
 348   # return `true` in case Application instance supports requested OAuth grant flow
 
 349   # during the authorization request to the server. This configuration +doesn't+
 
 350   # set flows per application, it only allows to check if application supports
 
 351   # specific grant flow.
 
 353   # For example you can add an additional database column to `oauth_applications` table,
 
 354   # say `t.array :grant_flows, default: []`, and store allowed grant flows that can
 
 355   # be used with this application there. Then when authorization requested Doorkeeper
 
 356   # will call this block to check if specific Application (passed with client_id and/or
 
 357   # client_secret) is allowed to perform the request for the specific grant type
 
 358   # (authorization, password, client_credentials, etc).
 
 360   # Example of the block:
 
 362   #   ->(flow, client) { client.grant_flows.include?(flow) }
 
 364   # In case this option invocation result is `false`, Doorkeeper server returns
 
 365   # :unauthorized_client error and stops the request.
 
 367   # @param allow_grant_flow_for_client [Proc] Block or any object respond to #call
 
 368   # @return [Boolean] `true` if allow or `false` if forbid the request
 
 370   # allow_grant_flow_for_client do |grant_flow, client|
 
 371   #   # `grant_flows` is an Array column with grant
 
 372   #   # flows that application supports
 
 374   #   client.grant_flows.include?(grant_flow)
 
 377   # If you need arbitrary Resource Owner-Client authorization you can enable this option
 
 378   # and implement the check your need. Config option must respond to #call and return
 
 379   # true in case resource owner authorized for the specific application or false in other
 
 382   # Be default all Resource Owners are authorized to any Client (application).
 
 384   # authorize_resource_owner_for_client do |client, resource_owner|
 
 385   #   resource_owner.admin? || client.owners_whitelist.include?(resource_owner)
 
 388   # Hook into the strategies' request & response life-cycle in case your
 
 389   # application needs advanced customization or logging:
 
 391   # before_successful_strategy_response do |request|
 
 392   #   puts "BEFORE HOOK FIRED! #{request}"
 
 395   # after_successful_strategy_response do |request, response|
 
 396   #   puts "AFTER HOOK FIRED! #{request}, #{response}"
 
 399   # Hook into Authorization flow in order to implement Single Sign Out
 
 400   # or add any other functionality. Inside the block you have an access
 
 401   # to `controller` (authorizations controller instance) and `context`
 
 402   # (Doorkeeper::OAuth::Hooks::Context instance) which provides pre auth
 
 403   # or auth objects with issued token based on hook type (before or after).
 
 405   # before_successful_authorization do |controller, context|
 
 406   #   Rails.logger.info(controller.request.params.inspect)
 
 408   #   Rails.logger.info(context.pre_auth.inspect)
 
 411   # after_successful_authorization do |controller, context|
 
 412   #   controller.session[:logout_urls] <<
 
 413   #     Doorkeeper::Application
 
 414   #       .find_by(controller.request.params.slice(:redirect_uri))
 
 417   #   Rails.logger.info(context.auth.inspect)
 
 418   #   Rails.logger.info(context.issued_token)
 
 421   # Under some circumstances you might want to have applications auto-approved,
 
 422   # so that the user skips the authorization step.
 
 423   # For example if dealing with a trusted application.
 
 425   skip_authorization do |_, client|
 
 426     client.scopes.include?("skip_authorization")
 
 429   # Configure custom constraints for the Token Introspection request.
 
 430   # By default this configuration option allows to introspect a token by another
 
 431   # token of the same application, OR to introspect the token that belongs to
 
 432   # authorized client (from authenticated client) OR when token doesn't
 
 433   # belong to any client (public token). Otherwise requester has no access to the
 
 434   # introspection and it will return response as stated in the RFC.
 
 438   # @param token [Doorkeeper::AccessToken]
 
 439   #   token to be introspected
 
 441   # @param authorized_client [Doorkeeper::Application]
 
 442   #   authorized client (if request is authorized using Basic auth with
 
 443   #   Client Credentials for example)
 
 445   # @param authorized_token [Doorkeeper::AccessToken]
 
 446   #   Bearer token used to authorize the request
 
 448   # In case the block returns `nil` or `false` introspection responses with 401 status code
 
 449   # when using authorized token to introspect, or you'll get 200 with { "active": false } body
 
 450   # when using authorized client to introspect as stated in the
 
 451   # RFC 7662 section 2.2. Introspection Response.
 
 453   # Using with caution:
 
 454   # Keep in mind that these three parameters pass to block can be nil as following case:
 
 455   #  `authorized_client` is nil if and only if `authorized_token` is present, and vice versa.
 
 456   #  `token` will be nil if and only if `authorized_token` is present.
 
 457   # So remember to use `&` or check if it is present before calling method on
 
 458   # them to make sure you doesn't get NoMethodError exception.
 
 460   # You can define your custom check:
 
 462   # allow_token_introspection do |token, authorized_client, authorized_token|
 
 463   #   if authorized_token
 
 464   #     # customize: require `introspection` scope
 
 465   #     authorized_token.application == token&.application ||
 
 466   #       authorized_token.scopes.include?("introspection")
 
 467   #   elsif token.application
 
 468   #     # `protected_resource` is a new database boolean column, for example
 
 469   #     authorized_client == token.application || authorized_client.protected_resource?
 
 471   #     # public token (when token.application is nil, token doesn't belong to any application)
 
 476   # Or you can completely disable any token introspection:
 
 478   # allow_token_introspection false
 
 480   # If you need to block the request at all, then configure your routes.rb or web-server
 
 481   # like nginx to forbid the request.
 
 483   # WWW-Authenticate Realm (default: "Doorkeeper").