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 flowThe main OAuth flow: the browser visits the authorization server, where the person signs in and approves access, then carries an authorization code back to the client, which exchanges it for tokens directly. When the client runs on a server, the tokens never pass through the browser. 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 OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. 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 clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret.. 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.readAll 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 endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect. 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-frameThe 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 tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. 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 tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. 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.
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.
Nothing in the protocolAn agreed set of rules for the messages two systems exchange and what each message means. HTTP, OAuth 2.0, OpenID Connect and SAML are all protocols. 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 codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it. 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.
Try it in the Lab
- Approve a photo frame on another screen with device authorizationStart a device authorization as a photo frame, poll too fast, approve it as Ava in another browser, then see a denied code and an expired one end the attempt.