Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Device authorization

Some devices that need a person's access can't show a sign-in page or take typed input. The device authorization grant splits the work: the device asks for access and collects the result, and the person approves on a phone or computer that is easy to use.

Starting on the device

Client credentials worked because the access belonged to the printer itself. Ava's photos are a different matter: only she can approve access to them, even when the application asking runs on a device that makes approving awkward.

Ava has bought a digital photo frame for her living room. It has a screen and a single button, and she wants it to show photos from her photo account. The authorization code flow doesn't fit well. The frame has no convenient browser, and even if it had one, typing an email address and password with a single button would be miserable. Asking Ava to type her password into the frame would bring back the problem OAuth was meant to solve.

The device authorization grant splits the work between two devices. The frame asks for access, and Ava approves it on a device that is easier to use, such as her phone.

The frame is a public client. Every frame of this model runs the same software, so a secret built into it could be extracted from any one of them. It identifies itself with its client ID only and sends a request to the photo service's device authorization endpoint, an endpoint that exists for this grant:

POST /device_authorization HTTP/1.1
Host: auth.photos.example
Content-Type: application/x-www-form-urlencoded

client_id=living-room-frame
&scope=photos.read

All domains, codes and tokens in these examples are fictional.

The response gives the frame two codes for two different audiences:

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store

{
  "device_code": "demo-device-code-4",
  "user_code": "WDJB-MJHT",
  "verification_uri": "https://photos.example/device",
  "verification_uri_complete": "https://photos.example/device?user_code=WDJB-MJHT",
  "expires_in": 900,
  "interval": 5
}

The user_code is meant for Ava. It is short and easy to read and type. The device_code stays with the frame, which uses it later to collect the result, and the frame never shows it on screen.

The frame displays the verification address, verification_uri, and the user code. It might also show a QR code built from verification_uri_complete, the same address with the code already filled in, so Ava can scan it with her phone instead of typing. expires_in says both codes stop working after 900 seconds, fifteen minutes in this example, and interval comes into play when the frame collects the result.

Approving on another device

On her phone, Ava opens photos.example/device. The photo service asks her to sign in if she isn't already signed in, then asks for the code shown on the frame. She types WDJB-MJHT. While Ava signs in and types the code, the frame is already asking for the result.

The service now knows which pending request she means. It shows what is asking and what it wants: Living room frame would like to view your photos. Ava compares the code with the one on the frame and approves.

Notice where each part happened. Ava signed in and made her decision on the photo service, in her phone's browser. The frame never saw her password, and her phone never needed to know anything about the frame except the code she read from its screen.

Polling for the result

The frame can't wait for a redirect, because nothing sends the frame a message when Ava approves. Instead, it asks the token endpoint at intervals, sending the device code. Asking again and again like this is called polling:

POST /token HTTP/1.1
Host: auth.photos.example
Content-Type: application/x-www-form-urlencoded

grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code
&device_code=demo-device-code-4
&client_id=living-room-frame

The grant type is a full URN, a name in the form urn:ietf:params:oauth:grant-type:device_code, rather than a short word like refresh_token. Grants added to OAuth after the original specification are named this way, so new names can't clash with old ones. Here it is percent-encoded because it travels in a form body.

Until Ava finishes, the token endpoint answers with an error that means "not yet":

HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store

{
  "error": "authorization_pending"
}

The frame waits at least interval seconds before asking again. If it asks too quickly, the server replies with slow_down, and the frame adds five seconds to its interval for the rest of the attempt. The frame polls while Ava uses her phone, and once she approves, the next poll returns an access token in the same shape as any other token response. The device code then works only once.

Two other answers end the attempt. access_denied means the request was refused, and expired_token means the codes ran out before anyone finished. In either case the frame stops polling and offers to start again with new codes.

The frame receives two codes and polls for the result. Ava signs in and approves on her phone, using the short code shown on the frame.

A code someone else started

The user code connects Ava's approval to one pending request, whichever device started it. That is also what makes it attractive to an attacker.

Suppose someone starts a device authorization request from their own device, then sends Ava a message: "Your photo account needs verifying. Go to photos.example/device and enter WDJB-MJHT." The page is genuine, and the sign-in is genuine. If Ava enters the code and approves, the attacker's device collects access to her photos on its next poll.

The same exchange, started by someone else's device. Every page the person sees is genuine, and the tokens still go to the device that holds the device code.

Nothing in the protocol can tell whether the person approving is standing in front of the device that asked. The defenses come from the approval screen and from people's habits. The service says that Ava is authorizing a device and names the client that is asking and what it wants, so she has a chance to notice that she isn't setting up a photo frame.

A message could also carry the verification_uri_complete link, with the code already filled in, so Ava never types it at all. When she arrives that way, the service still shows the code and asks her to confirm that it matches the one on her device's screen. If there is no device in front of her showing that code, there is nothing to compare it with, and that is the moment to stop.

The codes expire quickly, which limits how long an invitation like that stays useful. A code only makes sense when you are reading it off your own device's screen; one that arrives in an email or a message from someone else deserves suspicion.

The short user code creates one more risk. Someone could try guessing codes on the verification page, hoping to land on a pending request. The service limits how many wrong codes it accepts and makes codes long enough, compared with the number of requests pending at once, that guessing is impractical.

Use this grant for devices that can't reasonably show a browser or accept typed input, such as televisions, photo frames and command-line tools running on a remote server with no browser of their own. An application that can open a browser on the same device should use the authorization code flow, where the approval and the result stay together in one place.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

The frame polls the token endpoint and receives authorization_pending. What should it do? 1 of 3

Choose one answer.

Ava gets a message asking her to enter a code at the photo service's genuine device page. She is not setting up any device. What is the risk? 2 of 3

Choose one answer.

Ava's frame polls every two seconds and the token endpoint answers slow_down. What should the frame do for the rest of this attempt? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity