Skip to content

Commit

Permalink
Notifications when initiating logout
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersAbel committed Mar 26, 2020
1 parent 8ad7586 commit 1a2b976
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Sustainsys.Saml2/Configuration/Saml2Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ public Func<Saml2LogoutResponse, StoredRequestState, bool>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Logout")]
public Action<CommandResult> LogoutCommandResultCreated { get; set; } = cr => { };

/// <summary>
/// Notification called when a logout request is created to initiate single log
/// out with an identity provider.
/// </summary>
public Action<Saml2LogoutRequest, ClaimsPrincipal, IdentityProvider> LogoutRequestCreated { get; set; } = (lr, user, idp) => { };

/// <summary>
/// Notification called when a logout request has been transformed to an XML node tree.
/// </summary>
public Action<Saml2LogoutRequest, XDocument> LogoutRequestXmlCreated { get; set; } = (lr, xd) => { };

/// <summary>
/// Notification called when metadata has been created, but before
/// signing. At this point the contents of the metadata can be
Expand Down
2 changes: 1 addition & 1 deletion Sustainsys.Saml2/IdentityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public CommandResult Bind<TMessage>(
TMessage message, Action<TMessage, XDocument> xmlCreatedNotification)
where TMessage: ISaml2Message
{
return Saml2Binding.Get(Binding).Bind(message, null, xmlCreatedNotification);
return Saml2Binding.Get(Binding).Bind(message, spOptions.Logger, xmlCreatedNotification);
}

private readonly ConfiguredAndLoadedSigningKeysCollection signingKeys =
Expand Down
4 changes: 3 additions & 1 deletion Sustainsys.Saml2/WebSSO/LogOutCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ public static CommandResult InitiateLogout(HttpRequestData request, Uri returnUr
{
var logoutRequest = idp.CreateLogoutRequest(request.User);

options.Notifications.LogoutRequestCreated(logoutRequest, request.User, idp);

commandResult = Saml2Binding.Get(idp.SingleLogoutServiceBinding)
.Bind(logoutRequest);
.Bind(logoutRequest, options.SPOptions.Logger, options.Notifications.LogoutRequestXmlCreated);

commandResult.RelayState = logoutRequest.RelayState;
commandResult.RequestState = new StoredRequestState(
Expand Down
17 changes: 17 additions & 0 deletions Tests/Tests.Shared/WebSSO/LogoutCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,27 @@ public void LogoutCommand_Run_ReturnsLogoutRequest()
notifiedCommandResult = cr;
};

Saml2LogoutRequest logoutRequest = null;
options.Notifications.LogoutRequestCreated = (lr, u, idp) =>
{
logoutRequest = lr;
u.Identities.Single().FindFirst(Saml2ClaimTypes.SessionIndex).Value.Should().Be("SessionId");
idp.EntityId.Id.Should().Be("https://idp.example.com");
};

var logoutRequestXmlCreatedCalled = false;
options.Notifications.LogoutRequestXmlCreated = (lr, xd) =>
{
logoutRequestXmlCreatedCalled = true;
xd.Root.Attribute("ID").Value.Should().Be(lr.Id.Value);
};

var actual = CommandFactory.GetCommand(CommandFactory.LogoutCommandName)
.Run(request, options);

actual.Should().BeSameAs(notifiedCommandResult);
logoutRequest.Should().NotBeNull();
logoutRequestXmlCreatedCalled.Should().BeTrue();

var expected = new CommandResult
{
Expand Down

0 comments on commit 1a2b976

Please sign in to comment.