Skip to content

Save Remember Me authNote when an organization scope is set#47023

Open
Esurnir wants to merge 3 commits intokeycloak:mainfrom
Esurnir:bugfix/SetAuthNoteWithOrganizationScope_47017
Open

Save Remember Me authNote when an organization scope is set#47023
Esurnir wants to merge 3 commits intokeycloak:mainfrom
Esurnir:bugfix/SetAuthNoteWithOrganizationScope_47017

Conversation

@Esurnir
Copy link
Copy Markdown

@Esurnir Esurnir commented Mar 10, 2026

Prevent cleanup of Remember Me by AbstractUsernameFormAuthenticator if the checkbox is absent from the page (which is the case when using organization flow).

This PR make the assumption that OrganizationAuthenticator will execute before AbstractUserNameFormAuthenticator when the organization scope is specified.

Generative AI was used during the writing of this pull request for the code. Originally it was just to figure out where the issue lied but since the fix it proposed (which I tested locally) worked. I figured I would make a pull request about it.
I made some adjustment to mimic the "remove auth note when rememberMe is absent". Otherwise it looked like the note would never get cleaned up from a session.

Closes #47017

@Esurnir Esurnir requested a review from a team as a code owner March 10, 2026 13:57
@Esurnir Esurnir marked this pull request as draft March 10, 2026 16:04
@Esurnir
Copy link
Copy Markdown
Author

Esurnir commented Mar 10, 2026

Currently writing tests for the pull request. I'll reopen once the tests are written.

@Esurnir Esurnir force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch from d819b53 to 646bdfe Compare March 10, 2026 16:52
@Esurnir Esurnir marked this pull request as ready for review March 10, 2026 16:52
@Esurnir Esurnir requested review from a team as code owners March 10, 2026 16:52
@Esurnir
Copy link
Copy Markdown
Author

Esurnir commented Mar 10, 2026

