NokiaTube | New address | Mode Phone | Off high quality for Nokia Browser
Skip to content

Admin: Adding login auditing - #55586

Open
hypery2k wants to merge 4 commits into
keycloak:mainfrom
m13tLabs:feature/auditing
Open

hypery2k wants to merge 4 commits into
keycloak:mainfrom
m13tLabs:feature/auditing

Conversation

@hypery2k

Copy link
Copy Markdown
Contributor

Closes #52319

Primary an initial draft.

I left a comment in UserAdapter.java flagging that writing this field on every login triggers cluster-wide cache invalidation for that user's cache entry on every single login — there's no existing precedent in core for a per-login write at that volume, and it may need a write-through path that bypasses cache invalidation before this is stabilized.

@hypery2k
hypery2k requested review from a team as code owners November 26, 2026 17:09
Copilot AI balanced review requested due to automatic review settings November 26, 2026 17:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Enabling auditing can break valid logins for clients with URL-based IDs and read-only LDAP users through invalid profile attribute names and rejected attribute writes.

6 open issues
What changed in this PR

Introduces experimental login-activity tracking for #52319, allowing administrators to retrieve and filter users by their last successful authentication without querying stored login events.

Changes:

  • Adds a persistent, cached authentication timestamp to user models and API representations.
  • Records browser-login and client-credentials activity using plugin-compatible audit attributes.
  • Adds timestamp filters for user searches and counts, plus a legacy-attribute migration endpoint.
File Description
services/​src/​main/​java/​org/​keycloak/​userprofile/​LoginActivityAttributeRegistrar.java Registers admin-visible per-client audit attributes.
services/​src/​main/​java/​org/​keycloak/​services/​resources/​admin/​UsersResource.java Adds timestamp filters and legacy-data migration.
services/​src/​main/​java/​org/​keycloak/​services/​managers/​AuthenticationManager.java Records browser-login timestamps and per-client activity.
services/​src/​main/​java/​org/​keycloak/​protocol/​oidc/​grants/​ClientCredentialsGrantType.java Records client-credentials login activity.
server-spi/​src/​main/​java/​org/​keycloak/​models/​utils/​UserModelDelegate.java Delegates timestamp access to the underlying user model.
server-spi/​src/​main/​java/​org/​keycloak/​models/​UserModel.java Defines timestamp accessors and query keys.
server-spi-private/​src/​main/​java/​org/​keycloak/​models/​utils/​ModelToRepresentation.java Includes timestamps in full and brief user representations.
model/​jpa/​src/​main/​resources/​META-INF/​jpa-changelog-27.0.0.xml Adds the authentication timestamp database column.
model/​jpa/​src/​main/​java/​org/​keycloak/​models/​jpa/​UserAdapter.java Reads and writes the persistent timestamp.
model/​jpa/​src/​main/​java/​org/​keycloak/​models/​jpa/​JpaUserProvider.java Adds timestamp-range query predicates.
model/​jpa/​src/​main/​java/​org/​keycloak/​models/​jpa/​entities/​UserEntity.java Maps the timestamp to the user entity.
model/​infinispan/​src/​main/​java/​org/​keycloak/​models/​cache/​infinispan/​UserAdapter.java Adds cache-aware timestamp access and updates.
model/​infinispan/​src/​main/​java/​org/​keycloak/​models/​cache/​infinispan/​entities/​CachedUser.java Stores the timestamp in cached user snapshots.
core/​src/​main/​java/​org/​keycloak/​representations/​idm/​UserRepresentation.java Exposes the system-managed timestamp in user representations.
common/​src/​main/​java/​org/​keycloak/​common/​Profile.java Defines the experimental feature flag.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 26, 2026 18:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical defects can break logins for valid client IDs, allow unbounded migration queries, and overwrite concurrent user-profile configuration changes.

6 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Persist the timestamp for federated and LDAP users

server-spi/​src/​main/​java/​org/​keycloak/​models/​UserModel.java:112

Only the JPA adapter implements persistence for this setter; AbstractUserAdapterFederatedStorage and LDAP's non-imported InMemoryUserAdapter inherit this no-op, so their successful logins permanently leave the new field null. Add a durable federated-storage implementation rather than assuming every authenticated user has a USER_ENTITY row.

Medium severity Record last login after successful CIBA authentication

services/​src/​main/​java/​org/​keycloak/​protocol/​oidc/​grants/​ResourceOwnerPasswordCredentialsGrantType.java:182

The new hook covers password grants, but CibaGrantType creates a user session and returns tokens through createTokenResponse without invoking either this hook or redirectAfterSuccessfulFlow. Successful CIBA authentications therefore never update the timestamp; add recording at that flow's successful completion as well.

6 resolved since last review

Review effort: Balanced


if (modified) {
logger.infof("Registered last-login user profile attribute '%s' for realm '%s'", attrName, realm.getName());
upp.setConfiguration(config);
if (SAFE_ATTRIBUTE_NAME_CHARS.matcher(clientId).matches()) {
return PER_CLIENT_ATTRIBUTE_PREFIX + "_" + clientId;
}
return PER_CLIENT_ATTRIBUTE_PREFIX + "_" + UNSAFE_CHAR.matcher(clientId).replaceAll("_") + "_" + sha256Hex8(clientId);
Copilot AI balanced review requested due to automatic review settings November 26, 2026 18:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Admin searches can return LDAP users that violate authentication-time filters, while timestamps remain vulnerable to edits or loss and concurrent client registrations can overwrite one another.

4 open issues
2 previously missed issues

In code that hasn't changed since last review

High severity Apply timestamp filters to external provider results

services/​src/​main/​java/​org/​keycloak/​services/​resources/​admin/​UsersResource.java:340

These filters are implemented only by JPA, but this search also queries external providers. LDAPStorageProvider.searchForUserStream() uses its free-text search branch when SEARCH is present and ignores the timestamp bounds, so this endpoint can return LDAP users with a null timestamp even when lastSuccessfulAuthenticationAfter is specified. Apply the bounds consistently across providers before pagination/counting, or explicitly exclude providers that cannot satisfy them.

Medium severity Prevent concurrent client registrations from overwriting each other

services/​src/​main/​java/​org/​keycloak/​userprofile/​LoginActivityAttributeRegistrar.java:155

The lock is released before the request transaction commits, and getConfiguration() still reads the request's cached realm snapshot. Two first logins for different clients on the same node can therefore both read the old configuration and overwrite each other's registrations despite this synchronized block. Serialize the complete transaction and reload fresh configuration inside it, or move registration to a transactionally coordinated path.

4 resolved since last review

Review effort: Balanced

Comment on lines +317 to +319
String val = getFirstAttribute(LAST_SUCCESSFUL_AUTHENTICATION_ATTRIBUTE);
if (val == null) return null;
else return Long.valueOf(val);
@Override
public void setLastSuccessfulAuthentication(Long timestamp) {
checkReadonly();
this.lastSuccessfulAuthentication = timestamp;
@stianst stianst added the kind/feature Categorizes a PR related to a new feature label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Categorizes a PR related to a new feature team/core-shared

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose User Last Successful Authentication Timestamp

3 participants