Skip to content
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 @@ -54,14 +54,14 @@ public async Task RemoveSessionsAsync(RemoveSessionsContext context, Cancellatio

if (!sessionClientList.IsNullOrEmpty() && !context.ClientIds.IsNullOrEmpty())
{
clientIds = sessionClientList!.Where(x => context.ClientIds!.Contains(x)).ToArray();
clientIds = [..sessionClientList!.Where(x => context.ClientIds!.Contains(x))];
}

await backChannelLogoutService.SendLogoutNotificationsAsync(new LogoutNotificationContext
{
SubjectId = session.Session.SubjectId,
SessionId = session.Session.SessionId,
ClientIds = clientIds,
ClientIds = context.ClientIds.IsNullOrEmpty() ? sessionClientList : clientIds,
});
}
}
Expand All @@ -85,7 +85,7 @@ await persistedGrantStore.RemoveAllAsync(new PersistedGrantFilter
SubjectId = context.SubjectId,
SessionId = context.SessionId,
ClientIds = context.ClientIds?.ToArray() ?? [],
Types = typeFilter.ToArray(),
Types = [..typeFilter],
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public async Task RemoveSessionsAsync_WhenFilterSpecified_ShouldRemoveAllSession
List<AuthenticationTicketFilterResult> fakeSessions =
[
ServerSessionTestGenerators.GenerateAuthenticationTicketFilterResult("key2", "SchemeA", "alice", "session-0002", "Alice"),
ServerSessionTestGenerators.GenerateAuthenticationTicketFilterResult("key4", "SchemeB", "alice", "session-0002", "Alice"),
ServerSessionTestGenerators.GenerateAuthenticationTicketFilterResult("key4", "SchemeB", "alice", "session-0002", "Alice", clientIds: ["fake-client-a", "fake-client-b"]),
ServerSessionTestGenerators.GenerateAuthenticationTicketFilterResult("key7", "SchemeC", "alice", "session-0002", "Alice"),
];

Expand All @@ -150,20 +150,56 @@ public async Task RemoveSessionsAsync_WhenFilterSpecified_ShouldRemoveAllSession
ctx.SessionId == fakeSession.Session.SessionId &&
ctx.SubjectId == fakeSession.Session.SubjectId &&
fakeSession.AuthTicket != null &&
ctx.ClientIds == fakeSession.AuthTicket.Properties.GetClientList())));
ctx.ClientIds.SequenceEqual(fakeSession.AuthTicket.Properties.GetClientList()))));
}

Mock.Get(persistedGrantStore)
.Verify(x => x.RemoveAllAsync(It.Is<PersistedGrantFilter>(f =>
f.SessionId == fakeContext.SessionId &&
f.SubjectId == fakeContext.SubjectId &&
f.Types.AsEnumerable().ToHashSet().SetEquals(AllGrantTypes) &&
f.Types.AsEnumerable().SequenceEqual(AllGrantTypes) &&
f.ClientIds == (fakeContext.ClientIds ?? Array.Empty<string>()))));

Mock.Get(serverSessionStore)
.Verify(x => x.DeleteSessions(fakeContext.SubjectId, fakeContext.SessionId));
}

[Fact]
public async Task RemoveSessionsAsync_WhenClientIdsNotProvided_ShouldOnlyTriggerBackchannelNotificationsAndRevocationsForAllClients()
{
string[] fakeClientIds = ["client-a", "client-b", "client-c", "client-d"];
var fakeSession = ServerSessionTestGenerators.GenerateAuthenticationTicketFilterResult("key2", "SchemeA", "alice", "session-0002", "Alice", clientIds: fakeClientIds);
List<AuthenticationTicketFilterResult> fakeSessions = [fakeSession];

Mock.Get(serverSessionTicketStore)
.Setup(x => x.FilterServerAuthenticationTickets("alice", null))
.ReturnsAsync(fakeSessions);

RemoveSessionsContext fakeContext = new RemoveSessionsContext
{
SubjectId = "alice", SessionId = null,
};
DefaultSessionManagementService sut = CreateSut();

await sut.RemoveSessionsAsync(fakeContext, TestContext.Current.CancellationToken);

Mock.Get(backChannelLogoutService)
.Verify(x => x.SendLogoutNotificationsAsync(It.Is<LogoutNotificationContext>(ctx =>
ctx.SessionId == fakeSession.Session.SessionId &&
ctx.SubjectId == fakeSession.Session.SubjectId &&
ctx.ClientIds.SequenceEqual(fakeClientIds))));

Mock.Get(persistedGrantStore)
.Verify(x => x.RemoveAllAsync(It.Is<PersistedGrantFilter>(f =>
f.SessionId == fakeContext.SessionId &&
f.SubjectId == fakeContext.SubjectId &&
f.Types.AsEnumerable().SequenceEqual(AllGrantTypes) &&
f.ClientIds.SequenceEqual(fakeContext.ClientIds ?? Array.Empty<string>()))));

Mock.Get(serverSessionStore)
.Verify(x => x.DeleteSessions(fakeContext.SubjectId, fakeContext.SessionId));
}

