skip to content

With Doorkeeper as your OAuth provider, how do you set up a public mobile client so its authorization code flow must use PKCE?

level: seniorimportance: should knowfreq 18%

answer

  1. confidential: false on the application
  2. grant_flows default: authorization_code, client_credentials
  3. doorkeeper:pkce adds code_challenge columns
  4. force_pkce binds only non-confidential clients
  5. pkce_code_challenge_methods defaults to plain and S256

basics

~10 s

Register the client with confidential: false, run Doorkeeper's doorkeeper:pkce migration so grants can store a code_challenge, enable force_pkce, and narrow pkce_code_challenge_methods to S256; without the columns Doorkeeper never compares the code_verifier.

solid answer

~40 s

A mobile app cannot keep a secret, so its `Doorkeeper::Application` is saved with `confidential: false`, and Doorkeeper then accepts it by `uid` alone at the token endpoint. The default `grant_flows` are `authorization_code` and `client_credentials`, so the code flow is already on and the password and implicit grants stay off. PKCE needs `code_challenge` and `code_challenge_method` columns on `oauth_access_grants`; the base migration lacks them, so run `bin/rails generate doorkeeper:pkce`. `force_pkce` then refuses a non-confidential client's authorization request without a `code_challenge` and its token request without a `code_verifier`. `pkce_code_challenge_methods ["S256"]` refuses `plain`, and `use_refresh_token` lets the app renew tokens without another browser round trip. The trap: without the columns, `force_pkce` only checks that both parameters are present and never compares them.

code

bash · 2 lines
bash
bin/rails generate doorkeeper:pkce
bin/rails db:migrate

go deeper

for a junior

Recall that a mobile app is a public client, saved with confidential: false, and that Doorkeeper's force_pkce makes such clients use PKCE in the authorization code flow.

for a middle

Explain the doorkeeper:pkce migration, what force_pkce checks on the authorization and token requests, and how pkce_code_challenge_methods narrows accepted methods to S256.

for a senior

Show you verify PKCE end to end: confirm the columns exist and keep a test that sends a wrong code_verifier and expects invalid_grant, since a missing migration fails silently.

for a principal

Weigh which first-party and partner apps may register as public clients, how their refresh tokens are bounded, and how provider configuration is audited for drift.

