Skip to content

Commit

Permalink
Include binding type in XML created notifications.
Browse files Browse the repository at this point in the history
- It might make sense to make differences to the XML creation depending on binding
  • Loading branch information
AndersAbel committed Mar 26, 2020
1 parent 19856ce commit bd027d2
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Sustainsys.Saml2/Configuration/Saml2Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public Action<Saml2AuthenticationRequest, IdentityProvider, IDictionary<string,
AuthenticationRequestCreated
{ get; set; } = (request, provider, dictionary) => { };

public Action<Saml2AuthenticationRequest, XDocument>
public Action<Saml2AuthenticationRequest, XDocument, Saml2BindingType>
AuthenticationRequestXmlCreated
{ get; set; } = (request, xDocument) => { };
{ get; set; } = (request, xDocument, Saml2BindingType) => { };

/// <summary>
/// Notification called when the SignIn command has produced a
Expand Down Expand Up @@ -138,15 +138,15 @@ public Func<Saml2LogoutResponse, StoredRequestState, bool>
/// <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) => { };
public Action<Saml2LogoutRequest, XDocument, Saml2BindingType> LogoutRequestXmlCreated { get; set; } = (lr, xd, bt) => { };

/// <summary>
/// Notification called when a logout request has been received and processed and a Logout Response has been created.
/// </summary>
public Action<Saml2LogoutResponse, Saml2LogoutRequest, ClaimsPrincipal, IdentityProvider> LogoutResponseCreated { get; set; }
= (resp, req, u, idp) => { };

public Action<Saml2LogoutResponse, XDocument> LogoutResponseXmlCreated { get; set; } = (lr, xd) => { };
public Action<Saml2LogoutResponse, XDocument, Saml2BindingType> LogoutResponseXmlCreated { get; set; } = (lr, xd, bt) => { };

/// <summary>
/// Notification called when metadata has been created, but before
Expand Down
2 changes: 1 addition & 1 deletion Sustainsys.Saml2/IdentityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public CommandResult Bind(ISaml2Message request)
/// <param name="xmlCreatedNotification">Notification to call with Xml structure</param>
/// <returns>CommandResult with the bound message.</returns>
public CommandResult Bind<TMessage>(
TMessage message, Action<TMessage, XDocument> xmlCreatedNotification)
TMessage message, Action<TMessage, XDocument, Saml2BindingType> xmlCreatedNotification)
where TMessage: ISaml2Message
{
return Saml2Binding.Get(Binding).Bind(message, spOptions.Logger, xmlCreatedNotification);
Expand Down
6 changes: 4 additions & 2 deletions Sustainsys.Saml2/SAML2P/ISaml2MessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sustainsys.Saml2.Internal;
using Sustainsys.Saml2.WebSso;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -14,12 +15,13 @@ static class Saml2MessageExtensions
/// <param name="message">Saml2 message to transform to XML</param>
/// <param name="xmlCreatedNotification">Notification allowing modification of XML tree before serialization.</param>
/// <returns>string containing the Xml data.</returns>
public static string ToXml<TMessage>(this TMessage message, Action<TMessage, XDocument> xmlCreatedNotification)
public static string ToXml<TMessage>(
this TMessage message, Action<XDocument> xmlCreatedNotification)
where TMessage : ISaml2Message
{
var xDocument = new XDocument(message.ToXElement());

xmlCreatedNotification?.Invoke(message, xDocument);
xmlCreatedNotification(xDocument);

return xDocument.ToStringWithXmlDeclaration();
}
Expand Down
2 changes: 1 addition & 1 deletion Sustainsys.Saml2/WebSSO/Saml2Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public virtual CommandResult Bind(ISaml2Message message, ILoggerAdapter logger)
/// <param name="xmlCreatedNotification">Notification to call for modification of XDocument, can be null.</param>
/// <returns>CommandResult.</returns>
public virtual CommandResult Bind<TMessage>(
TMessage message, ILoggerAdapter logger, Action<TMessage, XDocument> xmlCreatedNotification)
TMessage message, ILoggerAdapter logger, Action<TMessage, XDocument, Saml2BindingType> xmlCreatedNotification)
where TMessage : ISaml2Message
{
throw new NotImplementedException();
Expand Down
2 changes: 1 addition & 1 deletion Sustainsys.Saml2/WebSSO/Saml2PostBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public override CommandResult Bind(ISaml2Message message, ILoggerAdapter logger)
}

public override CommandResult Bind<TMessage>(
TMessage message, ILoggerAdapter logger, Action<TMessage, XDocument> xmlCreatedNotification)
TMessage message, ILoggerAdapter logger, Action<TMessage, XDocument, Saml2BindingType> xmlCreatedNotification)
{
if(message == null)
{
Expand Down
4 changes: 2 additions & 2 deletions Sustainsys.Saml2/WebSSO/Saml2RedirectBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public override CommandResult Bind(ISaml2Message message, ILoggerAdapter logger)
}

public override CommandResult Bind<TMessage>(
TMessage message, ILoggerAdapter logger, Action<TMessage, XDocument> xmlCreatedNotification)
TMessage message, ILoggerAdapter logger, Action<TMessage, XDocument, Saml2BindingType> xmlCreatedNotification)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}

var messageXml = message.ToXml(xmlCreatedNotification);
var messageXml = message.ToXml(xd => xmlCreatedNotification?.Invoke(message, xd, Saml2BindingType.HttpRedirect));
logger?.WriteVerbose("Sending message over Http Redirect Binding\n" + messageXml);

var serializedRequest = Serialize(messageXml);
Expand Down
6 changes: 4 additions & 2 deletions Tests/Tests.Shared/WebSSO/LogoutCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ public void LogoutCommand_Run_ReturnsLogoutRequest()
};

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

var actual = CommandFactory.GetCommand(CommandFactory.LogoutCommandName)
Expand Down Expand Up @@ -480,11 +481,12 @@ public void LogoutCommand_Run_HandlesLogoutRequest_ReceivedThroughRedirectBindin
};

bool xmlCreatedCalled = false;
options.Notifications.LogoutResponseXmlCreated = (resp, xml) =>
options.Notifications.LogoutResponseXmlCreated = (resp, xml, bt) =>
{
xmlCreatedCalled = true;
resp.Should().BeSameAs(logoutResponse);
xml.Root.Attribute("ID").Value.Should().BeSameAs(resp.Id.Value);
bt.Should().Be(Saml2BindingType.HttpRedirect);
};

CommandResult notifiedCommandResult = null;
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests.Shared/WebSSO/Saml2RedirectBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void Saml2RedirectBinding_Bind_WithXmlDeclaration()
MessageName = "SAMLRequest"
};

var result = Saml2Binding.Get(Saml2BindingType.HttpRedirect).Bind(message, null, (m, x) =>
var result = Saml2Binding.Get(Saml2BindingType.HttpRedirect).Bind(message, null, (m, x, t) =>
{
x.Declaration = new XDeclaration("42.17", "utf-73", null);
});
Expand Down
5 changes: 3 additions & 2 deletions Tests/Tests.Shared/WebSSO/SignInCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ public void SignInCommand_Run_Calls_Notifications()
};

bool authenticationRequestXmlCreatedCalled = false;
options.Notifications.AuthenticationRequestXmlCreated = (ar, xd) =>
options.Notifications.AuthenticationRequestXmlCreated = (ar, xd, bt) =>
{
ar.Should().BeSameAs(saml2AuthenticationRequest);
authenticationRequestXmlCreatedCalled = true;
ar.Should().BeSameAs(saml2AuthenticationRequest);
bt.Should().Be(Saml2BindingType.HttpRedirect);
};

SignInCommand.Run(idp.EntityId, null, request, options, relayData)
Expand Down

0 comments on commit bd027d2

Please sign in to comment.