OAuth Device Authorization Grant

On this page

The Device Authorization GrantAn OAuth 2.0 grant type for devices with limited input, such as TVs. The user completes the login on a second device. logs users in on devices with no browser or keyboard, such as a TV. The device shows a short code. The user enters the code on a phone or a computer and logs in there. The device then gets the tokens. The screenshots come from the Device Grant example, which also works as a code reference.

Configure the Application#

On the OAuth tab of the Application, enable Device in Enabled grants and set Device Verification URL. For the other OAuth settings, see Configure Application OAuth Settings.

Set Device Verification URL to a page in your app, so your app can pass the Tenant Id through the flow. The page can host your own form. The page can also redirect to the themed FusionAuth device page at /oauth2/device, which requires the client_id and tenantId query parameters. After the user submits the code on the FusionAuth device page, FusionAuth logs the user in with an Authorization Code grant and redirects to the redirect_uri. FusionAuth approves the device when your app exchanges the authorization code for tokens.

A device can't keep a client secret. To let the device poll the Token endpoint without a client secret, set Client Authentication to Not required. When Client Authentication is Required or Not required when using PKCE, the device must send the client secret. For more information, see the Client Secret table.

Version

Before FusionAuth 1.71.0, the Token endpoint didn't apply Client Authentication to the Device Authorization grant. After you update to 1.71.0 or later, a device that polls without a client secret fails with an invalid_client error unless Client Authentication is Not required.

To change how long the device code and user code stay valid, or how FusionAuth generates user codes, use the Advanced tab of the Tenant.

Start the Flow on the Device#

The device sends a request to the Device Authorize endpoint. The OpenID Configuration also lists the endpoint.

To request scopes on the tokens, send scope. To get a refresh token, add offline_access, and enable Generate refresh tokens on the OAuth tab of the Application. FusionAuth applies the scopes when the user enters the code, as described in Pass the User Code to FusionAuth. For scope policies, custom scopes, and consent, see Scopes.

The device prompts the user to connect.
The device prompts the user to connect.

The example adds line breaks for readability.

Example HTTP Request
POST /oauth2/device_authorize HTTP/1.1
Host: piedpiper.fusionauth.io
Content-Type: application/x-www-form-urlencoded

client_id=06494b74-a796-4723-af44-1bdb96b48875
    &scope=offline_access

The response holds the values the device needs for the rest of the flow:

Example JSON Response
{
  "device_code": "e6f_lF1rG_yroI0DxeQB5OrLDKU18lrDhFXeQqIKAjg",
  "expires_in": 600,
  "interval": 5,
  "user_code": "SFYNPV",
  "verification_uri": "https://piedpiper.fusionauth.io/oauth2/device",
  "verification_uri_complete": "https://piedpiper.fusionauth.io/oauth2/device?user_code=SFYNPV"
}

Poll the Token Endpoint#

The device sends the device_code to the Token endpoint every interval seconds. Until the user approves the device, FusionAuth returns an authorization_pending error. The example adds line breaks for readability.

Example HTTP Request
POST /oauth2/token HTTP/1.1
Host: piedpiper.fusionauth.io
Content-Type: application/x-www-form-urlencoded

client_id=06494b74-a796-4723-af44-1bdb96b48875
    &device_code=e6f_lF1rG_yroI0DxeQB5OrLDKU18lrDhFXeQqIKAjg
    &grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code

While the user hasn't approved the device, FusionAuth returns the following error:

Example Pending JSON Error Response
{
  "error": "authorization_pending",
  "error_description": "The authorization request is still pending."
}

After the device code expires, FusionAuth returns the following error, and the device must start over:

Example Expired JSON Error Response
{
  "error": "expired_token",
  "error_description": "The device_code has expired, and the device authorization session has concluded."
}

For an unknown device code, FusionAuth returns the following error:

Example Invalid JSON Error Response
{
  "error": "invalid_request",
  "error_reason": "invalid_device_code",
  "error_description": "The request has an invalid parameter: device_code"
}

Show the Code to the User#

The device shows the user_code and asks the user to visit the verification_uri. To save the user from typing the code, the device can show a QR code for the verification_uri_complete, which fills in the code.

The device shows the user code and the verification URL.
The device shows the user code and the verification URL.

The user visits the URL and enters the code:

The FusionAuth device page, where the user enters the user code.
The FusionAuth device page, where the user enters the user code.

Pass the User Code to FusionAuth#

FusionAuth approves the device after the user enters the code and logs in. Collect the code in one of the following ways:

Version

Before FusionAuth 1.71.0, an app that hosted the code form could send the code in the user_code parameter of the Authorization Code or Implicit grant. FusionAuth approved the device when the login completed. In 1.71.0 and later, those grants approve the device only when the user entered the code on the FusionAuth device page. The Password grant still approves the device from the user_code parameter. For more information, see the Token endpoint.

The device confirms that the user connected.
The device confirms that the user connected.

Approve the Device From Your App#

When your app hosts the code form, validate the code with the Device Validate endpoint. For a valid code, the endpoint returns a 200 response with no body. Then log the user in with any FusionAuth login method, and send the user's access token and the user code to the Device Approve endpoint. The access token must belong to the same Application as the device. An access token from the Login API or the Passwordless API works when the login request includes the Application Id.

The following example approves a device after a passwordless loginAny login that doesn't ask for a password, such as passkeys or one-time passwords. The Passwordless API handles one-time passwords only.:

FusionAuthYour AppUserDeviceFusionAuthYour AppUserDevicePOST /oauth2/device_authorizedevice_code and user_codeShow the user_codeEnter the user_codeGET /oauth2/device/validatePOST /api/passwordless/startPasswordless codeSend the code by email or SMSReturn with the codePOST /api/passwordless/loginAccess tokenPOST /oauth2/device/approvePOST /oauth2/tokenTokens
FusionAuthYour AppUserDeviceFusionAuthYour AppUserDevicePOST /oauth2/device_authorizedevice_code and user_codeShow the user_codeEnter the user_codeGET /oauth2/device/validatePOST /api/passwordless/startPasswordless codeSend the code by email or SMSReturn with the codePOST /api/passwordless/loginAccess tokenPOST /oauth2/device/approvePOST /oauth2/tokenTokens

A device grant that your app approves after a passwordless login.

The device starts the flow and polls the Token endpoint as usual. Your app handles the rest of the flow:

  1. Collect the user code and the user's login Id, such as an email address. Validate the code with the Device Validate endpoint.

  2. Call the Start Passwordless endpoint with the user's login Id and the applicationId of the device's Application. FusionAuth returns a passwordless code.

  3. Deliver the code to the user. To have FusionAuth send the code by email or SMS, call the Send Passwordless endpoint.

  4. When the user returns with the code, call the Complete Passwordless endpoint. The response holds the access token in the token field.

  5. Call the Device Approve endpoint with the access token and the user code. When Client Authentication is Required or Not required when using PKCE, also send the client secret. Your app runs on a server, so your app can keep the secret. The example adds line breaks for readability.

    Example HTTP Request
    POST /oauth2/device/approve HTTP/1.1
    Host: piedpiper.fusionauth.io
    Content-Type: application/x-www-form-urlencoded
    
    client_id=06494b74-a796-4723-af44-1bdb96b48875
        &client_secret=f6MNmsHhGbqn2Ew1PU3BXsEu6lfpj7hW3w2Y2Yd5Uvc
        &token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
        &user_code=SFYNPV
    

    For a successful request, the response holds a deviceGrantStatus of Approved.

The next poll from the device returns the tokens. The tokens carry the scopes that the device requested from the Device Authorize endpoint. For a third-party Application, FusionAuth keeps only the scopes in the scope claim of the approving access token, plus openid and offline_access.

When another user already entered the code on the FusionAuth device page, the Device Approve endpoint rejects the request with an invalid_user_code error.

Receive the Tokens#

After the user logs in, the next poll from the device returns the tokens:

Example JSON Response
{
  "access_token" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODUxNDA5ODQsImlhdCI6MTQ4NTEzNzM4NCwiaXNzIjoiYWNtZS5jb20iLCJzdWIiOiIyOWFjMGMxOC0wYjRhLTQyY2YtODJmYy0wM2Q1NzAzMThhMWQiLCJhcHBsaWNhdGlvbklkIjoiNzkxMDM3MzQtOTdhYi00ZDFhLWFmMzctZTAwNmQwNWQyOTUyIiwicm9sZXMiOltdfQ.Mp0Pcwsz5VECK11Kf2ZZNF_SMKu5CgBeLN9ZOP04kZo",
  "expires_in" : 3600,
  "refresh_token": "ze9fi6Y9sMSf3yWp3aaO2w7AMav2MFdiMIi2GObrAi-i3248oo0jTQ",
  "scope": "offline_access",
  "token_type" : "Bearer",
  "userId" : "3b6d2f70-4821-4694-ac89-60333c9c4165"
}