## Public and confidential clients in Doorkeeper Every `Doorkeeper::Application` has a boolean `confidential` column, `true` by default. A **confidential** client, such as a partner's server, can keep its `secret` and must present it at the token endpoint. A **public** client, such as the club's iOS app, ships its code to every phone, so any secret in it is public. Saving the application with `confidential: false` tells Doorkeeper to accept that client by `uid` alone: `by_uid_and_secret` returns it when the secret is blank. The generated migration still declares `secret` as `null: false`, so Doorkeeper generates a secret anyway; the client simply never needs it. Without a secret, the authorization code is the only thing standing between a stolen redirect and a token, which is why public clients use PKCE. ## Which grant flows are on | Setting | On by default | Notes | |---|---|---| | `authorization_code` in `grant_flows` | yes | the flow a mobile app uses | | `client_credentials` in `grant_flows` | yes | machine clients, no member involved | | `password` in `grant_flows` | no | also needs a `resource_owner_from_credentials` block | | `implicit` in `grant_flows` | no | returns tokens from the authorization endpoint | | `use_refresh_token` | no | adds `refresh_token` to the token endpoint | `grant_flows %w[authorization_code]` drops client credentials as well, when no machine clients exist. ## Turning PKCE on 1. Run `bin/rails generate doorkeeper:pkce` and `bin/rails db:migrate`. The migration adds `code_challenge` and `code_challenge_method` to `oauth_access_grants`. 2. Add `force_pkce` to the initializer. 3. Add `pkce_code_challenge_methods ["S256"]`; the default is `%w[plain S256]`. 4. Register the app with `confidential: false` and a redirect URI such as `clubrunner://oauth/callback`. ## What force_pkce does and does not do - On the **authorization request**, a non-confidential client without a `code_challenge` gets `invalid_request` ("Code challenge is required."). - On the **token request**, a non-confidential client without a `code_verifier` gets `invalid_request` for the missing parameter. - **Confidential** clients are exempt. If one sends a `code_challenge` anyway, the grant stores it and the token request must carry a matching `code_verifier`, or it fails with `invalid_grant`. - Whenever a challenge is sent, Doorkeeper insists on an explicit `code_challenge_method` from the accepted list, else `invalid_code_challenge_method`. ## The silent failure Doorkeeper decides whether PKCE is available by asking whether `oauth_access_grants` has a `code_challenge` column (`pkce_supported?`). If the `doorkeeper:pkce` migration never ran: - the challenge is dropped when the grant is saved; - the method check is skipped; - the verifier comparison returns true for any value; - `force_pkce` still demands both parameters, so clients see a working flow. Nothing is logged. Two checks catch it: confirm the columns exist in `db/schema.rb`, and keep a request test that sends a **wrong** `code_verifier` and expects `invalid_grant`. ## Refresh tokens for the app `use_refresh_token` makes code-flow (and password) token responses include a `refresh_token`, and enables `grant_type=refresh_token` at `/oauth/token`. Client-credentials and implicit tokens never get one. The block form, `use_refresh_token { |context| ... }`, decides per request from `context.client`, `context.grant_type` and `context.scopes`. ## Checking the setup from a console - `Doorkeeper::AccessGrant.pkce_supported?` returns `true` only once the columns exist. - `Doorkeeper.config.pkce_code_challenge_methods_supported` returns `["S256"]` here, and `[]` when the columns are missing, which is the quickest way to spot the silent failure. - `Doorkeeper.config.force_pkce?` returns `true`. - The mobile client's `confidential?` returns `false`. ## What the app sends With this configuration the iOS app's two requests look like this: - **Authorization request** (in the system browser): `response_type=code`, `client_id`, `redirect_uri`, `scope`, `state`, `code_challenge` and `code_challenge_method=S256`. - **Token request** (`POST /oauth/token`): `grant_type=authorization_code`, `code`, `redirect_uri`, `client_id` and `code_verifier`, with no `client_secret`. Doorkeeper finds the public client by `client_id`, loads the grant by the code, checks that the grant belongs to that client and that the redirect URI matches, then compares the verifier with the stored challenge before issuing tokens. ## Common misreadings - **"force_pkce covers every client."** It covers non-confidential ones; confidential clients are checked only when they send a challenge. - **"The base migration is enough."** PKCE needs the `doorkeeper:pkce` columns. - **"A public client needs its secret."** It is accepted by `uid` alone. - **"Misconfiguration will be loud."** The missing-columns case is silent.

  • Does a confidential web client get PKCE checks when force_pkce is on?
    Only if it opts in. `force_pkce` exempts confidential clients, but when any client sends a `code_challenge`, the grant stores it and the token request must carry a matching `code_verifier`, or it fails with `invalid_grant`. Doorkeeper also requires the authorization request to name a `code_challenge_method` from the accepted list whenever it sends a challenge.
  • What does the block form of use_refresh_token let you decide?
    `use_refresh_token { |context| ... }` decides per token request. The block receives a context with `client`, `grant_type` and `scopes` and returns true or false, so you can, for example, issue refresh tokens only when `context.grant_type == Doorkeeper::OAuth::AUTHORIZATION_CODE`. Client-credentials tokens never get one, whatever the block returns.

  • force_pkce makes every client, confidential ones included, send a code_challenge.
  • The doorkeeper:migration tables already include the code_challenge columns.
  • With force_pkce on, a missing PKCE migration makes Doorkeeper fail loudly.
  • A public client must still send its client_secret to the token endpoint.
  • Doorkeeper enables the password grant by default for first-party apps.

Assumes Doorkeeper 5.9; force_pkce arrived in 5.7.1 and pkce_code_challenge_methods in 5.8.0, so earlier 5.x installs have neither option.