Skip to content

Fix OIDC reauthentication when a session is involved #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,18 @@ abstract void authenticateAsync(InternalConnection connection, ConnectionDescrip
OperationContext operationContext, SingleResultCallback<Void> callback);

public void reauthenticate(final InternalConnection connection, final OperationContext operationContext) {
authenticate(connection, connection.getDescription(), operationContext);
authenticate(connection, connection.getDescription(), operationContextWithoutSession(operationContext));
}

public void reauthenticateAsync(final InternalConnection connection, final OperationContext operationContext,
final SingleResultCallback<Void> callback) {
beginAsync().thenRun((c) -> {
authenticateAsync(connection, connection.getDescription(), operationContext, c);
authenticateAsync(connection, connection.getDescription(), operationContextWithoutSession(operationContext), c);
}).finish(callback);
}

private static OperationContext operationContextWithoutSession(final OperationContext operationContext) {
return operationContext.withSessionContext(
new ReadConcernAwareNoOpSessionContext(operationContext.getSessionContext().getReadConcern()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import com.mongodb.MongoSecurityException;
import com.mongodb.MongoSocketException;
import com.mongodb.assertions.Assertions;
import com.mongodb.client.ClientSession;
import com.mongodb.client.FindIterable;
import com.mongodb.client.Fixture;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.TestListener;
import com.mongodb.event.CommandListener;
import com.mongodb.lang.Nullable;
Expand Down Expand Up @@ -334,12 +337,17 @@ public void test3p3UnexpectedErrorDoesNotClearCache() {

@Test
public void test4p1Reauthentication() {
testReauthentication(false);
}

private void testReauthentication(final boolean inSession) {
TestCallback callback = createCallback();
MongoClientSettings clientSettings = createSettings(callback);
try (MongoClient mongoClient = createMongoClient(clientSettings)) {
try (MongoClient mongoClient = createMongoClient(clientSettings);
ClientSession session = inSession ? mongoClient.startSession() : null) {
failCommand(391, 1, "find");
// #. Perform a find operation that succeeds.
performFind(mongoClient);
performFind(mongoClient, session);
}
assertEquals(2, callback.invocations.get());
}
Expand Down Expand Up @@ -392,6 +400,11 @@ private static void performInsert(final MongoClient mongoClient) {
.insertOne(Document.parse("{ x: 1 }"));
}

@Test
public void test4p5ReauthenticationInSession() {
testReauthentication(true);
}

@Test
public void test5p1AzureSucceedsWithNoUsername() {
assumeAzure();
Expand Down Expand Up @@ -914,12 +927,14 @@ private <T extends Throwable> void assertFindFails(
}
}

private void performFind(final MongoClient mongoClient) {
mongoClient
.getDatabase("test")
.getCollection("test")
.find()
.first();
private static void performFind(final MongoClient mongoClient) {
performFind(mongoClient, null);
}

private static void performFind(final MongoClient mongoClient, @Nullable final ClientSession session) {
MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("test");
FindIterable<Document> findIterable = session == null ? collection.find() : collection.find(session);
findIterable.first();
}

protected void delayNextFind() {
Expand Down