diff --git a/docs/services/cognito.md b/docs/services/cognito.md index b384f9ee67..7ac230c518 100644 --- a/docs/services/cognito.md +++ b/docs/services/cognito.md @@ -748,14 +748,15 @@ client-credentials flow: ### Managed login -Managed login signs in the pool's own users with authorization code and, optionally, PKCE. -The client needs `COGNITO` in `SupportedIdentityProviders`, `AllowedOAuthFlows=["code"]` and +Managed login offers the client's identity providers with authorization code and, optionally, PKCE. +Native sign-in needs `COGNITO` in `SupportedIdentityProviders`; a federated-only client gets provider +links without a native sign-in form. The client needs `AllowedOAuthFlows=["code"]` and the callback in `CallbackURLs`. No domain is needed; on a custom domain the same flow runs at `/oauth2/authorize`, `/login` and `/logout`. 1. `GET /cognito-idp/oauth2/authorize` redirects to `/cognito-idp/login` with the request's parameters, `login_hint` included. If the browser already has a managed login session in the - pool, it skips the form and redirects straight to the callback with a code, as AWS does; the + pool and the client allows `COGNITO`, it skips the form and redirects straight to the callback with a code; the code's tokens carry the `auth_time` of the session's sign-in, not of this request. A `scope` the client's `AllowedOAuthScopes` does not include is refused first, as on AWS, with a redirect to the callback carrying `error=invalid_request`, `error_description=invalid_scope` @@ -763,7 +764,9 @@ the callback in `CallbackURLs`. No domain is needed; on a custom domain the same 2. `GET /cognito-idp/login` renders a plain username and password form, with the username filled in from `login_hint` when the request has one. The form carries the request in hidden fields and a CSRF token that must match the `XSRF-TOKEN` cookie set with it. With choice-based sign-in - (below), it asks for the username alone. + (below), it asks for the username alone. Links for the client's external providers retain the + authorization request, including state, nonce and PKCE. A client without `COGNITO` gets only + these links; native credential submission and explicit `identity_provider=COGNITO` are refused. 3. `POST /cognito-idp/login` checks the password as `USER_PASSWORD_AUTH` does, including sign-in aliases and the pre and post authentication and user migration triggers, but without the client's `ExplicitAuthFlows`. On success it sets a `cognito` session cookie diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoOAuthController.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoOAuthController.java index e407e6e6d5..402fde8b93 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoOAuthController.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoOAuthController.java @@ -81,10 +81,8 @@ public class CognitoOAuthController { } /** - * Without an {@code identity_provider}, or with {@code COGNITO}, signs in one of the pool's own - * users through managed login; any other value names an external provider to federate with. - * A client that does not list COGNITO cannot use managed login, even when it supports exactly - * one other provider: the caller names that provider instead. + * Without an {@code identity_provider}, offers the client's sign-in providers through managed + * login. An explicit provider selects native sign-in or external federation. */ @GET @Path("/cognito-idp/oauth2/authorize") @@ -104,7 +102,7 @@ public class CognitoOAuthController { relyingPartyState, nonce, codeChallenge, codeChallengeMethod, loginHint); String provider = trimToNull(providerName); if (provider == null || COGNITO_PROVIDER.equals(provider)) { - return authorizeWithManagedLogin(requestContext, request, sessionId); + return authorizeWithManagedLogin(requestContext, request, sessionId, provider != null); } AuthorizationCheck check = checkAuthorizationRequest(requestContext, request); @@ -127,12 +125,15 @@ public class CognitoOAuthController { * redirects to the login endpoint with the request's parameters. */ private Response authorizeWithManagedLogin(ContainerRequestContext requestContext, AuthorizationRequest request, - String sessionId) { - AuthorizationCheck check = checkManagedLoginRequest(requestContext, request); + String sessionId, boolean nativeProvider) { + AuthorizationCheck check = nativeProvider + ? checkManagedLoginRequest(requestContext, request) + : checkAuthorizationRequest(requestContext, request); if (check.error() != null) { return check.error(); } - Optional code = issueAuthorizationCode(sessionId, check.client(), request); + Optional code = check.client().getSupportedIdentityProviders().contains(COGNITO_PROVIDER) + ? issueAuthorizationCode(sessionId, check.client(), request) : Optional.empty(); if (code.isPresent()) { return redirectWithCode(request, code.get()); } @@ -156,7 +157,7 @@ public class CognitoOAuthController { @QueryParam("login_hint") String loginHint) { AuthorizationRequest request = new AuthorizationRequest(responseType, clientId, redirectUri, scope, state, nonce, codeChallenge, codeChallengeMethod, loginHint); - AuthorizationCheck check = checkManagedLoginRequest(requestContext, request); + AuthorizationCheck check = checkAuthorizationRequest(requestContext, request); if (check.error() != null) { return check.error(); } @@ -946,6 +947,22 @@ public class CognitoOAuthController { */ private Response signInPage(ContainerRequestContext requestContext, AuthorizationRequest request, Response.Status status, String error, List fields) { + UserPoolClient client = cognitoService.findClientById(request.clientId()); + if (client.getSupportedIdentityProviders().isEmpty() && error == null) { + error = "No sign-in providers are enabled for this application. Contact your administrator."; + } + String authorizePath = requestContext.getProperty(CognitoCustomDomainFilter.POOL_PROPERTY) != null + ? "/oauth2/authorize" : "/cognito-idp/oauth2/authorize"; + List providerLinks = new ArrayList<>(); + for (String provider : client.getSupportedIdentityProviders()) { + if (COGNITO_PROVIDER.equals(provider)) { + continue; + } + Map parameters = new LinkedHashMap<>(request.parameters()); + parameters.put("identity_provider", provider); + providerLinks.add("

