From 1e4e8b9ee80d1a7670d993f62bf18eb8ecd7e26a Mon Sep 17 00:00:00 2001
From: Brian Taylor <82356519+bsaurusrex@users.noreply.github.com>
Date: Wed, 2 Sep 2026 23:50:47 +0800
Subject: [PATCH] feat(cognito): implement managed login branding (#2869)
* feat(cognito): implement managed login branding
CreateManagedLoginBranding, DescribeManagedLoginBranding,
DescribeManagedLoginBrandingByClient, UpdateManagedLoginBranding and
DeleteManagedLoginBranding were unsupported, so a config declaring
`aws_cognito_managed_login_branding` could not be applied against Floci.
A branding belongs to one app client and is stored on it. Behaviour matches what
the live API does:
- a second branding for the same client raises ManagedLoginBrandingExistsException
- ManagedLoginBrandingId must be a version 4 UUID, checked before the lookup
- Settings is omitted from the response when the caller supplied none, while
Assets is always returned
- members an update omits are left unchanged
Settings is stored opaquely rather than validated against AWS's unpublished
schema, and ReturnMergedResources does not merge Cognito's default corpus.
Closes #2856
* fix(cognito): reject malformed CreateManagedLoginBranding members
Create coerced an absent UseCognitoProvidedValues to false and silently ignored a
wrongly typed Settings or Assets, so a malformed request produced persisted state.
Measured against AWS:
neither UseCognitoProvidedValues nor Settings -> InvalidParameterException:
useCognitoProvidedValues or settings should be specified (but not both)
Assets of the wrong type -> SerializationException: Unexpected field type
UseCognitoProvidedValues of the wrong type -> accepted, not rejected
Create now enforces the first rule and type-checks Settings and Assets. A
non-boolean UseCognitoProvidedValues is still accepted, because AWS accepts it.
AWS answers a wrongly typed Settings with a 500 InternalErrorException; Floci
returns the same client error it returns for Assets rather than reproducing that,
and the divergence is documented.
* fix(cognito): check Assets elements before converting them
The Assets reader validated only the outer array, so a scalar element reached
Jackson's convertValue and escaped as a 500 InternalFailure. This is the same
class of bug as the LogConfigurations one, which I fixed in that reader without
applying it here.
AWS separates the two failures, and they differ from the LogConfigurations rules:
[1, 2] / ["str"] -> SerializationException: Unexpected value type in payload
[null] -> InvalidParameterException: 1 validation error detected:
Value '[null]' at 'assets' failed to satisfy constraint:
Member must satisfy constraint: [Member must not be null]
A null element is rejected here, where LogConfigurations drops it.
* fix(cognito): match AWS branding source validation
A managed login branding request must select exactly one source of branding,
and the same rule governs create and update. Measured against Cognito in
ap-southeast-1:
useCognitoProvidedValues settings result
absent absent InvalidParameterException
true absent accepted
false absent InvalidParameterException
absent present accepted
true present InvalidParameterException
false present accepted
Create rejected only the neither case, update validated nothing at all, so
naming both members and updating with no source both succeeded where the
service rejects them. Presence of the member is not the test: false selects no
source, which is why it needs settings alongside it.
Reading the flag had to move with it. AWS coerces a JSON string rather than
rejecting it, storing true for "true" and for "yes" and false for "false",
while Jackson maps every string except "true" to false. That turned "yes" into
a request selecting no source, so the coercion now follows the service.
* fix(cognito): reject malformed UseCognitoProvidedValues types
Jackson's asBoolean() maps a number, a JSON null, an object and an array all to
false, so a malformed flag alongside valid Settings passed source validation and
persisted as branding state that was never requested.
Measured against Cognito in ap-southeast-1: 1, null, {} and [] are each a
SerializationException, with or without Settings, while omitting the member is
accepted and stores false. Only booleans and strings are read now, strings still
coerced the way the service coerces them, and everything else raises
SerializationException the way SesController already does.
* fix(cognito): bound managed login branding assets at 40
Neither create nor update checked the documented 40-item maximum on Assets, so
an oversized request was accepted and stored where AWS rejects it.
Measured against Cognito in ap-southeast-1:
create 40 assets passes the bound, reaches the source rule
create 41 assets InvalidParameterException, length constraint
create 41 + unknown client the length error, not the missing client
update 41 + malformed id 2 validation errors, the length one first
update malformed id alone 1 validation error, before the pool lookup
The asset and branding-id checks now run before anything is looked up and
share one message, matching those last three rows. DescribeManagedLoginBranding
ran its pool lookup ahead of the branding-id check, which the fourth row shows
is the wrong way round, so it moved with them.
The length message embeds the request model's toString. The blob renders as the
decoded buffer, confirmed at three payload sizes; the decoder used here is the
lenient one so that building a rejection cannot itself fail.
---
docs/services/cognito.md | 40 ++
.../services/cognito/CognitoJsonHandler.java | 140 ++++
.../services/cognito/CognitoService.java | 184 +++++
.../cognito/model/ManagedLoginBranding.java | 57 ++
.../cognito/model/UserPoolClient.java | 8 +
...toManagedLoginBrandingIntegrationTest.java | 661 ++++++++++++++++++
6 files changed, 1090 insertions(+)
create mode 100644 src/main/java/io/github/hectorvent/floci/services/cognito/model/ManagedLoginBranding.java
create mode 100644 src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginBrandingIntegrationTest.java
diff --git a/docs/services/cognito.md b/docs/services/cognito.md
index 54cc64c608..772e1afd51 100644
--- a/docs/services/cognito.md
+++ b/docs/services/cognito.md
@@ -145,6 +145,46 @@ further divergences, both deliberate:
| AdminRemoveUserFromGroup | Removes a user from a group. |
| AdminListGroupsForUser | Lists the groups assigned to a user. |
+### Managed Login Branding
+
+| Action | Description |
+|--------|-------------|
+| CreateManagedLoginBranding | Creates the branding for an app client. |
+| DescribeManagedLoginBranding | Returns a branding by its id. |
+| DescribeManagedLoginBrandingByClient | Returns the branding attached to an app client. |
+| UpdateManagedLoginBranding | Updates a branding's settings, assets or provided-values flag. |
+| DeleteManagedLoginBranding | Deletes a branding from its app client. |
+
+`CreateManagedLoginBranding` must name either `UseCognitoProvidedValues` or `Settings`; a
+request with neither is rejected. One branding per app client: a second
+`CreateManagedLoginBranding` for the same client is rejected with
+`ManagedLoginBrandingExistsException`. `ManagedLoginBrandingId` must be a
+version 4 UUID, and a malformed one is rejected before the lookup, as AWS does.
+`Assets` holds at most 40 entries on create and on update.
+`Settings` is omitted from the response when the caller supplied none, while `Assets` is
+always returned. Members an update omits are left unchanged.
+
+The asset-count and branding-id checks run before the pool, client or branding is looked
+up, so an oversized request naming something that does not exist reports the request
+problem rather than `ResourceNotFoundException`, and an update violating both reports them
+in one message with the asset list first.
+
+Branding is presentation for the hosted UI, which Floci does not serve, so it is stored
+and returned rather than rendered. Two divergences follow from that:
+
+- **`Settings` is stored opaquely.** AWS validates it against a deep schema, rejecting
+ unknown properties with `Invalid settings provided. Validation errors: [{property:
+ $.components...., errorType: UnknownProperty}]`. That schema is not published, so Floci
+ accepts any object.
+- **A wrongly typed `Settings` returns a client error.** AWS answers that particular input
+ with `InternalErrorException` and a 500; Floci returns
+ `SerializationException: Unexpected field type`, which is what AWS returns for a wrongly
+ typed `Assets`. Reproducing someone else's 500 seemed worse than being consistent.
+- **`ReturnMergedResources` is not honoured.** Against AWS it merges Cognito's own default
+ settings and assets into the response: on a pool with 8 configured assets it returns 38.
+ Reproducing that needs Cognito's default corpus, so Floci returns the stored branding
+ either way.
+
## Well-Known And OAuth Endpoints
| Endpoint | Description |
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
index b7431f7791..bdca6f26bb 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
@@ -15,12 +15,14 @@ import io.github.hectorvent.floci.services.cognito.model.UserPool;
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
import io.github.hectorvent.floci.services.cognito.model.UserPoolClientSecret;
import io.github.hectorvent.floci.services.cognito.model.UserPoolDomain;
+import io.github.hectorvent.floci.services.cognito.model.ManagedLoginBranding;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -75,6 +77,11 @@ public class CognitoJsonHandler {
case "AdminEnableUser" -> handleAdminEnableUser(request);
case "AdminDisableUser" -> handleAdminDisableUser(request);
case "AdminLinkProviderForUser" -> handleAdminLinkProviderForUser(request);
+ case "CreateManagedLoginBranding" -> handleCreateManagedLoginBranding(request);
+ case "DescribeManagedLoginBranding" -> handleDescribeManagedLoginBranding(request);
+ case "DescribeManagedLoginBrandingByClient" -> handleDescribeManagedLoginBrandingByClient(request);
+ case "UpdateManagedLoginBranding" -> handleUpdateManagedLoginBranding(request);
+ case "DeleteManagedLoginBranding" -> handleDeleteManagedLoginBranding(request);
case "ListUsers" -> handleListUsers(request);
case "InitiateAuth" -> handleInitiateAuth(request);
case "AdminInitiateAuth" -> handleAdminInitiateAuth(request);
@@ -555,6 +562,139 @@ public class CognitoJsonHandler {
return Response.ok(objectMapper.createObjectNode()).build();
}
+ /**
+ * AWS coerces a JSON string in this member instead of rejecting it, and rejects every
+ * other non-boolean type. Measured against Cognito in ap-southeast-1:
+ *
+ *
+ * absent accepted, stored false
+ * true / false accepted as given
+ * "true", "yes" accepted, stored true
+ * "false" accepted, stored false
+ * 1, null, {}, [] SerializationException
+ *
+ *
+ * Jackson would map all of those last four to false through {@code asBoolean()},
+ * which is how a malformed flag used to be accepted alongside valid settings and
+ * persisted as unintended branding state.
+ */
+ private static Boolean readUseCognitoProvidedValues(JsonNode request) {
+ if (!request.has("UseCognitoProvidedValues")) {
+ return null;
+ }
+ JsonNode node = request.path("UseCognitoProvidedValues");
+ if (node.isBoolean()) {
+ return node.booleanValue();
+ }
+ if (node.isTextual()) {
+ return !"false".equalsIgnoreCase(node.asText());
+ }
+ throw new AwsException("SerializationException", null, 400);
+ }
+
+ private Response handleCreateManagedLoginBranding(JsonNode request) {
+ ManagedLoginBranding branding = service.createManagedLoginBranding(
+ request.path("UserPoolId").asText(),
+ request.path("ClientId").asText(),
+ readUseCognitoProvidedValues(request),
+ readObjectMap(request, "Settings"),
+ readMapList(request, "Assets")
+ );
+ return brandingResponse(branding);
+ }
+
+ private Response handleDescribeManagedLoginBranding(JsonNode request) {
+ return brandingResponse(service.describeManagedLoginBranding(
+ request.path("UserPoolId").asText(),
+ request.path("ManagedLoginBrandingId").asText(null)));
+ }
+
+ private Response handleDescribeManagedLoginBrandingByClient(JsonNode request) {
+ return brandingResponse(service.describeManagedLoginBrandingByClient(
+ request.path("UserPoolId").asText(),
+ request.path("ClientId").asText()));
+ }
+
+ private Response handleUpdateManagedLoginBranding(JsonNode request) {
+ return brandingResponse(service.updateManagedLoginBranding(
+ request.path("UserPoolId").asText(),
+ request.path("ManagedLoginBrandingId").asText(null),
+ readUseCognitoProvidedValues(request),
+ readObjectMap(request, "Settings"),
+ readMapList(request, "Assets")));
+ }
+
+ private Response handleDeleteManagedLoginBranding(JsonNode request) {
+ service.deleteManagedLoginBranding(
+ request.path("UserPoolId").asText(),
+ request.path("ManagedLoginBrandingId").asText(null));
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
+ private Response brandingResponse(ManagedLoginBranding branding) {
+ ObjectNode response = objectMapper.createObjectNode();
+ response.set("ManagedLoginBranding", managedLoginBrandingToNode(branding));
+ return Response.ok(response).build();
+ }
+
+ /** AWS omits Settings entirely when none was supplied, but always returns Assets. */
+ private ObjectNode managedLoginBrandingToNode(ManagedLoginBranding branding) {
+ ObjectNode node = objectMapper.createObjectNode();
+ node.put("ManagedLoginBrandingId", branding.getManagedLoginBrandingId());
+ node.put("UserPoolId", branding.getUserPoolId());
+ node.put("UseCognitoProvidedValues", branding.isUseCognitoProvidedValues());
+ if (branding.getSettings() != null) {
+ node.set("Settings", objectMapper.valueToTree(branding.getSettings()));
+ }
+ ArrayNode assets = node.putArray("Assets");
+ branding.getAssets().forEach(asset -> assets.add(objectMapper.valueToTree(asset)));
+ node.put("CreationDate", branding.getCreationDate());
+ node.put("LastModifiedDate", branding.getLastModifiedDate());
+ return node;
+ }
+
+ /** Absent or null yields null; a value of the wrong JSON type is a deserialization failure. */
+ private Map readObjectMap(JsonNode request, String member) {
+ JsonNode node = request.get(member);
+ if (node == null || node.isNull()) {
+ return null;
+ }
+ if (!node.isObject()) {
+ throw new AwsException("SerializationException", "Unexpected field type", 400);
+ }
+ return objectMapper.convertValue(node, new TypeReference>() {});
+ }
+
+ /**
+ * Elements are checked before conversion so a scalar cannot escape as an IllegalArgumentException.
+ * AWS separates the two failures: a null element is a validation error, a non-object element a
+ * deserialization one. Note this differs from LogConfigurations, where a null element is dropped.
+ */
+ private List