[Fact]
public async Task RemoveSessionsAsync_WhenClientIdsProvided_ShouldOnlyTriggerBackchannelNotificationsAndRevocationsForThoseClients()
{
Expand All @@ -188,14 +224,14 @@ public async Task RemoveSessionsAsync_WhenClientIdsProvided_ShouldOnlyTriggerBac
.Verify(x => x.SendLogoutNotificationsAsync(It.Is<LogoutNotificationContext>(ctx =>
ctx.SessionId == fakeSession.Session.SessionId &&
ctx.SubjectId == fakeSession.Session.SubjectId &&
ctx.ClientIds.ToHashSet().SetEquals(expectedClientIds))));
ctx.ClientIds.SequenceEqual(expectedClientIds))));

Mock.Get(persistedGrantStore)
.Verify(x => x.RemoveAllAsync(It.Is<PersistedGrantFilter>(f =>
f.SessionId == fakeContext.SessionId &&
f.SubjectId == fakeContext.SubjectId &&
f.Types.AsEnumerable().ToHashSet().SetEquals(AllGrantTypes) &&
f.ClientIds.ToHashSet().SetEquals(fakeContext.ClientIds ?? Array.Empty<string>()))));
f.Types.AsEnumerable().SequenceEqual(AllGrantTypes) &&
f.ClientIds.SequenceEqual(fakeContext.ClientIds ?? Array.Empty<string>()))));

Mock.Get(serverSessionStore)
.Verify(x => x.DeleteSessions(fakeContext.SubjectId, fakeContext.SessionId));
Expand Down Expand Up @@ -231,15 +267,15 @@ public async Task RemoveSessionsAsync_WhenRemoveSessionsSetToFalse_ShouldNotRemo
ctx.SessionId == fakeSession.Session.SessionId &&
ctx.SubjectId == fakeSession.Session.SubjectId &&
fakeSession.AuthTicket != null &&
ctx.ClientIds.ToHashSet().SetEquals(fakeSession.AuthTicket.Properties.GetClientList()))));
ctx.ClientIds.SequenceEqual(fakeSession.AuthTicket.Properties.GetClientList()))));
}

Mock.Get(persistedGrantStore)
.Verify(x => x.RemoveAllAsync(It.Is<PersistedGrantFilter>(f =>
f.SessionId == fakeContext.SessionId &&
f.SubjectId == fakeContext.SubjectId &&
f.Types.AsEnumerable().ToHashSet().SetEquals(AllGrantTypes) &&
f.ClientIds.ToHashSet().SetEquals(fakeContext.ClientIds ?? Array.Empty<string>()))));
f.Types.AsEnumerable().SequenceEqual(AllGrantTypes) &&
f.ClientIds.SequenceEqual(fakeContext.ClientIds ?? Array.Empty<string>()))));

