feat(cognito-idp): adding create/list/delete user pool client secret cognito-idp actions (#345)

* feat: add Cognito IDP list/add/delete user pool client secrets functionality
* feat: add Cognito IDP list/add/delete user pool client secrets functionality - addressing comments
* feat: add Cognito IDP list/add/delete user pool client secrets functionality - addressing comments
This commit is contained in:
Daniel Abeledo authored and GitHub committed 2026-04-14 23:11:58 -05:00
1 parent 022c214f6f
commit b9ddb8ee5d
5 files changed
+438 -19

No files matched your search

@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.cognito.model.ResourceServer;
import io.github.hectorvent.floci.services.cognito.model.ResourceServerScope;
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 jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
@@ -73,6 +74,9 @@ public class CognitoJsonHandler {
case "AdminRemoveUserFromGroup" -> handleAdminRemoveUserFromGroup(request);
case "AdminListGroupsForUser" -> handleAdminListGroupsForUser(request);
case "GetTokensFromRefreshToken" -> handleGetTokensFromRefreshToken(request);
case "ListUserPoolClientSecrets" -> handleListUserPoolClientSecrets(request);
case "AddUserPoolClientSecret" -> handleAddUserPoolClientSecret(request);
case "DeleteUserPoolClientSecret" -> handleDeleteUserPoolClientSecret(request);
default -> Response.status(400)
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))
.build();
@@ -626,4 +630,52 @@ public class CognitoJsonHandler {
return node;
}
private Response handleListUserPoolClientSecrets(JsonNode request) {
List<UserPoolClientSecret> clientSecrets =
service.listUserPoolClientSecrets(
request.path("UserPoolId").asText(),
request.path("ClientId").asText()
);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode items = response.putArray("ClientSecrets");
clientSecrets.forEach(cs -> items.add(clientSecretToNode(cs, false)));
return Response.ok(response).build();
}
private Response handleAddUserPoolClientSecret(JsonNode request) {
String clientId = request.path("ClientId").asText();
String clientSecret = request.path("ClientSecret").asText(null);
String userPoolId = request.path("UserPoolId").asText();
UserPoolClientSecret cs = service.addUserPoolClientSecret(
clientId, clientSecret, userPoolId
);
boolean includeClientSecretValue = clientSecret == null;
ObjectNode wrapper = objectMapper.createObjectNode();
wrapper.set("ClientSecretDescriptor", clientSecretToNode(cs, includeClientSecretValue));
return Response.ok(wrapper).build();
}
private Response handleDeleteUserPoolClientSecret(JsonNode request) {
String clientId = request.path("ClientId").asText();
String clientSecretId = request.path("ClientSecretId").asText();
String userPoolId = request.path("UserPoolId").asText();
service.deleteUserPoolClientSecret(clientId, clientSecretId, userPoolId);
return Response.ok(objectMapper.createObjectNode()).build();
}
private ObjectNode clientSecretToNode(UserPoolClientSecret cs,
boolean includeClientSecretValue) {
ObjectNode node = objectMapper.createObjectNode();
node.put("ClientSecretId", cs.getClientSecretId());
if (includeClientSecretValue) {
node.put("ClientSecretValue", cs.getClientSecretValue());
}
node.put("ClientSecretCreateDate", cs.getClientSecretCreateDate());
return node;
}
}
@@ -1,34 +1,24 @@
package io.github.hectorvent.floci.services.cognito;
import io.github.hectorvent.floci.core.common.AwsException;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.services.cognito.model.CognitoGroup;
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
import io.github.hectorvent.floci.services.cognito.model.ResourceServer;
import io.github.hectorvent.floci.services.cognito.model.ResourceServerScope;
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.*;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ApplicationScoped
@@ -177,7 +167,17 @@ public class CognitoService {
client.setAllowedOAuthFlows(normalizeStringList(allowedOAuthFlows));
client.setAllowedOAuthScopes(normalizeStringList(allowedOAuthScopes));
if (generateSecret) {
client.setClientSecret(generateSecretValue());
String clientSecret = generateSecretValue();
client.setClientSecret(clientSecret);
long epochMillis = System.currentTimeMillis();
UserPoolClientSecret userPoolClientSecret = new UserPoolClientSecret(
clientId + "--" + epochMillis,
epochMillis / 1000,
clientSecret
);
client.getUserPoolClientSecrets().add(userPoolClientSecret);
}
clientStore.put(clientId, client);
LOG.infov("Created User Pool Client: {0} for pool {1}", clientId, userPoolId);
@@ -202,6 +202,71 @@ public class CognitoService {
clientStore.delete(clientId);
}
public List<UserPoolClientSecret> listUserPoolClientSecrets(String userPoolId, String clientId) {
UserPoolClient client = clientStore.get(clientId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "User pool client not found", 404));
if (!client.getUserPoolId().equals(userPoolId)) {
throw new AwsException("ResourceNotFoundException", "User pool client not found", 404);
}
return client.getUserPoolClientSecrets();
}
public UserPoolClientSecret addUserPoolClientSecret(String clientId, String clientSecret, String userPoolId) {
UserPoolClient client = clientStore.get(clientId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "User pool client not found", 404));
if (!client.getUserPoolId().equals(userPoolId)) {
throw new AwsException("ResourceNotFoundException", "User pool client not found", 404);
}
if (client.getUserPoolClientSecrets().size() >= 2) {
throw new AwsException("LimitExceededException", "Client secrets cannot exceed limit of 2 secrets.", 400);
}
if (clientSecret == null) {
clientSecret = generateSecretValue();
} else if (!clientSecret.matches("\\w{24,64}")) {
throw new AwsException("InvalidParameterException",
"Client secret format is invalid.", 400);
}
long epochMillis = System.currentTimeMillis();
UserPoolClientSecret userPoolClientSecret = new UserPoolClientSecret(
clientId + "--" + epochMillis,
epochMillis / 1000,
clientSecret
);
client.getUserPoolClientSecrets().add(userPoolClientSecret);
return userPoolClientSecret;
}
public void deleteUserPoolClientSecret(String clientId, String clientSecretId, String userPoolId) {
UserPoolClient client = clientStore.get(clientId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "User pool client not found", 404));
if (!client.getUserPoolId().equals(userPoolId)) {
throw new AwsException("ResourceNotFoundException", "User pool client not found", 404);
}
UserPoolClientSecret userPoolClientSecret = client.getUserPoolClientSecrets().stream()
.filter(s -> s.getClientSecretId().equals(clientSecretId))
.findFirst()
.orElseThrow(() -> new AwsException(
"ResourceNotFoundException", "Client secret does not exist", 404));
if (client.getUserPoolClientSecrets().size() <= 1) {
throw new AwsException(
"InvalidParameterException", "Cannot delete the only " +
"client secret.", 400
);
}
if (userPoolClientSecret.getClientSecretValue().equals(client.getClientSecret())) {
client.setClientSecret(null);
}
client.getUserPoolClientSecrets().remove(userPoolClientSecret);
}
// ──────────────────────────── Resource Servers ────────────────────────────
public ResourceServer createResourceServer(String userPoolId, String identifier, String name,
@@ -967,15 +1032,23 @@ public class CognitoService {
private void validateClientSecret(UserPoolClient client, String clientSecret) {
String expectedSecret = client.getClientSecret();
if (expectedSecret == null || expectedSecret.isBlank() || !client.isGenerateSecret()) {
if (client.getUserPoolClientSecrets().isEmpty()
&& (expectedSecret == null || expectedSecret.isBlank() || !client.isGenerateSecret())) {
throw new AwsException("InvalidClientException", "Client must have a secret for client_credentials", 400);
}
if (clientSecret == null || clientSecret.isBlank()) {
throw new AwsException("InvalidClientException", "Client secret is required", 400);
}
if (!expectedSecret.equals(clientSecret)) {
throw new AwsException("InvalidClientException", "Client secret is invalid", 400);
for (UserPoolClientSecret userPoolClientSecret : client.getUserPoolClientSecrets()) {
if (clientSecret.equals(userPoolClientSecret.getClientSecretValue())) {
return;
}
}
// for "legacy" clients
if (expectedSecret != null && expectedSecret.equals(clientSecret)) {
return;
}
throw new AwsException("InvalidClientException", "Client secret is invalid", 400);
}
private void validateClientAllowsClientCredentials(UserPoolClient client) {
@@ -13,6 +13,7 @@ public class UserPoolClient {
private String userPoolId;
private String clientName;
private String clientSecret;
private List<UserPoolClientSecret> userPoolClientSecrets = new ArrayList<>();
private boolean generateSecret;
private boolean allowedOAuthFlowsUserPoolClient;
private List<String> allowedOAuthFlows = new ArrayList<>();
@@ -38,6 +39,13 @@ public class UserPoolClient {
public String getClientSecret() { return clientSecret; }
public void setClientSecret(String clientSecret) { this.clientSecret = clientSecret; }
public List<UserPoolClientSecret> getUserPoolClientSecrets() {
return userPoolClientSecrets;
}
public void setUserPoolClientSecrets(List<UserPoolClientSecret> userPoolClientSecrets) {
this.userPoolClientSecrets = userPoolClientSecrets;
}
public boolean isGenerateSecret() { return generateSecret; }
public void setGenerateSecret(boolean generateSecret) { this.generateSecret = generateSecret; }
@@ -0,0 +1,44 @@
package io.github.hectorvent.floci.services.cognito.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserPoolClientSecret {
private String clientSecretId;
private String clientSecretValue;
private long clientSecretCreateDate;
public UserPoolClientSecret() {
// empty constructor for jackson deserialisation
}
public UserPoolClientSecret(String clientSecretId,
long clientSecretCreateDate, String clientSecretValue) {
this.clientSecretCreateDate = clientSecretCreateDate;
this.clientSecretId = clientSecretId;
this.clientSecretValue = clientSecretValue;
}
public long getClientSecretCreateDate() {
return clientSecretCreateDate;
}
public void setClientSecretCreateDate(long clientSecretCreateDate) {
this.clientSecretCreateDate = clientSecretCreateDate;
}
public String getClientSecretId() {
return clientSecretId;
}
public void setClientSecretId(String clientSecretId) {
this.clientSecretId = clientSecretId;
}
public String getClientSecretValue() {
return clientSecretValue;
}
public void setClientSecretValue(String clientSecretValue) {
this.clientSecretValue = clientSecretValue;
}
}