skip to content

In Doorkeeper, how do default_scopes and optional_scopes differ, and which scopes does an access token get when the client's request names none?

level: middleimportance: should knowfreq 20%

answer

  1. server scopes are default plus optional
  2. omitted scope falls back to defaults
  3. explicit request replaces the defaults
  4. application's scopes column narrows the allowed set
  5. enforce_configured_scopes validates saved applications

basics

~10 s

In Doorkeeper, default_scopes are granted when a client requests no scope, while optional_scopes are granted only when named; together they are the scopes the server accepts, and a client's registered scopes narrow that set.

solid answer

~40 s

Both lists live in the initializer, and together they are the server's scopes: a requested scope outside them fails with `invalid_scope`. `default_scopes` do two jobs. When an authorization or token request omits `scope`, the token gets the default scopes, intersected with the client's registered scopes if it has any; and a bare `doorkeeper_authorize!` checks for them. `optional_scopes` are granted only when the client names them in `scope=`. An explicit request **replaces** the defaults rather than adding to them, so a client asking for `read_workouts` alone gets a token without `read_profile`. A `Doorkeeper::Application` can also store its own `scopes`; when filled, that list replaces the server's as the allowed set for that client. `enforce_configured_scopes` makes Doorkeeper refuse to save an application whose scopes are not configured ones.

code

ruby · 14 lines
ruby
# config/initializers/doorkeeper.rb
Doorkeeper.configure do
  default_scopes  :read_profile
  optional_scopes :read_workouts, :write_workouts
  enforce_configured_scopes
  scopes_by_grant_type client_credentials: [:read_workouts]
end

# a read-only partner, from bin/rails console
Doorkeeper::Application.create!(
  name: "PaceLab",
  redirect_uri: "https://pacelab.example/oauth/callback",
  scopes: "read_profile read_workouts"
)

go deeper

for a junior

Recall that default_scopes fill in an empty scope request, optional_scopes must be asked for by name, and both are declared in the Doorkeeper initializer.

for a middle

Explain that an explicit scope replaces the defaults, that a client's registered scopes narrow what it may request, and what happens when neither a scope nor defaults exist.

for a senior

Show you would enable enforce_configured_scopes, because a filled application scopes column replaces the server list and can grant a scope nobody configured.

for a principal

Discuss designing a small, stable scope vocabulary that members understand on a consent screen, and keeping machine clients off member data with scopes_by_grant_type.

## Two lists, one allowed set A **scope** is a named permission a client asks for and a member approves on the consent screen. Doorkeeper declares them in `config/initializers/doorkeeper.rb` with two methods: - `default_scopes :read_profile` lists scopes granted **without being asked for**. - `optional_scopes :read_workouts, :write_workouts` lists scopes granted **only when requested by name**. `Doorkeeper.config.scopes` is the two lists added together. Every scope a request names must appear in the allowed set, otherwise the request fails with `invalid_scope`, and the consent screen describes each scope through the `doorkeeper.scopes` keys in your locale file. ## What a token gets The rule, applied on the authorization request and on token-endpoint grants alike: 1. If the request carries a non-empty `scope`, the token gets **exactly** those scopes, provided each is allowed. 2. If `scope` is missing, Doorkeeper falls back to `default_scopes`, intersected with the client's registered scopes when the client has any. 3. If `scope` is missing and `default_scopes` is empty, the authorization request fails with `invalid_request` for the missing parameter. Applied to a running-club provider with `default_scopes :read_profile` and `optional_scopes :read_workouts, :write_workouts`: | `scope` in the request | Application's `scopes` | Token gets | |---|---|---| | none | blank | `read_profile` | | none | `read_profile read_workouts` | `read_profile` | | none | `read_workouts` | refused: defaults and client share nothing | | `read_workouts` | blank | `read_workouts` only | | `read_workouts write_workouts` | `read_profile read_workouts` | refused: `write_workouts` not allowed for this client | Row four is the one people get wrong: the defaults are a **fallback**, not a base set, so that token fails a bare `doorkeeper_authorize!` that checks `read_profile`. ## The application's own scopes Each `Doorkeeper::Application` row has a `scopes` column. When it is blank the client may request anything in the server's set. When it is filled, Doorkeeper's `ScopeChecker` uses it **instead of** the server's set: `app_scopes.presence || server_scopes`. That makes per-client restriction easy, since a read-only partner is registered with `scopes: "read_profile read_workouts"`, but it has a sharp edge. Because the application's list replaces the server's, an application saved with a scope nobody configured, say `admin`, can be granted that scope. Without extra configuration Doorkeeper does not check the column against the initializer. `enforce_configured_scopes` adds a model validation, so `Doorkeeper::Application` refuses to save scopes outside `default_scopes` and `optional_scopes`. Turn it on whenever applications are created through a form. ## Scopes per grant type `scopes_by_grant_type` narrows the set further by grant: `scopes_by_grant_type client_credentials: [:read_workouts]` stops machine clients from ever holding `write_workouts`. Grant types left out of the hash keep the full allowed set. ## Checking a token's scopes - `doorkeeper_token.scopes` returns a `Doorkeeper::OAuth::Scopes` object; `doorkeeper_token.includes_scope?(:write_workouts)` checks one in code. - `GET /oauth/token/info` returns the scopes of the token presented, handy when debugging a client. ## Common misreadings - **"Defaults are always included."** They replace an empty `scope`, nothing more; a client that names any scope gets only what it named. - **"Optional means automatic."** An optional scope is never granted unless the request names it and the client is allowed it. - **"The application's scopes are just a label."** They are the allowed set for that client whenever the column is filled. - **"Doorkeeper validates application scopes out of the box."** Only with `enforce_configured_scopes`. A good habit is to treat the initializer as the single source of truth for the scope vocabulary: enable `enforce_configured_scopes`, keep the list short, and add a scope only together with the controller filter that checks it and the locale entry that explains it to members on the consent screen. A scope nobody checks is a promise the API does not keep. ## Summary | Setting | Effect | |---|---| | `default_scopes` | fallback for an empty `scope`; checked by a bare `doorkeeper_authorize!` | | `optional_scopes` | grantable only when requested by name | | application `scopes` | replaces the server's set for that client when filled | | `enforce_configured_scopes` | validates application scopes against the initializer | | `scopes_by_grant_type` | limits scopes per grant type |

  • What happens if a client omits scope and default_scopes is empty?
    The authorization request fails with `invalid_request`, naming the missing `scope` parameter, because Doorkeeper has nothing to fall back to. Declaring one harmless default, such as `read_profile`, lets clients omit `scope` and still get a usable token.
  • How do you stop client-credentials tokens from ever carrying write_workouts?
    Add `scopes_by_grant_type client_credentials: [:read_workouts]` to the initializer. Doorkeeper then checks a client-credentials request's scopes against that list as well as the allowed set, and refuses anything else with `invalid_scope`. Grant types missing from the hash are not narrowed.

  • default_scopes are added to every token, whatever the client asks for.
  • optional_scopes are granted automatically once a client is registered.
  • A client can always request any scope listed in optional_scopes.
  • An application's scopes column only labels the consent screen.
  • Doorkeeper rejects unconfigured application scopes even without enforce_configured_scopes.