From bfea1bec1c82b6167a55e2eb61922c29a2119507 Mon Sep 17 00:00:00 2001 From: mrfelek Date: Wed, 11 Nov 2026 00:10:55 +0100 Subject: [PATCH] fix(ssooidc): name the modeled exception in x-amzn-ErrorType (#7919) SSO OIDC errors carried the OAuth error code in the body but no x-amzn-ErrorType header. AWS sends both, and the SDKs resolve the modeled exception from the header, so a pending device authorization surfaced as an unmodeled 400 instead of AuthorizationPendingException: `aws sso login` stopped polling and failed with "An error occurred (400) when calling the CreateToken operation". Every OIDC error now names its exception, derived from the OAuth code (authorization_pending -> AuthorizationPendingException, slow_down -> SlowDownException, invalid_grant -> InvalidGrantException, ...). Address review on #7919: server_error is the one SSO OIDC error whose modeled exception is not named after its OAuth code, so it mapped to ServerErrorException, which the SDKs cannot resolve. It now maps to InternalServerException. A unit test pins the header name for every OAuth code Floci returns. --- .../services/ssooidc/SsoOidcController.java | 21 +++- .../ssooidc/SsoOidcErrorTypeTest.java | 33 ++++++ .../ssooidc/SsoOidcIntegrationTest.java | 103 ++++++++++-------- 3 files changed, 113 insertions(+), 44 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/ssooidc/SsoOidcErrorTypeTest.java diff --git a/src/main/java/io/floci/aws/services/ssooidc/SsoOidcController.java b/src/main/java/io/floci/aws/services/ssooidc/SsoOidcController.java index 82150ef85d..c4846bfbd8 100644 --- a/src/main/java/io/floci/aws/services/ssooidc/SsoOidcController.java +++ b/src/main/java/io/floci/aws/services/ssooidc/SsoOidcController.java @@ -409,6 +409,25 @@ public class SsoOidcController { ObjectNode response = objectMapper.createObjectNode(); response.put("error", error); response.put("error_description", description); - return Response.status(status).type(MediaType.APPLICATION_JSON).entity(response).build(); + // AWS SDKs read the modeled exception from this header, not from the OAuth "error" body + // member: without it a pending device authorization is not retried and `aws sso login` fails. + return Response.status(status).type(MediaType.APPLICATION_JSON) + .header("x-amzn-ErrorType", exceptionName(error)) + .entity(response).build(); + } + + /** OAuth error code to the SSO OIDC exception name, e.g. authorization_pending to AuthorizationPendingException. */ + static String exceptionName(String error) { + // The one modeled exception not named after its OAuth code. + if ("server_error".equals(error)) { + return "InternalServerException"; + } + StringBuilder name = new StringBuilder(); + for (String part : error.split("_")) { + if (!part.isEmpty()) { + name.append(Character.toUpperCase(part.charAt(0))).append(part.substring(1)); + } + } + return name.append("Exception").toString(); } } diff --git a/src/test/java/io/floci/aws/services/ssooidc/SsoOidcErrorTypeTest.java b/src/test/java/io/floci/aws/services/ssooidc/SsoOidcErrorTypeTest.java new file mode 100644 index 0000000000..504888b2ed --- /dev/null +++ b/src/test/java/io/floci/aws/services/ssooidc/SsoOidcErrorTypeTest.java @@ -0,0 +1,33 @@ +package io.floci.aws.services.ssooidc; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Every OAuth error code Floci answers with maps to the exception the SSO OIDC API models, which + * the SDKs resolve from the x-amzn-ErrorType header. + */ +class SsoOidcErrorTypeTest { + + @ParameterizedTest + @CsvSource({ + "access_denied, AccessDeniedException", + "authorization_pending, AuthorizationPendingException", + "expired_token, ExpiredTokenException", + "invalid_client, InvalidClientException", + "invalid_client_metadata, InvalidClientMetadataException", + "invalid_grant, InvalidGrantException", + "invalid_redirect_uri, InvalidRedirectUriException", + "invalid_request, InvalidRequestException", + "invalid_scope, InvalidScopeException", + "server_error, InternalServerException", + "slow_down, SlowDownException", + "unauthorized_client, UnauthorizedClientException", + "unsupported_grant_type, UnsupportedGrantTypeException", + }) + void namesTheModeledException(String oauthError, String exception) { + assertEquals(exception, SsoOidcController.exceptionName(oauthError)); + } +} diff --git a/src/test/java/io/floci/aws/services/ssooidc/SsoOidcIntegrationTest.java b/src/test/java/io/floci/aws/services/ssooidc/SsoOidcIntegrationTest.java index eee56aeebd..20f7177942 100644 --- a/src/test/java/io/floci/aws/services/ssooidc/SsoOidcIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ssooidc/SsoOidcIntegrationTest.java @@ -23,6 +23,7 @@ import static org.hamcrest.Matchers.matchesPattern; class SsoOidcIntegrationTest { private static final String AUTH_HEADER = "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/sso-oauth/aws4_request"; + private static final String DEVICE_CODE_GRANT = "urn:ietf:params:oauth:grant-type:device_code"; @Inject SsoAdminService ssoAdminService; @@ -56,7 +57,7 @@ class SsoOidcIntegrationTest { Response registration = given() .contentType("application/json") .body("{\"clientName\":\"Device Integration\",\"clientType\":\"public\"," - + "\"grantTypes\":[\"urn:ietf:params:oauth:grant-type:device_code\"]}") + + "\"grantTypes\":[\"" + DEVICE_CODE_GRANT + "\"]}") .when().post("/client/register") .then().statusCode(200) .extract().response(); @@ -86,24 +87,10 @@ class SsoOidcIntegrationTest { @Test void deviceAuthorizationRejectsCallerSelectedPrincipal() { - Response registration = given() - .contentType("application/json") - .body("{\"clientName\":\"Principal Guard\",\"clientType\":\"public\"," - + "\"grantTypes\":[\"urn:ietf:params:oauth:grant-type:device_code\"]}") - .when().post("/client/register") - .then().statusCode(200) - .extract().response(); - Response authorization = given() - .contentType("application/json") - .body("{\"clientId\":\"" + registration.path("clientId") + "\",\"clientSecret\":\"" - + registration.path("clientSecret") + "\",\"startUrl\":\"https://example.awsapps.com/start\"}") - .when().post("/device_authorization") - .then().statusCode(200) - .extract().response(); + DeviceAuthorization authorization = startDeviceAuthorization("Principal Guard", DEVICE_CODE_GRANT); - String userCode = authorization.path("userCode"); given() - .queryParam("user_code", userCode) + .queryParam("user_code", authorization.userCode()) .queryParam("principal_id", "11111111-2222-3333-4444-555555555555") .when().get("/device") .then().statusCode(403) @@ -112,35 +99,16 @@ class SsoOidcIntegrationTest { @Test void createTokenCompletesDeviceFlowAndRefreshesToken() { - Response registration = given() - .contentType("application/json") - .body("{\"clientName\":\"Token Integration\",\"clientType\":\"public\"," - + "\"grantTypes\":[\"urn:ietf:params:oauth:grant-type:device_code\",\"refresh_token\"]}") - .when().post("/client/register") - .then().statusCode(200) - .extract().response(); - String clientId = registration.path("clientId"); - String clientSecret = registration.path("clientSecret"); - Response authorization = given() - .contentType("application/json") - .body("{\"clientId\":\"" + clientId + "\",\"clientSecret\":\"" + clientSecret - + "\",\"startUrl\":\"https://example.awsapps.com/start\"}") - .when().post("/device_authorization") - .then().statusCode(200) - .extract().response(); - String deviceCode = authorization.path("deviceCode"); - String userCode = authorization.path("userCode"); + DeviceAuthorization authorization = + startDeviceAuthorization("Token Integration", DEVICE_CODE_GRANT, "refresh_token"); + String clientId = authorization.clientId(); + String clientSecret = authorization.clientSecret(); - given().queryParam("user_code", userCode) + given().queryParam("user_code", authorization.userCode()) .when().get("/device") .then().statusCode(200).body("status", equalTo("authorized")); - Response token = given() - .contentType("application/json") - .body("{\"clientId\":\"" + clientId + "\",\"clientSecret\":\"" + clientSecret - + "\",\"grantType\":\"urn:ietf:params:oauth:grant-type:device_code\"," - + "\"deviceCode\":\"" + deviceCode + "\"}") - .when().post("/token") + Response token = createDeviceCodeToken(authorization) .then().statusCode(200) .body("tokenType", equalTo("Bearer")) .body("accessToken", matchesPattern("[0-9a-f]{64}")) @@ -213,6 +181,55 @@ class SsoOidcIntegrationTest { .body("{\"clientName\":\"Bad Client\",\"clientType\":\"confidential\"}") .when().post("/client/register") .then().statusCode(400) - .body("error", equalTo("invalid_client_metadata")); + .body("error", equalTo("invalid_client_metadata")) + .header("x-amzn-ErrorType", "InvalidClientMetadataException"); + } + + @Test + void pendingDeviceAuthorizationNamesTheModeledExceptionForSdkRetries() { + // AWS SDKs map OIDC errors from x-amzn-ErrorType; `aws sso login` only keeps polling on + // AuthorizationPendingException. + DeviceAuthorization authorization = startDeviceAuthorization("Pending Integration", DEVICE_CODE_GRANT); + + createDeviceCodeToken(authorization) + .then().statusCode(400) + .header("x-amzn-ErrorType", "AuthorizationPendingException") + .body("error", equalTo("authorization_pending")); + } + + private record DeviceAuthorization(String clientId, String clientSecret, String deviceCode, String userCode) { + } + + /** Sends the device-code CreateToken request a polling client sends for {@code authorization}. */ + private static Response createDeviceCodeToken(DeviceAuthorization authorization) { + return given() + .contentType("application/json") + .body("{\"clientId\":\"" + authorization.clientId() + "\",\"clientSecret\":\"" + + authorization.clientSecret() + "\",\"grantType\":\"" + DEVICE_CODE_GRANT + + "\",\"deviceCode\":\"" + authorization.deviceCode() + "\"}") + .when().post("/token"); + } + + /** Registers a public client with the given grant types and starts a device authorization for it. */ + private static DeviceAuthorization startDeviceAuthorization(String clientName, String... grantTypes) { + String grants = String.join("\",\"", grantTypes); + Response registration = given() + .contentType("application/json") + .body("{\"clientName\":\"" + clientName + "\",\"clientType\":\"public\"," + + "\"grantTypes\":[\"" + grants + "\"]}") + .when().post("/client/register") + .then().statusCode(200) + .extract().response(); + String clientId = registration.path("clientId"); + String clientSecret = registration.path("clientSecret"); + Response authorization = given() + .contentType("application/json") + .body("{\"clientId\":\"" + clientId + "\",\"clientSecret\":\"" + clientSecret + + "\",\"startUrl\":\"https://example.awsapps.com/start\"}") + .when().post("/device_authorization") + .then().statusCode(200) + .extract().response(); + return new DeviceAuthorization(clientId, clientSecret, + authorization.path("deviceCode"), authorization.path("userCode")); } }