Why the device flow exists
The usual OAuth sign-in sends the user to a web page and back to the app through a redirect. That works when the app runs in a browser or can open one. It does not work well on a smart TV, a game console, a printer or a terminal on a remote server. These devices either have no browser or have one that is painful to type into.
The OAuth 2.0 Device Authorization Grant, RFC 8628, solves this by splitting the work across two devices. The device asks for a short code and shows it. The user opens a URL on a phone or laptop, signs in there and enters the code. Meanwhile the device polls the token endpoint until the server says the user is done.
Because the user signs in on their own device, they can use everything the server offers: passwords, one-time codes, passkeys and identity providers. The limited device never sees the password.
How the flow works, step by step
1. Device authorization request
The device sends a POST to the device authorization endpoint with its client_id and the scopes it wants.
POST /realms/acme/protocol/openid-connect/auth/device HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
client_id=tv-app&scope=openid%20profile{
"device_code": "SAMPLE_DEVICE_CODE",
"user_code": "WDJB-MJHT",
"verification_uri": "https://auth.example.com/realms/acme/device",
"verification_uri_complete": "https://auth.example.com/realms/acme/device?user_code=WDJB-MJHT",
"expires_in": 600,
"interval": 5
}2. User interaction
The device shows verification_uri and user_code. It can also show a QR code of verification_uri_complete, which carries the code so the user does not have to type it. The user signs in on the second device and confirms.
3. Polling the token endpoint
While the user works, the device polls the token endpoint with grant_type=urn:ietf:params:oauth:grant-type:device_code and the device_code. It waits interval seconds between attempts, or 5 seconds if the server gave none.
POST /realms/acme/protocol/openid-connect/token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code
&device_code=SAMPLE_DEVICE_CODE&client_id=tv-appWhen the user finishes, the poll returns a normal token response.
Polling responses and what to do
RFC 8628 section 3.5 defines the answers a device gets while it polls. Handle each one explicitly.
| Response | Meaning | What the device does |
|---|---|---|
authorization_pending | The user has not finished. | Wait interval seconds and poll again. |
slow_down | Still pending, and polling too fast. | Add 5 seconds to the interval, then keep polling. |
access_denied | The user declined. | Stop and tell the user. |
expired_token | The device code expired. | Start again with a new device authorization request. |
| 200 with tokens | The user approved. | Store the tokens and stop polling. |
What this tool does and does not do
The builder writes both requests of the flow, the device authorization request and the polling request, as curl commands and raw HTTP.
- It builds Keycloak’s device and token endpoints from a server URL and realm, or takes two custom URLs.
- It defaults to a public client, which suits most devices, and also supports secrets and signed JWTs.
- It shows a sample device authorization response and a table of polling answers.
It does not run the flow, poll for you or keep a timer. Paste the device_code from your real response into the polling field, then run the command yourself at the interval the server gave.
How to use the builder
- Enter your Keycloak server URL and realm, or switch to Custom URL and paste both endpoints.
- Enter the scopes. Add
offline_accessonly if the device must keep working after the session ends. - Set the client id and client authentication.
- Run the first request and note
device_code,user_codeandinterval. - Open the verification URL in a browser, sign in and enter the user code.
- Paste the device code into the builder and run the polling request until tokens come back.
Where the device flow fits
TVs, consoles and kiosks
This is the classic case: a big screen, a remote control and no keyboard. Show a short code and a QR code, and let the user finish on a phone.
Command-line tools
A CLI that may run over SSH, inside a container or on a headless server cannot rely on opening a browser. The device flow lets the user approve from any machine. On a desktop with a browser, the code flow with a loopback redirect is usually smoother, because the user does not have to copy a code.
Devices without a screen keyboard
Smart speakers, printers and other appliances can show a code on a small display or read it aloud, then poll. The only requirements are an internet connection and a way to show the user a short code.
Building the polling loop
Most bugs in device flow clients sit in the polling loop. A correct loop is short, but each branch matters.
- Read
intervalfrom the device authorization response, or use 5 seconds. - Compute a deadline from
expires_inand stop when it passes. - Wait the interval, then send the polling request.
- On
authorization_pending, loop again with the same interval. - On
slow_down, add 5 seconds to the interval for all later polls. - On
access_deniedorexpired_token, stop and tell the user what happened. - On a 200 response, store the tokens and leave the loop.
Treat network errors and 5xx answers as temporary: wait and try again, but stay within the deadline. RFC 8628 says a client must slow down after a connection timeout and recommends doubling the interval each time. Never poll faster than the server asks.
Run the loop in the background so the device can still show the code, a countdown and a cancel button.
How Keycloak handles the device flow
Keycloak supports RFC 8628. The device authorization endpoint is https://<host>/realms/<realm>/protocol/openid-connect/auth/device, and polling goes to the usual token endpoint.
- Turn on OAuth 2.0 Device Authorization Grant in the client’s capability settings.
- Devices usually run as public clients, with Client authentication off, because a secret on a device can be extracted.
- The discovery document lists the endpoint as
device_authorization_endpointand the grant ingrant_types_supported.
If you would rather not run Keycloak yourself, our plans cover a managed, upstream Keycloak on a cluster of your own.
Common errors and how to fix them
unauthorized_clienton the first request: the grant is not enabled for the client. Turn on OAuth 2.0 Device Authorization Grant.invalid_client: the client id is wrong, or the client is confidential and no credentials were sent.slow_downagain and again: the device does not add 5 seconds after each one. Keep the larger interval for the rest of the flow.expired_token: the user took longer thanexpires_in. Start over and show a fresh code.invalid_grantwhile polling: the device code was already used or belongs to another client.
The OAuth error lookup explains any other code you see.
Security notes
The device flow has one known weakness: an attacker can start a flow, then trick someone into entering the attacker’s user code on a real sign-in page. RFC 8628 section 5.4 discusses this remote phishing risk. Show the user which device and app they are approving, and keep codes short-lived.
User codes are short so people can type them, which also makes them guessable. RFC 8628 asks servers to limit attempts at the verification page. Device codes themselves must stay secret, since anyone holding one can poll for the tokens.
As RFC 9700 advises for all flows, ask only for the scopes the device needs.
Best practices
- Show both the URL and the code in large type, and add a QR code of
verification_uri_completewhen the screen allows. - Respect
intervalandslow_downexactly. - Stop polling at
expires_ininstead of waiting forexpired_token. - Store refresh tokens in the device’s protected storage.
- Let the user cancel and start over from the device.