Skip to content

OAuth grant type chooser

Answer two questions about your app to see which OAuth 2.0 flow fits, why, and how to set up the client in Keycloak.

Runs in your browser. Nothing you paste is sent to us.

Your app

Who gets the token?
Where does the app run?
Two flows to avoid
The implicit flow returns tokens in the URL, where they leak through history and logs, and RFC 9700 says not to use it. The password grant gives the app the user’s password and rules out multi-factor sign-in, single sign-on and passkeys; RFC 9700 says it must not be used. The code flow with PKCE replaces both.

Use this

recommended

Authorization code flow with PKCE, confidential client

Your back end can keep a client secret, and the tokens never reach the browser. PKCE adds protection against stolen codes.

steps
  1. Redirect the user to the authorization endpoint with state, nonce and a code_challenge.
  2. On the callback, check state and exchange the code at the token endpoint with the client secret and code_verifier.
  3. Keep the tokens on the server and give the browser a session cookie.
Keycloak client
  • Client authentication: on
  • Standard flow: on; everything else off unless you need it
  • Valid redirect URIs: the exact callback URLs
  • PKCE method: S256

How to use it

  1. Choose who gets the token: a person, a service acting as itself, or a service acting for a user.
  2. If a person signs in, choose where the app runs.
  3. Read the recommended flow, its steps and the Keycloak client settings.

What to know

For almost every app where a person signs in, the answer is the authorization code flow with PKCE. What changes is whether the client is confidential and how the redirect comes back.

Services calling an API as themselves use client credentials. Devices without a browser use the device flow. Services calling another API for a user use token exchange.

The implicit and password grants are not recommended for new apps. RFC 9700 says not to use the implicit flow, because tokens leak through the URL, and says the password grant must not be used, because the app sees the password.

What a grant type is

A grant type is the way a client obtains an access token. RFC 6749, the OAuth 2.0 framework, defined four: authorization code, implicit, resource owner password credentials and client credentials. Later specifications added more, such as the device grant in RFC 8628 and token exchange in RFC 8693. Refresh tokens are a grant too: they buy new tokens without asking the user again.

Each grant fits a different situation. The question is always the same: who is the token for, and where does the code that asks for it run? A browser app, a server, a phone, a television and a background job each have different abilities. Some can keep a secret, some can show a browser, some have no user at all.

The short answer

Your appUse this grant
Web app with a back endAuthorization code with PKCE, confidential client
Single-page app in the browserAuthorization code with PKCE, public client
Mobile or desktop appAuthorization code with PKCE in the system browser
TV, console or device without a good browserDevice authorization grant
Service calling an API as itselfClient credentials
Service calling another API for a userToken exchange

For almost every app where a person signs in, the answer is the authorization code flow with PKCE. What changes is whether the client can keep a secret and how the browser comes back to it.

How the authorization code flow works

Because it is the default answer, it is worth knowing the steps. The app sends the browser to the authorization endpoint. The user signs in there, never in the app. The server redirects back to the app with a short-lived code. The app then trades the code for tokens over a direct back-channel request.

PKCE (RFC 7636) adds a secret that only the app knows. The app sends a hash of it in the first step and the secret itself in the last, so a stolen code is useless to anyone else.

Sample token request for the code flow (made-up values)
POST /realms/acme/protocol/openid-connect/token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=SAMPLE_CODE
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&client_id=web-app&code_verifier=SAMPLE_VERIFIER

What this tool does

The chooser asks two questions: who gets the token, and, if a person signs in, where the app runs. It then shows the recommended flow, why it fits, the steps in short, the settings for a Keycloak client, and links to our tools that build each request.

It does not inspect your app or your Keycloak realm. It gives the usual recommendation for each kind of app, based on RFC 9700 and RFC 8252. Unusual setups, such as a browser app that must work without any server, may need more thought.

How to use the chooser

  1. Choose who gets the token: a person who signs in, a service acting as itself, or a service acting for a user.
  2. If a person signs in, choose where the app runs.
  3. Read the recommended flow and why it fits.
  4. Apply the Keycloak client settings it lists.
  5. Open the linked tools to build the requests for your realm.

Each case in detail

Web apps with a back end

The server keeps a client secret, so the client is confidential. It runs the code flow with PKCE, keeps tokens on the server and gives the browser only a session cookie. In Keycloak: Client authentication on, Standard flow on, exact Valid redirect URIs, PKCE method S256.

Single-page apps

Code in the browser cannot keep a secret, so the client is public and PKCE carries the protection. Keep tokens in memory rather than in local storage, and keep access tokens short-lived. A back end for front end, a small server that runs the confidential flow and hands the browser only a cookie, keeps tokens out of the browser altogether. In Keycloak: Client authentication off, Standard flow on, Implicit flow off, Web origins set so the browser may call the token endpoint.

