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 app | Use this grant |
|---|---|
| Web app with a back end | Authorization code with PKCE, confidential client |
| Single-page app in the browser | Authorization code with PKCE, public client |
| Mobile or desktop app | Authorization code with PKCE in the system browser |
| TV, console or device without a good browser | Device authorization grant |
| Service calling an API as itself | Client credentials |
| Service calling another API for a user | Token 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.
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
- Choose who gets the token: a person who signs in, a service acting as itself, or a service acting for a user.
- If a person signs in, choose where the app runs.
- Read the recommended flow and why it fits.
- Apply the Keycloak client settings it lists.
- 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.