Tests added to OrganizationAuthenticationTests (those I'll admit I let claude create them).

I ran those test with the pre-PR code and they fail, and only pass once the PR code is added. Those tests were written using Generative AI.

For the tests I originally tried to use a similar test to the login test but unfortunately the behavior is different, the box doesn't seem pre-checked when the remember me flag is set.
Checking the event didn't seem enough to guarantee the rememberMe flag is set.
So with some further research the AI settled on checking the rememberMe flag on the session list for the user. I'm not sure if there's a better way to check for it.

@Esurnir Esurnir force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch from 646bdfe to 3759e09 Compare March 10, 2026 18:15
mposolda added a commit to mposolda/keycloak that referenced this pull request Mar 17, 2026
closes keycloak#47023

Signed-off-by: mposolda <mposolda@gmail.com>
@pedroigor
Copy link
Copy Markdown
Contributor

Thanks, @Esurnir. LGTM. Running CI.

@pedroigor pedroigor force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch 3 times, most recently from 3a8005c to 3eddf64 Compare April 2, 2026 22:07
@pedroigor
Copy link
Copy Markdown
Contributor

@Esurnir Added a commit to keep remember-me enabled in the login form after the session expires or is destroyed (not on logouts, as the current behavior does not keep the cookie).

Also updated tests.

@Esurnir Esurnir force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch from 3eddf64 to 1a8d354 Compare April 27, 2026 19:32
@Esurnir Esurnir force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch from 2223e61 to 5120bdd Compare May 6, 2026 12:45
@mabartos
Copy link
Copy Markdown
Member

mabartos commented May 7, 2026

@Esurnir Would it be possible to create the new test case in the new test framework? In the /tests/base module.

Otherwise, the Testsuite Deprecation Check will always wail.

Thanks!

cc: @pedroigor

@Esurnir
Copy link
Copy Markdown
Author

Esurnir commented May 7, 2026

@Esurnir Would it be possible to create the new test case in the new test framework? In the /tests/base module.

Otherwise, the Testsuite Deprecation Check will always wail.

Thanks!

cc: @pedroigor

I'll try my best ! There's a few tests I modified in the old arquilian based test. Should I :
A. Migrate those old test to the new test file I'll create for my test cases.
B. Leave them in the old test files and modify them.
C. Migrate the full two test files (it would preserve history, but it might be a bit too much for the scope of this pr ?).

@Esurnir Esurnir force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch from 5120bdd to 206865b Compare May 8, 2026 10:48
Esurnir added a commit to Esurnir/keycloak that referenced this pull request May 8, 2026
Move testRememberMeWithOrganizationScope and
testRememberMeNotCheckedWithOrganizationScope from the legacy Arquillian
testsuite into tests/base under the new Keycloak Test Framework, addressing
the Testsuite Deprecation Check raised on PR keycloak#47023.

The new file uses the identity-first LoginUsernamePage for the username
step and LoginPage (with username hidden) for the password step, the
Events injection for login event assertions, and
realm.admin().deleteSession for session expiry. A small awaitNextEvent
helper retries events.poll() with a short backoff to handle the brief
window between OAuth callback completion and login event persistence in
the event store.

The legacy OrganizationAuthenticationTest no longer contains those two
methods. The AssertEvents @rule field and five imports it required
(Details, EventRepresentation, UserSessionRepresentation, AssertEvents,
org.junit.Rule) are removed since no remaining test in the legacy class
uses them.

Closes keycloak#47017

Co-authored-by: Pedro Igor <pigor.craveiro@gmail.com>
Signed-off-by: Jean-Baptiste Zeller <esurnir@gmail.com>
@Esurnir
Copy link
Copy Markdown
Author

Esurnir commented May 8, 2026

@mabartos done. pushed in the latest commit.
The two new tests now live in tests/base/.../organization/authentication/OrganizationRememberMeAuthenticationTest.java and I reset the legacy OrganizationAuthenticationTest.

A couple of choices worth flagging:

  • Focused class name (OrganizationRememberMeAuthenticationTest) rather than OrganizationAuthenticationTest: wanted to leave the conventional destination empty so a future whole-file migration with migration-util/migrate.sh can preserve git history via commit-migration.sh.
  • Small awaitNextEvent helper : events.poll() returned null intermittently in a 20× stability run because the LOGIN event isn't always persisted by the time the OAuth callback completes. Use Awaitility to retry until the event is available.

Thanks for your guidance!

cc: @pedroigor

@Esurnir Esurnir marked this pull request as draft May 9, 2026 10:16
@Esurnir
Copy link
Copy Markdown
Author

Esurnir commented May 9, 2026

AI code reviewer claim the bug would still be there in a multi org flow. So I'm letting it run a tiny bit more before shipping the PR.

@Esurnir
Copy link
Copy Markdown
Author

Esurnir commented May 9, 2026

Ok confirmed locally and fixed it (with gen ai assitance). A new test case has been added to cover the 'multi organization' flow so that remember me doesn't get cleared if the user go through the select-organization switch. The change become based on if the user has sent the rememberMe box inside the form or not.
I didn't realize the select organization box code was also running inside OrganizationAuthenticator.

Tests done locally :

  • logging in with a user with two organization using organization scope and ticking the remember me box. Clicking one of the two organization. Entering my password. Then closing my browser. Reopening it. Expected : I'm still logged in.
  • logging in with a user with two organization using organization scope and not ticking the remember me box. Clicking one of the two organization. Entering my password. Then closing my browser. Reopening it. Expected : I'm logged out.

Esurnir and others added 2 commits May 9, 2026 14:06
Modify the OrganizationAuthenticator to save the rememberMe note since
it is the Authenticator receiving the username form request during
organization enabled login flow.
Prevent cleanup of Remember Me by AbstractUsernameFormAuthenticator
if the checkbox is absent from the page.
Add test case for OrganizationAuthenticationTest that check the session
is created with the rememberMe flag active.

Closes keycloak#47017

Signed-off-by: Jean-Baptiste Zeller <esurnir@gmail.com>
Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
@Esurnir Esurnir force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch from ecfdc30 to 22cdb65 Compare May 9, 2026 12:11
OrganizationAuthenticator.action unconditionally called processRememberMe
on every form submission. The select-organization.ftl form, posted by
multi-org members during the org-selection step, does not carry the
rememberMe field, so the call fell into AuthenticatorUtils.processRememberMe's
else branch and removed the authNote previously saved by the same
authenticator on the identity-first submission.

Gate the call by presence of the rememberMe parameter, mirroring
WebAuthnPasswordlessAuthenticator. Also use the local form variable
in createLoginForm for consistency with the loginHint branch above.

Migrate the existing Remember Me Organization scope tests to the new
test framework, and add testRememberMePreservedThroughSelectOrganization
covering the multi-org case (member of two organizations, picks one on
the org selection page, verifies the user session still has rememberMe
enabled).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Jean-Baptiste Zeller <esurnir@gmail.com>
@Esurnir Esurnir force-pushed the bugfix/SetAuthNoteWithOrganizationScope_47017 branch from 22cdb65 to 14aa03d Compare May 9, 2026 12:13
@Esurnir Esurnir marked this pull request as ready for review May 9, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remember me does not work when organization scope is enabled on login session.

3 participants