From e16d62e93892eb734d2f242d9625a36cf6b0213d Mon Sep 17 00:00:00 2001
From: Dinesh Gupta
Date: Sun, 15 Jun 2025 02:43:49 +0100
Subject: [PATCH] Add device verification authentication context support
Previously, device consent handling did not provide a dedicated context
for device verification authentication flows. This commit introduces
OAuth2DeviceVerificationAuthenticationContext and updates related
providers and tests to enhance device authorization and consent flows.
Fixes gh-1965
Signed-off-by: Dinesh Gupta
Add Predicate for authorizationConsentRequired for device code grant
Introduce a customizable Predicate to determine whether user authorization consent is required
in the Device Code grant flow. This enhancement allows applications to define custom logic for
skipping or displaying the consent page, enabling greater flexibility to handle cases where
user code confirmation and scope approval may be decoupled.
The default behavior is preserved, but can be overridden by calling
OAuth2DeviceVerificationAuthenticationProvider#setAuthorizationConsentRequired(Predicate).
Closes: gh-1965
Signed-off-by: Dinesh Gupta
Add Predicate for authorizationConsentRequired for device code grant
This commit introduces a Predicate extension point for determining
if user consent is required during the OAuth 2.0 Device Authorization
Grant (device code flow).
- Adds OAuth2DeviceVerificationAuthenticationContext to provide context to the Predicate
- Updates OAuth2DeviceVerificationAuthenticationProvider to support a custom Predicate via setAuthorizationConsentRequired
- Refactors default consent logic to use the Predicate
- Updates and adds tests for custom Predicate behavior
Closes gh-1965
Signed-off-by: Dinesh Gupta
Refactor DeviceVerification context to align with code grant context
Refactored OAuth2DeviceVerificationAuthenticationContext to use a map-based
structure consistent with OAuth2AuthorizationCodeRequestAuthenticationContext.
Aligned method signatures, builder pattern, and attribute handling for
consistency and extensibility. Updated OAuth2DeviceVerificationAuthenticationProvider
to use the revised context and normalize requested scopes.
Closes gh-1965-device-consent
Authored-by: Dinesh Gupta
Align device verification consent logic with code grant context
Refactored OAuth2DeviceVerificationAuthenticationProvider and its tests to ensure
the device verification consent logic and structure are consistent with the authorization code flow.
Improved test consistency, predicate usage, and aligned context handling for maintainability.
Closes gh-1965-device-consent
Authored-by: Dinesh Gupta
Clarify Javadoc for device consent predicate
Closes gh-1965-device-consent
Authored-by: Dinesh Gupta
Signed-off-by: Dinesh Gupta
Fix test cases for device code consent predicate
Cleaned up and improved consistency of test cases related to the device code authorizationConsentRequired predicate.
Signed-off-by: Dinesh Gupta
---
...viceVerificationAuthenticationContext.java | 185 ++++++++++++++++++
...iceVerificationAuthenticationProvider.java | 45 ++++-
...rificationAuthenticationProviderTests.java | 85 ++++++++
3 files changed, 310 insertions(+), 5 deletions(-)
create mode 100644 oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2DeviceVerificationAuthenticationContext.java
diff --git a/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2DeviceVerificationAuthenticationContext.java b/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2DeviceVerificationAuthenticationContext.java
new file mode 100644
index 000000000..d6ac24dd9
--- /dev/null
+++ b/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2DeviceVerificationAuthenticationContext.java
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2025 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.security.oauth2.server.authorization.authentication;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.springframework.lang.Nullable;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
+import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent;
+import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
+import org.springframework.util.Assert;
+
+/**
+ * An {@link OAuth2AuthenticationContext} that holds an
+ * {@link OAuth2DeviceVerificationAuthenticationToken} and additional information and is
+ * used when validating the OAuth 2.0 Device Verification Request parameters, as well as
+ * determining if authorization consent is required.
+ *
+ * @author Dinesh Gupta
+ * @since 2.0.0
+ * @see OAuth2AuthenticationContext
+ * @see OAuth2DeviceVerificationAuthenticationToken
+ * @see OAuth2DeviceVerificationAuthenticationProvider#setAuthorizationConsentRequired(java.util.function.Predicate)
+ */
+public final class OAuth2DeviceVerificationAuthenticationContext implements OAuth2AuthenticationContext {
+
+ private final Map