Mock.Get(serverSessionStore)
.Verify(x => x.DeleteSessions(fakeContext.SubjectId, fakeContext.SessionId), Times.Never);
Expand Down Expand Up @@ -275,8 +311,8 @@ public async Task RemoveSessionsAsync_WhenSendBackchannelFalse_ShouldNotSendBack
.Verify(x => x.RemoveAllAsync(It.Is<PersistedGrantFilter>(f =>
f.SessionId == fakeContext.SessionId &&
f.SubjectId == fakeContext.SubjectId &&
f.Types.AsEnumerable().ToHashSet().SetEquals(AllGrantTypes) &&
f.ClientIds.ToHashSet().SetEquals(fakeContext.ClientIds ?? Array.Empty<string>()))));
f.Types.AsEnumerable().SequenceEqual(AllGrantTypes) &&
f.ClientIds.SequenceEqual(fakeContext.ClientIds ?? Array.Empty<string>()))));

Mock.Get(serverSessionStore)
.Verify(x => x.DeleteSessions(fakeContext.SubjectId, fakeContext.SessionId));
Expand Down Expand Up @@ -312,15 +348,15 @@ public async Task RemoveSessionsAsync_WhenRevokeTokensFalse_ShouldNotRevokeToken
ctx.SessionId == fakeSession.Session.SessionId &&
ctx.SubjectId == fakeSession.Session.SubjectId &&
fakeSession.AuthTicket != null &&
ctx.ClientIds.ToHashSet().SetEquals(fakeSession.AuthTicket.Properties.GetClientList()))));
ctx.ClientIds.SequenceEqual(fakeSession.AuthTicket.Properties.GetClientList()))));
}

Mock.Get(persistedGrantStore)
.Verify(x => x.RemoveAllAsync(It.Is<PersistedGrantFilter>(f =>
f.SessionId == fakeContext.SessionId &&
f.SubjectId == fakeContext.SubjectId &&
f.Types.AsEnumerable().ToHashSet().SetEquals(ConsentGrantTypes) &&
f.ClientIds.ToHashSet().SetEquals(fakeContext.ClientIds ?? Array.Empty<string>()))));
f.Types.AsEnumerable().SequenceEqual(ConsentGrantTypes) &&
f.ClientIds.SequenceEqual(fakeContext.ClientIds ?? Array.Empty<string>()))));

Mock.Get(serverSessionStore)
.Verify(x => x.DeleteSessions(fakeContext.SubjectId, fakeContext.SessionId));
Expand Down Expand Up @@ -356,15 +392,15 @@ public async Task RemoveSessionsAsync_WhenRevokeConsentsFalse_ShouldNotRevokeCon
ctx.SessionId == fakeSession.Session.SessionId &&
ctx.SubjectId == fakeSession.Session.SubjectId &&
fakeSession.AuthTicket != null &&
ctx.ClientIds.ToHashSet().SetEquals(fakeSession.AuthTicket.Properties.GetClientList()))));
ctx.ClientIds.SequenceEqual(fakeSession.AuthTicket.Properties.GetClientList()))));
}

Mock.Get(persistedGrantStore)
.Verify(x => x.RemoveAllAsync(It.Is<PersistedGrantFilter>(f =>
f.SessionId == fakeContext.SessionId &&
f.SubjectId == fakeContext.SubjectId &&
f.Types.AsEnumerable().ToHashSet().SetEquals(TokenGrantTypes) &&
f.ClientIds.ToHashSet().SetEquals(fakeContext.ClientIds ?? Array.Empty<string>()))));
f.Types.AsEnumerable().SequenceEqual(TokenGrantTypes) &&
f.ClientIds.SequenceEqual(fakeContext.ClientIds ?? Array.Empty<string>()))));

Mock.Get(serverSessionStore)
.Verify(x => x.DeleteSessions(fakeContext.SubjectId, fakeContext.SessionId));
Expand Down Expand Up @@ -400,7 +436,7 @@ public async Task RemoveSessionsAsync_WhenRevokeTokensAndConsentsFalse_ShouldNot
ctx.SessionId == fakeSession.Session.SessionId &&
ctx.SubjectId == fakeSession.Session.SubjectId &&
fakeSession.AuthTicket != null &&
ctx.ClientIds.ToHashSet().SetEquals(fakeSession.AuthTicket.Properties.GetClientList()))));
ctx.ClientIds.SequenceEqual(fakeSession.AuthTicket.Properties.GetClientList()))));
}

Mock.Get(persistedGrantStore)
Expand Down
Loading