Mobile and desktop apps

RFC 8252 says native apps should sign in through the system browser, not an embedded web view, so the user’s password never passes through the app and single sign-on works. The redirect comes back on a claimed https link, a private URI scheme, or a loopback address with any port. Keycloak ignores the port for http://localhost and http://127.0.0.1 redirect URIs.

Devices and command-line tools

A television or console has no good browser or keyboard. With the device grant, it shows a short code, the user signs in on a phone or laptop, and the device polls the token endpoint. In Keycloak, turn on OAuth 2.0 Device Authorization Grant for the client. A command-line tool on a computer that has a browser can use the code flow with a loopback redirect instead.

Machine-to-machine

A background job or service with no user posts its own credentials to the token endpoint and gets a token for itself. There is no refresh token; it simply asks again. In Keycloak: Client authentication on, Service accounts roles on, Standard flow off, and roles granted to the service account.

Calling an API for a user

A service that received a user’s token and must call another API can exchange it for a token meant for that API, rather than forwarding the original. Since Keycloak 26.2, standard token exchange is turned on per client in its capability settings.

Where refresh tokens fit

The refresh token grant is not a way to sign in. It keeps a session going after one of the other grants. The code flow and the device flow usually return a refresh token along with the access token, so the app can get new access tokens without sending the user back to the sign-in page.

Client credentials should not return a refresh token (RFC 6749 section 4.4.3). The service can simply ask again with its own credentials.

In Keycloak, a refresh token stops working when the user’s session ends. Asking for the offline_access scope gives an offline token instead, which survives the session. Use it only for apps that truly work while the user is away, and store it as carefully as a password.

Flows to avoid

Two grants from RFC 6749 are no longer recommended. RFC 9700, the OAuth security best current practice, explains why.

The implicit grant

The implicit flow returns the access token directly in the redirect URL. Tokens in URLs leak through browser history, logs and referrer headers, and the flow cannot bind the token to the client that asked for it. RFC 9700 says clients should not use it. The code flow with PKCE replaces it for browser apps.

The password grant

With the resource owner password credentials grant, the app collects the user’s password and sends it to the token endpoint. The app sees the password, and the flow cannot do multi-factor sign-in, single sign-on or passkeys. RFC 9700 says it must not be used. Move old apps that rely on it to the code flow.

Common mistakes

  • Shipping a client secret inside a browser or mobile app. Anyone can extract it; use a public client with PKCE.
  • Using client credentials to act for a user. The token then describes the service, not the person.
  • Using an embedded web view for sign-in in a native app.
  • Leaving Implicit flow or Direct access grants on in Keycloak for clients that do not need them.
  • Registering wildcard redirect URIs where an exact one would do.

Best practices for every flow

  • Turn on only the flows each client needs.
  • Use PKCE with S256 in every code flow, confidential clients included, as RFC 9700 recommends.
  • Keep access tokens short-lived and use refresh tokens where a user is present.
  • Restrict tokens to the audience that uses them.
  • Prefer a signed JWT or mutual TLS over a shared secret for confidential clients when you can.

Running Keycloak well is part of getting flows right. If you would rather have it managed, see our plans.

Questions

Frequently asked questions

Which OAuth flow should a single-page app use?

The authorization code flow with PKCE, as a public client. A back end for front end, which keeps tokens on a server and gives the browser a cookie, adds more protection.

Which OAuth flow should a mobile app use?

The authorization code flow with PKCE in the system browser, with a claimed https link or a private URI scheme as the redirect. RFC 8252 describes this pattern.

Is the implicit flow deprecated?

RFC 9700 says clients should not use it, and OAuth 2.1, which gathers current best practice, leaves it out. Tokens in the URL leak, and the code flow with PKCE now works in browsers.

Is the password grant ever acceptable?

Not for new apps. RFC 9700 says it must not be used. It exposes the password to the app and blocks multi-factor sign-in, single sign-on and passkeys.

When should I use client credentials?

When no person is involved, such as a scheduled job or one service calling another as itself. The token describes the client, so do not use it to act for a user.

Do confidential clients need PKCE?

Yes, it is recommended. RFC 9700 recommends PKCE for all clients because it also protects against codes injected into another user’s session.

How should a command-line tool sign users in?

If the computer has a browser, open it with the code flow and receive the redirect on a loopback address. If it may run where there is no browser, such as over SSH, use the device flow.

What is the difference between a public and a confidential client?

A confidential client can keep a secret, such as a server. A public client cannot, such as a browser or mobile app. In Keycloak, Client authentication on makes a client confidential.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.