Device authorization grant
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
The device authorization grant (RFC 8628) lets an input-constrained program obtain tokens while the user approves on
another device. The endpoints are Spring Authorization Server’s: POST /oauth2/device_authorization (framework owned) and
/oauth2/device_verification, whose page is custom and defined in the pages contract.
Enabling the endpoints
/**
* Enables the OAuth 2.0 Device Authorization Grant on the protocol chain. The device authorization endpoint
* ({@code POST /oauth2/device_authorization}) is where the device, authenticated as a public client by its identifier
* alone, obtains a device code and a user code. The verification endpoint ({@code /oauth2/device_verification}) is
* where the signed-in user approves the device; its verification URI is the framework's default, the issuer plus the
* endpoint, whose {@code GET} without a code is the page of {@link DeviceVerificationController}. The verification
* endpoint is customized to guard its requests ({@link DeviceVerificationRequestGuard}), to hand the consent decision
* to the existing consent page, to answer its failures with {@link DeviceVerificationErrorHandler} and to go back to
* the verification page when the device was approved.
*/
@RequiredArgsConstructor
public class DeviceProtocolChainCustomizer implements ProtocolChainCustomizer {
/** Order of the customizer: after the multi-factor ({@code 0}) and step-up ({@code 1}) ones. */
public static final int ORDER = 2;
/** Reads the identifier of a public client of the device flow. */
private final AuthenticationConverter clientConverter;
/** Authenticates a public client of the device flow. */
private final AuthenticationProvider clientProvider;
/** Rate limit and multi-factor policy of the verification requests. */
private final AuthenticationConverter guard;
/** Answers the failures of the verification endpoint. */
private final AuthenticationFailureHandler errorHandler;
/** Answers a verification that did not need consent, or whose consent was given. */
private final AuthenticationSuccessHandler successHandler;
/**
* Customizes the device endpoints.
*
* @param server configurer of the authorization server.
*/
@Override
public void customize(final OAuth2AuthorizationServerConfigurer server) {
server.clientAuthentication(client -> client
.authenticationConverter(clientConverter)
.authenticationProvider(clientProvider));
server.deviceAuthorizationEndpoint(Customizer.withDefaults());
server.deviceVerificationEndpoint(endpoint -> endpoint
.consentPage(ConsentController.CONSENT_PATH)
.deviceVerificationRequestConverters(converters -> converters.addFirst(guard))
.errorResponseHandler(errorHandler)
.deviceVerificationResponseHandler(successHandler));
}
}
/**
* Declares the customizer that enables the device endpoints. It is applied after the multi-factor and step-up
* ones.
*
* @param registeredClients finds the clients of the devices.
* @param authorizationService finds the device authorization of a user code.
* @param serverSettings settings of the protocol endpoints, for the device authorization one.
* @param rateLimiter limits the attempts.
* @param authorizationManager applies the multi-factor policy.
* @param secondFactorEntryPoint entry point of the second-factor page.
* @param reauthenticationEntryPoint entry point that forces a new sign-in.
* @return the customizer.
*/
@Bean
@Order(DeviceProtocolChainCustomizer.ORDER)
public ProtocolChainCustomizer deviceProtocolChainCustomizer(final RegisteredClientRepository registeredClients,
final OAuth2AuthorizationService authorizationService, final AuthorizationServerSettings serverSettings,
final DeviceVerificationRateLimiter rateLimiter, final MfaAuthorizationManager authorizationManager,
final SecondFactorRequiredEntryPoint secondFactorEntryPoint,
final ReauthenticationEntryPoint reauthenticationEntryPoint) {
final var errorHandler = new DeviceVerificationErrorHandler(
new SecondFactorErrorResponseHandler(secondFactorEntryPoint, reauthenticationEntryPoint),
DeviceVerificationController.VERIFICATION_PATH);
final var approvedUrl = DeviceVerificationController.VERIFICATION_PATH + "?"
+ DeviceVerificationController.SUCCESS_PARAMETER;
final var successHandler = new SimpleUrlAuthenticationSuccessHandler(approvedUrl);
return new DeviceProtocolChainCustomizer(
new DeviceClientAuthenticationConverter(serverSettings.getDeviceAuthorizationEndpoint()),
new DeviceClientAuthenticationProvider(registeredClients),
new DeviceVerificationRequestGuard(rateLimiter, authorizationManager, authorizationService,
registeredClients), errorHandler, successHandler);
}
Verification page
The user opens the verification URI, signs in (with the second factor the policy requires), types the user code and
approves on the consent page, which hands the decision to POST /oauth2/device_verification.
/oauth2/device_verification:
get:
tags: [ Device verification page ]
operationId: deviceVerificationPage
summary: Renders the device verification page
description: >-
Renders the page the verification URI of the device authorization response points to: the form where the user
types the code the device displays (RFC 8628). The form posts the code to the authorization server's own device
verification endpoint (POST /oauth2/device_verification, owned by the framework, with the CSRF token), never to
this operation. A request that already carries the code (verification_uri_complete) is answered by that
endpoint directly, which also applies the multi-factor policy of the account and the limit on the attempts, and
sends the browser on to the consent page (GET /oauth2/consent) or back here with a confirmation or an error. A
request without a signed-in session is redirected to the sign-in page and resumed afterwards.
parameters:
- name: user_code
in: query
required: false
description: A user code to prefill in the form. Any value that is not a plausible code is ignored.
schema:
type: string
maxLength: 32
- name: error
in: query
required: false
description: >-
Present (its value is ignored, usually empty) when the previous attempt failed; the page then shows a
message that never says why (an unknown, expired or already used code look the same).
schema:
type: string
- name: success
in: query
required: false
description: >-
Present (its value is ignored, usually empty) when the user has just approved the device; the page then
says that the device may continue.
schema:
type: string
responses:
'200':
description: The device verification page.
content:
text/html:
schema:
type: string
<!--
Device verification page (DeviceVerificationController.deviceVerificationPage): the form where the user types the code
the device shows (RFC 8628). It posts the code to the framework's endpoint, which asks for consent on the consent page
and sends the browser back here with ?error when the code is refused and ?success when the device was approved
(th:action adds the CSRF field). A failed attempt is announced in an assertive live region and never says why.
-->
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout(~{::title}, ~{::main})}">
<head>
<title th:text="#{device.title}">Connect a device</title>
</head>
<body>
<main>
<h1 th:text="#{device.heading}">Connect a device</h1>
<p th:if="${approved}" class="success" role="status" th:text="#{device.success}">The device may continue now.</p>
<div th:unless="${approved}">
<p th:text="#{device.intro}">Type the code your device shows to approve it.</p>
<div aria-live="assertive" role="alert">
<p th:if="${failed}" class="error" th:text="#{device.error}">That code is not valid.</p>
</div>
<form method="post" th:action="@{/oauth2/device_verification}">
<p>
<label for="user-code" th:text="#{device.code}">Code</label>
<input type="text" id="user-code" name="user_code" th:value="${userCode}" required autofocus maxlength="32"
autocomplete="off" autocapitalize="characters" spellcheck="false">
</p>
<button type="submit" id="verify" th:text="#{device.submit}">Continue</button>
</form>
</div>
</main>
</body>
</html>
/**
* Renders the device verification page.
*
* @param userCode a code to prefill, ignored unless it looks like a user code.
* @param error present when the previous attempt failed.
* @param success present when the device was just approved.
* @return the page.
*/
@Override
public ResponseEntity<String> deviceVerificationPage(final String userCode, final String error,
final String success) {
final var model = new HashMap<String, Object>();
model.put(ERROR_ATTRIBUTE, error != null);
model.put(SUCCESS_ATTRIBUTE, success != null);
if (userCode != null && PLAUSIBLE_CODE.matcher(userCode).matches()) {
model.put(USER_CODE_ATTRIBUTE, userCode);
}
return pageRenderer.render(VIEW_NAME, model);
}
A user code is short, so attempts are limited per client address and per user, and the MFA policy is checked before the
framework converters run. The policy includes the client’s mfaRequired setting: the post of the user code names no
client (and a client without consent approves the device on that post), so the guard finds the client of the device
authorization stored for the code, normalised as the framework stores it; the client_id of the consent decision is
only the fallback when no authorization holds the code.
device:
# Device authorization grant (RFC 8628): the attempts to verify a user code, which is short, are limited per client
# address and per signed-in user, counted in a fixed window.
rate-limit:
enabled: true
window: PT15M
per-client: 30
per-user: 10
/**
* Rate limiter of the attempts to verify a user code, on the pattern of the other limiters of this server: Bucket4j
* token buckets in Caffeine caches ({@link FixedWindowBuckets}), one per client address and one per signed-in user, so
* a user code, which is short, cannot be guessed faster than the limits allow. The buckets live in this instance
* only: several instances each apply the limits on their own. The client address is
* {@code request.getRemoteAddr()}, so behind a proxy {@code server.forward-headers-strategy} must be set for it to be
* the real client.
*/
public final class DeviceVerificationRateLimiter {
/** Key used when the client address is unknown. */
private static final String UNKNOWN_CLIENT = "unknown";
/** Limits, window and bound of the tracked keys. */
private final DeviceRateLimitProperties properties;
/** Buckets per client address. */
private final FixedWindowBuckets clients;
/** Buckets per signed-in user. */
private final FixedWindowBuckets users;
/**
* Creates the limiter.
*
* @param properties limits, window and bound of the tracked keys.
* @param clock source of the current time.
*/
public DeviceVerificationRateLimiter(final DeviceRateLimitProperties properties, final Clock clock) {
this.properties = properties;
clients = new FixedWindowBuckets(clock, properties.window(), properties.maxTrackedKeys());
users = new FixedWindowBuckets(clock, properties.window(), properties.maxTrackedKeys());
}
/**
* Counts one attempt and rejects it once a limit is exceeded.
*
* @param clientAddress address of the calling client, may be {@code null}.
* @param userName name of the signed-in user.
* @throws DeviceVerificationRateLimitedException if the client address or the user exceeded its limit.
*/
public void check(final String clientAddress, final String userName) {
if (!properties.enabled()) {
return;
}
final var clientWait = clients.tryConsume(clientAddress == null ? UNKNOWN_CLIENT : clientAddress,
properties.perClient());
if (clientWait.isPresent()) {
throw new DeviceVerificationRateLimitedException(clientWait.get());
}
final var userWait = users.tryConsume(userName, properties.perUser());
if (userWait.isPresent()) {
throw new DeviceVerificationRateLimitedException(userWait.get());
}
}
}
/**
* Guards the device verification endpoint. It is installed first among the converters of the endpoint and never
* converts anything: it either lets the request go on (returning {@code null}) or refuses it. Every request that
* carries a user code (the code typed on the verification page, a link that carries it, and the consent decision) is
* counted against the limit on the attempts, and the multi-factor policy of the account and of the client must be
* satisfied, as the authorization endpoint requires before it issues a code: a user who owes a second factor is sent
* to the second-factor page and resumes afterwards. The framework's own MFA customizer covers only
* {@code /oauth2/authorize}.
*
* <p>The client is the one the device authorization stored for the user code was issued to, because the code typed on
* the page (or carried by the link) is the only thing the request names: so a client that requires multi-factor
* authentication is enforced even when it skips the consent page. Only when no authorization holds the code (an
* unknown or expired one, which the endpoint refuses anyway) is the {@code client_id} of the consent decision used.
*/
@RequiredArgsConstructor
public class DeviceVerificationRequestGuard implements AuthenticationConverter {
/** Type of the token the user code is stored as. */
static final OAuth2TokenType USER_CODE_TOKEN_TYPE = new OAuth2TokenType(OAuth2ParameterNames.USER_CODE);
/** Number of letters and digits of a user code. */
private static final int USER_CODE_LENGTH = 8;
/** Position of the dash the framework stores a user code with. */
private static final int USER_CODE_GROUP = USER_CODE_LENGTH / 2;
/** Everything a user may type around the letters and digits of a code (dashes, spaces). */
private static final Pattern NOT_ALPHANUMERIC = Pattern.compile("[^A-Z\\d]+");
/** Limits the attempts. */
private final DeviceVerificationRateLimiter rateLimiter;
/** Applies the multi-factor policy. */
private final MfaAuthorizationManager authorizationManager;
/** Finds the device authorization of a user code. */
private final OAuth2AuthorizationService authorizationService;
/** Finds the client of a device authorization. */
private final RegisteredClientRepository registeredClientRepository;
/**
* Checks the request.
*
* @param request the request.
* @return always {@code null}: this converter only vetoes requests.
* @throws DeviceVerificationRateLimitedException if the client address or the user made too many attempts.
* @throws SecondFactorRequiredException if the user still owes a second factor.
*/
@Override
public Authentication convert(final HttpServletRequest request) {
final var user = SecurityContextHolder.getContext().getAuthentication();
if (!MfaAuthorizationManager.isUser(user)) {
return null;
}
rateLimiter.check(request.getRemoteAddr(), user.getName());
final var clientRequires = authorizationManager.clientRequiresMfa(clientIdOf(request));
if (!authorizationManager.evaluate(user, clientRequires).isGranted()) {
throw new SecondFactorRequiredException();
}
return null;
}
/**
* Finds the client a request is about: the client of the device authorization stored for its user code, or the
* {@code client_id} parameter when no authorization holds that code.
*
* @param request the request.
* @return the client id, or {@code null} when neither tells it.
*/
private String clientIdOf(final HttpServletRequest request) {
return normalize(request.getParameter(OAuth2ParameterNames.USER_CODE))
.map(userCode -> authorizationService.findByToken(userCode, USER_CODE_TOKEN_TYPE))
.map(authorization -> registeredClientRepository.findById(authorization.getRegisteredClientId()))
.map(RegisteredClient::getClientId)
.orElseGet(() -> request.getParameter(OAuth2ParameterNames.CLIENT_ID));
}
/**
* Normalises a user code as the framework stores it: upper case letters and digits in two groups of four,
* separated by a dash.
*
* @param userCode the code as typed, may be {@code null}.
* @return the stored form, or empty when the value cannot be a user code.
*/
private static Optional<String> normalize(final String userCode) {
if (!StringUtils.hasText(userCode)) {
return Optional.empty();
}
final var compact = NOT_ALPHANUMERIC.matcher(userCode.toUpperCase(Locale.ROOT)).replaceAll("");
if (compact.length() != USER_CODE_LENGTH) {
return Optional.empty();
}
return Optional.of(compact.substring(0, USER_CODE_GROUP) + "-" + compact.substring(USER_CODE_GROUP));
}
}
Public clients
The device client is public (no secret). Spring’s PKCE-based authentication cannot serve the device flow, so a
dedicated converter and provider authenticate a client that is registered with the device grant and no secret by its
identifier alone. The request must not ask for openid: Spring Authorization Server refuses it in device requests, so
the seeded device-client (dev profile only) has no ID token.
/**
* Reads the identifier of a public client of the device flow, the only kind of client authentication that flow has
* for a device that cannot keep a secret: the {@code client_id} of the request to the device authorization endpoint
* and of the polling request to the token endpoint with the device code grant. Spring Authorization Server's public
* client authentication needs a PKCE code verifier, which the device flow does not have. A request that carries any
* other credential (an {@code Authorization} header, a client secret or a client assertion) is left to the
* converters of those credentials.
*/
public class DeviceClientAuthenticationConverter implements AuthenticationConverter {
/** Names of the parameters that carry a credential of a confidential client. */
private static final List<String> CREDENTIAL_PARAMETERS = List.of(
OAuth2ParameterNames.CLIENT_SECRET, OAuth2ParameterNames.CLIENT_ASSERTION,
OAuth2ParameterNames.CLIENT_ASSERTION_TYPE);
/** Matches the request for the codes, which names the client. */
private final RequestMatcher deviceAuthorizationRequest;
/**
* Creates the converter.
*
* @param deviceAuthorizationEndpoint path of the device authorization endpoint.
*/
public DeviceClientAuthenticationConverter(final String deviceAuthorizationEndpoint) {
deviceAuthorizationRequest = PathPatternRequestMatcher.withDefaults()
.matcher(HttpMethod.POST, deviceAuthorizationEndpoint);
}
/**
* Reads the client of a request.
*
* @param request the request.
* @return the request to authenticate the public client, or {@code null} when the request is not one of the device
* flow's or carries a credential.
* @throws OAuth2AuthenticationException {@code invalid_request} if the client is named more than once or not at
* all.
*/
@Override
public Authentication convert(final HttpServletRequest request) {
if (!isDeviceRequest(request) || carriesCredential(request)) {
return null;
}
final var clientIds = request.getParameterValues(OAuth2ParameterNames.CLIENT_ID);
if (clientIds == null || clientIds.length != 1 || !StringUtils.hasText(clientIds[0])) {
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
}
return new DeviceClientAuthenticationToken(clientIds[0]);
}
/**
* Tells whether the request is one of the device flow's: the request for the codes, or the poll with the device
* code grant.
*
* @param request the request.
* @return {@code true} for a device flow request that names a client.
*/
private boolean isDeviceRequest(final HttpServletRequest request) {
if (request.getParameter(OAuth2ParameterNames.CLIENT_ID) == null) {
return false;
}
return deviceAuthorizationRequest.matches(request) || isDeviceCodePoll(request);
}
/**
* Tells whether the request is a poll of the token endpoint with the device code grant.
*
* @param request the request.
* @return {@code true} when it is.
*/
private static boolean isDeviceCodePoll(final HttpServletRequest request) {
return AuthorizationGrantType.DEVICE_CODE.getValue().equals(request.getParameter(
OAuth2ParameterNames.GRANT_TYPE)) && request.getParameter(OAuth2ParameterNames.DEVICE_CODE) != null;
}
/**
* Tells whether the request carries the credential of a confidential client.
*
* @param request the request.
* @return {@code true} when it does.
*/
private static boolean carriesCredential(final HttpServletRequest request) {
return request.getHeader(HttpHeaders.AUTHORIZATION) != null
|| CREDENTIAL_PARAMETERS.stream().anyMatch(name -> request.getParameter(name) != null);
}
}
No migration is needed: the schema of the authorization service already has the device_code_* and user_code_* columns.
Polling CLI
service-client has a command-line client, active only under the device-flow profile. It prints the user code and
the verification URI, then polls /oauth2/token, honouring interval, slow_down, authorization_pending and
expired_token.
java -jar service-client/boot/target/service-client-boot-1.0.0-SNAPSHOT.jar --spring.profiles.active=device-flow --device-flow.issuer=http://localhost:9000