Sign in with " + escapeHtml(provider) + "

"); + } String csrfToken = stateStore.generateOpaqueKey(); List formFields = new ArrayList<>(); for (Map.Entry parameter : request.parameters().entrySet()) { @@ -954,6 +971,13 @@ public class CognitoOAuthController { for (String field : fields) { formFields.add(" " + field); } + String form = client.getSupportedIdentityProviders().contains(COGNITO_PROVIDER) ? """ +
+ + %s +
+ """.formatted(escapeHtml(loginPath(requestContext)), CSRF_FIELD, escapeHtml(csrfToken), + String.join("\n", formFields)) : ""; String page = """ @@ -966,16 +990,13 @@ public class CognitoOAuthController {

Sign in

%s -
- %s -
+ %s
""".formatted(error == null ? "" : "

" + escapeHtml(error) + "

", - escapeHtml(loginPath(requestContext)), CSRF_FIELD, escapeHtml(csrfToken), - String.join("\n", formFields)); + String.join("\n", providerLinks), form); // No form-action directive: browsers apply it to the redirect after the form is submitted, // which goes to the client's callback on another origin. return Response.status(status) diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java index e83d99bd94..3e7b57c0cf 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java @@ -790,23 +790,105 @@ class CognitoManagedLoginIntegrationTest { } @Test - void clientWithoutCognitoAmongItsProvidersCannotUseManagedLogin() throws Exception { - String poolId = cognitoJson("CreateUserPool", "{\"PoolName\":\"FederatedOnlyPool\"}").path("UserPool").path("Id").asText(); + void federatedOnlyClientOffersItsProviderWithoutNativeCredentials() throws Exception { + Pool pool = federatedPool(false); + Map query = authorizeRequest(pool.clientId()); + Response page = loginPage(null, "/cognito-idp/oauth2/authorize", query); + assertFalse(page.asString().contains("No sign-in providers are enabled for this application." + + " Contact your administrator.

"), page.asString()); + assertFalse(page.asString().contains(" query = withField(authorizeRequest(pool.clientId()), "state", "