The Device Grant Flow (Device Authorization Grant) allows a CLI or device with limited input capabilities to obtain an access token. The process follows these steps:
- Request Device Authorization: The client requests a device code and a verification URI from the OAuth2 server.
- User Authentication: The user is presented with a
verification_uri (and sometimes a user code) to visit in a web browser to authorize the request. - Polling: The CLI automatically polls the token endpoint using the
device_code at a specified interval (defaulting to 5 seconds if not provided by the server). - Token Exchange: Once the user completes the authorization in the browser, the polling succeeds and the CLI exchanges the device code for an access token.
During polling, the CLI handles specific OAuth2 error codes like authorization_pending and slow_down by continuing to wait or adjusting the polling frequency.
// Note: This is a conceptual representation of the internal flow logic
// used by the OAuth2c CLI for the Device Grant.
// 1. Request authorization
authReq, authRes, err := oauth2.RequestDeviceAuthorization(ctx, clientConfig, serverConfig, httpClient)
// 2. User visits the URI
verificationURI := authRes.VerificationURI
// 3. Polling loop (internal to the CLI)
// The CLI waits for the user to authorize via the browser...
// 4. Exchange for token
tokenReq, tokenRes, err := oauth2.RequestToken(ctx, clientConfig, serverConfig, httpClient, oauth2.WithDeviceCode(authRes.DeviceCode))