fix(cognito): keep the sign-in's auth_time across managed login sessions and refresh (#5157)

The tokens an authorization code redeems for were stamped with the time of
the redemption, so an authorize that a managed login session let skip the
form reported a fresh sign-in, and so did every refresh of those tokens.

The managed login session now records when the user signed in, the
authorization code carries that time (or the identity provider callback's
for federated sign-in), and the token endpoint mints with it. Refresh tokens
carry it too, so REFRESH_TOKEN_AUTH, GetTokensFromRefreshToken and the token
endpoint's refresh_token grant keep the original auth_time, as AWS
documents: "Refreshing a token doesn't reset the auth_time claim."

The sign-in time is a tagged auth_time= field after the family id, ahead of
the scope field. Refresh tokens minted before keep working and fall back to
the time they were issued.

A pre token generation trigger can no longer suppress or override auth_time,
which AWS lists among the claims a trigger cannot add, modify or suppress.
This commit is contained in:
jwoa95 authored and GitHub committed 2026-10-20 11:48:02 -04:00
1 parent 0ebcf84653
commit d1a2708634
14 files changed
+329 -98

No files matched your search

@@ -712,7 +712,7 @@ final class CognitoAuthFlowHandler {
clientMetadata, "TokenGeneration_RefreshTokens", scopes);
Map<String, Object> result = new HashMap<>();
result.put("AuthenticationResult", service.accessAndIdTokens(user, pool, client, override, refreshTokenUuid,
scopes, iat / 1000L));
scopes, CognitoService.refreshTokenAuthTime(parts, iat)));
return result;
}
@@ -95,10 +95,11 @@ public class CognitoFederationService {
String issuer = optionalText(claims, "iss");
CognitoUser user = cognitoService.provisionFederatedUser(transaction.userPoolId(), provider,
subject, issuer, mapAttributes(provider, claims));
Instant signedInAt = clock.instant();
CognitoAuthorizationCode authorizationCode = new CognitoAuthorizationCode(
transaction.userPoolId(), transaction.clientId(), user.getUsername(), transaction.redirectUri(),
transaction.scopes(), transaction.nonce(), transaction.codeChallenge(),
clock.instant().plus(AUTHORIZATION_CODE_LIFETIME));
signedInAt, signedInAt.plus(AUTHORIZATION_CODE_LIFETIME));
return stateStore.putAuthorizationCode(authorizationCode);
}
@@ -11,6 +11,7 @@ import org.jboss.logging.Logger;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
@@ -92,14 +93,17 @@ public class CognitoManagedLoginService {
}
private String openSession(UserPoolClient client, CognitoUser user) {
Instant signedInAt = clock.instant();
return stateStore.putSession(new CognitoManagedLoginSession(
client.getUserPoolId(), user.getUsername(), clock.instant().plus(SESSION_LIFETIME)));
client.getUserPoolId(), user.getUsername(), signedInAt, signedInAt.plus(SESSION_LIFETIME)));
}
/**
* Issues an authorization code to the user of the browser's session, or nothing when there is
* no session for the client's pool. Every pool's session travels in the same cookie on Floci's
* own host, so a session of one pool must never sign its user in to another.
* own host, so a session of one pool must never sign its user in to another. The code keeps the
* session's sign-in time, so its tokens carry the {@code auth_time} of the sign-in, not of this
* request.
*
* @param grantedScopes the scopes the authorization request was granted, which the code keeps
*/
@@ -116,7 +120,7 @@ public class CognitoManagedLoginService {
}
CognitoAuthorizationCode code = new CognitoAuthorizationCode(client.getUserPoolId(), client.getClientId(),
session.get().username(), redirectUri, grantedScopes, nonce, codeChallenge,
clock.instant().plus(AUTHORIZATION_CODE_LIFETIME));
session.get().authTime(), clock.instant().plus(AUTHORIZATION_CODE_LIFETIME));
return Optional.of(stateStore.putAuthorizationCode(code));
}
@@ -619,7 +619,7 @@ public class CognitoOAuthController {
UserPool pool = cognitoService.describeUserPool(consumedCode.userPoolId());
CognitoUser user = cognitoService.adminGetUser(consumedCode.userPoolId(), consumedCode.userId());
Map<String, Object> authentication = cognitoService.generateAuthResultForHostedAuth(user, pool, client,
nonceClaim(consumedCode.nonce()), consumedCode.scopes());
nonceClaim(consumedCode.nonce()), consumedCode.scopes(), consumedCode.authTime());
return tokenResponse(authentication);
} catch (AwsException e) {
LOG.error("Failed to redeem Cognito authorization code", e);
@@ -137,6 +137,11 @@ public class CognitoService implements ResourceProvider {
* starts with it, so a token whose username holds a pipe is never read as one with scopes.
*/
private static final String REFRESH_TOKEN_SCOPE_FIELD = "scope=";
/**
* Starts the refresh token field that holds when the user signed in, in epoch seconds. A family id
* never starts with it either, so a token minted before the field existed is never read as one with it.
*/
private static final String REFRESH_TOKEN_AUTH_TIME_FIELD = "auth_time=";
private static final String IDENTITIES_ATTRIBUTE = "identities";
@@ -4433,7 +4438,7 @@ public class CognitoService implements ResourceProvider {
// Use refresh token UUID as origin_jti for derived tokens
Map<String, Object> result = new HashMap<>();
result.put("AuthenticationResult", accessAndIdTokens(user, pool, client, override, refreshTokenUuid,
scopes, issuedAt / 1000L));
scopes, refreshTokenAuthTime(parts, issuedAt)));
return result;
}
@@ -4533,13 +4538,17 @@ public class CognitoService implements ResourceProvider {
* allows, rather than the {@code aws.cognito.signin.user.admin} of an API sign-in, and the ID
* token is minted only when they include {@code openid}, as on AWS. The trigger is told the same
* scopes, and a V2 trigger's scope changes apply on top of them.
*
* <p>{@code authTime} is when the user signed in, which the code kept: the tokens carry it as
* {@code auth_time}, and so does every token refreshed from them.
*/
Map<String, Object> generateAuthResultForHostedAuth(CognitoUser user, UserPool pool, UserPoolClient client,
ClaimsOverride protocolClaims, List<String> grantedScopes) {
ClaimsOverride protocolClaims, List<String> grantedScopes,
Instant authTime) {
List<String> scopes = scopesStillAllowed(client, grantedScopes);
ClaimsOverride trigger = authFlowHandler.preTokenGenerationForHostedAuth(pool, client, user, scopes);
return generateAuthResult(user, pool, client, mergeUnderProtocolClaims(trigger, protocolClaims),
UUID.randomUUID().toString(), scopes);
UUID.randomUUID().toString(), scopes, authTime.getEpochSecond());
}
/**
@@ -4612,22 +4621,25 @@ public class CognitoService implements ResourceProvider {
}
Map<String, Object> generateAuthResult(CognitoUser user, UserPool pool, UserPoolClient client, ClaimsOverride override, String originJti) {
return generateAuthResult(user, pool, client, override, originJti, null);
return generateAuthResult(user, pool, client, override, originJti, null, null);
}
/**
* @param grantedScopes the scopes of the OAuth grant the tokens are minted for, or null for a
* sign-in through the API. The refresh token keeps them, so that a refresh
* mints the same kind of tokens.
* @param authTime when the user signed in, epoch seconds, which the refresh token keeps too;
* null for a sign-in through the API, which the user completes with this request
*/
private Map<String, Object> generateAuthResult(CognitoUser user, UserPool pool, UserPoolClient client,
ClaimsOverride override, String originJti,
List<String> grantedScopes) {
List<String> grantedScopes, Long authTime) {
long issuedAt = System.currentTimeMillis();
long signedInAt = authTime != null ? authTime : issuedAt / 1000L;
Map<String, Object> auth = accessAndIdTokens(user, pool, client, override, originJti, grantedScopes,
issuedAt / 1000L);
signedInAt);
auth.put("RefreshToken", buildRefreshToken(pool, user.getUsername(), client.getClientId(), issuedAt,
originJti, grantedScopes));
originJti, grantedScopes, signedInAt));
return auth;
}
@@ -4761,12 +4773,12 @@ public class CognitoService implements ResourceProvider {
Map<String, Object> addOrOverride = isAccess ? override.accessClaimsToAddOrOverride() : override.idClaimsToAddOrOverride();
if (suppress != null) {
suppress.stream()
.filter(claim -> !isTriggerProtectedIdentityClaim(claim, isAccess))
.filter(claim -> !isTriggerProtectedClaim(claim, isAccess))
.forEach(claims::remove);
}
if (addOrOverride != null) {
addOrOverride.forEach((claim, value) -> {
if (!isTriggerProtectedIdentityClaim(claim, isAccess)) {
if (!isTriggerProtectedClaim(claim, isAccess)) {
claims.put(claim, value);
}
});
@@ -4802,10 +4814,12 @@ public class CognitoService implements ResourceProvider {
/**
* AWS lets no pre token generation trigger add, modify or suppress the claims that identify
* the user: {@code sub} in either token, {@code username} in the access token and
* {@code cognito:username} in the ID token.
* {@code cognito:username} in the ID token. Nor {@code auth_time}, when the user signed in, in
* either token.
*/
private static boolean isTriggerProtectedIdentityClaim(String claim, boolean isAccess) {
return "sub".equals(claim) || (isAccess ? "username" : "cognito:username").equals(claim);
private static boolean isTriggerProtectedClaim(String claim, boolean isAccess) {
return "sub".equals(claim) || "auth_time".equals(claim)
|| (isAccess ? "username" : "cognito:username").equals(claim);
}
private String encodeJwtHeader(UserPool pool) {
@@ -5492,17 +5506,20 @@ public class CognitoService implements ResourceProvider {
}
/**
* A signed {@code poolId|username|clientId|issuedAt|familyId} token. An OAuth grant's token adds a
* field holding its scopes, base64url-encoded because a scope may contain the pipe that separates
* the fields; a sign-in through the API, {@code grantedScopes} null, has none.
* A signed {@code poolId|username|clientId|issuedAt|familyId|authTime} token, {@code authTime} being
* when the user signed in, which a managed login session can put well before the token is issued.
* An OAuth grant's token adds a field holding its scopes, base64url-encoded because a scope may
* contain the pipe that separates the fields; a sign-in through the API, {@code grantedScopes}
* null, has none.
*/
String buildRefreshToken(UserPool pool, String username, String clientId, long issuedAt, String originJti,
List<String> grantedScopes) {
List<String> grantedScopes, long authTime) {
if (ensureRefreshTokenSecret(pool)) {
poolStore.put(pool.getId(), pool);
}
String familyId = originJti != null ? originJti : UUID.randomUUID().toString();
String raw = pool.getId() + "|" + username + "|" + clientId + "|" + issuedAt + "|" + familyId;
String raw = pool.getId() + "|" + username + "|" + clientId + "|" + issuedAt + "|" + familyId
+ "|" + REFRESH_TOKEN_AUTH_TIME_FIELD + authTime;
if (grantedScopes != null) {
raw += "|" + REFRESH_TOKEN_SCOPE_FIELD + Base64Url.encode(String.join(" ", grantedScopes));
}
@@ -5522,8 +5539,9 @@ public class CognitoService implements ResourceProvider {
/**
* The fields of a refresh token whose signature verifies: {@code [poolId, username, clientId,
* issuedAt, familyId]}, and for an OAuth grant's token a sixth, its scopes separated by spaces.
* Only the username can hold a pipe, so the fields after it are read from the end of the token.
* issuedAt, familyId, authTime]}, {@code authTime} empty for a token minted before refresh tokens
* kept it, and for an OAuth grant's token a seventh, its scopes separated by spaces. Only the
* username can hold a pipe, so the fields after it are read from the end of the token.
*/
String[] parseRefreshToken(String refreshToken) {
try {
@@ -5532,9 +5550,16 @@ public class CognitoService implements ResourceProvider {
int signatureAt = raw.lastIndexOf('|');
String payload = raw.substring(0, signatureAt);
String[] parts = payload.split("\\|", -1);
boolean hasScopes = parts[parts.length - 1].startsWith(REFRESH_TOKEN_SCOPE_FIELD);
int trailing = hasScopes ? 4 : 3;
if (parts.length < 2 + trailing) {
int end = parts.length;
boolean hasScopes = parts[end - 1].startsWith(REFRESH_TOKEN_SCOPE_FIELD);
if (hasScopes) {
end--;
}
boolean hasAuthTime = end > 0 && parts[end - 1].startsWith(REFRESH_TOKEN_AUTH_TIME_FIELD);
if (hasAuthTime) {
end--;
}
if (end < 5) {
return null;
}
UserPool pool = poolStore.get(parts[0]).orElse(null);
@@ -5546,13 +5571,14 @@ public class CognitoService implements ResourceProvider {
if (!MessageDigest.isEqual(expectedSignature, actualSignature)) {
return null;
}
int usernameEnd = parts.length - trailing;
String[] fields = new String[hasScopes ? 6 : 5];
int usernameEnd = end - 3;
String[] fields = new String[hasScopes ? 7 : 6];
fields[0] = parts[0];
fields[1] = String.join("|", Arrays.copyOfRange(parts, 1, usernameEnd));
System.arraycopy(parts, usernameEnd, fields, 2, 3);
fields[5] = hasAuthTime ? parts[end].substring(REFRESH_TOKEN_AUTH_TIME_FIELD.length()) : "";
if (hasScopes) {
fields[5] = new String(Base64.getUrlDecoder().decode(
fields[6] = new String(Base64.getUrlDecoder().decode(
parts[parts.length - 1].substring(REFRESH_TOKEN_SCOPE_FIELD.length())), StandardCharsets.UTF_8);
}
return fields;
@@ -5562,10 +5588,20 @@ public class CognitoService implements ResourceProvider {
/** The scopes of the OAuth grant a parsed refresh token belongs to, or null for a sign-in through the API. */
static List<String> refreshTokenScopes(String[] parts) {
if (parts.length < 6) {
if (parts.length < 7) {
return null;
}
return parts[5].isEmpty() ? List.of() : List.of(parts[5].split(" "));
return parts[6].isEmpty() ? List.of() : List.of(parts[6].split(" "));
}
/**
* When the user signed in to get a parsed refresh token, in epoch seconds: every token refreshed
* from it keeps that as its {@code auth_time}, since "Refreshing a token doesn't reset the
* {@code auth_time} claim" on AWS. A token minted before refresh tokens kept it falls back to when
* it was issued, {@code issuedAtMillis}.
*/
static long refreshTokenAuthTime(String[] parts, long issuedAtMillis) {
return parts[5].isEmpty() ? issuedAtMillis / 1000L : Long.parseLong(parts[5]);
}
record VerifiedAccessToken(String username, String poolId, String subject, String clientId) {}
@@ -8,7 +8,9 @@ import java.util.List;
* when the code was issued, which redemption narrows to the client's current AllowedOAuthScopes but
* never widens. {@code nonce} and {@code codeChallenge} are the authorization request's values, or
* null when it sent none; the token endpoint puts the nonce in the ID token and requires a matching
* {@code code_verifier} when there is a challenge.
* {@code code_verifier} when there is a challenge. {@code authTime} is when the user signed in, which
* can be well before the code was issued when a managed login session skipped the sign-in form; the
* tokens the code redeems for carry it as {@code auth_time}.
*/
public record CognitoAuthorizationCode(
String userPoolId,
@@ -18,6 +20,7 @@ public record CognitoAuthorizationCode(
List<String> scopes,
String nonce,
String codeChallenge,
Instant authTime,
Instant expiresAt) {
public CognitoAuthorizationCode {
@@ -4,7 +4,8 @@ import java.time.Instant;
/**
* A browser's managed login session, which lets {@code /oauth2/authorize} skip the sign-in form.
* {@code username} is the user's canonical username, not the alias they typed.
* {@code username} is the user's canonical username, not the alias they typed. {@code authTime} is
* when the user signed in, which every token issued through the session carries as {@code auth_time}.
*/
public record CognitoManagedLoginSession(String userPoolId, String username, Instant expiresAt) {
public record CognitoManagedLoginSession(String userPoolId, String username, Instant authTime, Instant expiresAt) {
}