Skip to content

Commit

Permalink
Save AuthenticationProperties when issuing AuthnRequest.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersAbel committed Jan 15, 2015
1 parent 2d39ac1 commit 7b34c7c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ protected override async Task ApplyResponseChallengeAsync()
idp,
challenge.Properties.RedirectUri,
await Context.ToHttpRequestData(),
Options);
Options,
challenge.Properties);

result.Apply(Context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,34 @@ public async Task KentorAuthServicesAuthenticationMiddleware_RedirectRemembersRe
storedAuthnData.ReturnUrl.Should().Be(returnUrl);
}

[TestMethod]
public async Task KentorAuthServicesAuthenicationMiddleware_StoresAuthenticationProperties()
{
var returnUrl = "http://sp.example.com/returnurl";

var prop = new AuthenticationProperties()
{
RedirectUri = returnUrl
};
prop.Dictionary["test"] = "SomeValue";

var middleware = new KentorAuthServicesAuthenticationMiddleware(
new StubOwinMiddleware(401, new AuthenticationResponseChallenge(
new string[] {"KentorAuthServices"}, prop)),
CreateAppBuilder(), new KentorAuthServicesAuthenticationOptions(true));

var context = OwinTestHelpers.CreateOwinContext();

await middleware.Invoke(context);

var requestId = AuthnRequestHelper.GetRequestId(new Uri(context.Response.Headers["Location"]));

StoredRequestState storedAuthnData;
PendingAuthnRequests.TryRemove(new Saml2Id(requestId), out storedAuthnData);

((AuthenticationProperties)storedAuthnData.Data).Dictionary["test"].Should().Be("SomeValue");
}

[NotReRunnable]
[TestMethod]
public async Task KentorAuthServicesAuthenticationMiddleware_AcsWorks()
Expand Down
21 changes: 19 additions & 2 deletions Kentor.AuthServices/IdentityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,27 @@ public Uri MetadataUrl
/// successful authentication.</param>
/// <param name="authServicesUrls">Urls for AuthServices, used to populate fields
/// in the created AuthnRequest</param>
/// <returns></returns>
/// <returns>AuthnRequest</returns>
public Saml2AuthenticationRequest CreateAuthenticateRequest(
Uri returnUrl,
AuthServicesUrls authServicesUrls)
{
return CreateAuthenticateRequest(returnUrl, authServicesUrls, null);
}

/// <summary>
/// Create an authenticate request aimed for this idp.
/// </summary>
/// <param name="returnUrl">The return url where the browser should be sent after
/// successful authentication.</param>
/// <param name="authServicesUrls">Urls for AuthServices, used to populate fields
/// in the created AuthnRequest</param>
/// <param name="relayData">Aux data that should be preserved across the authentication</param>
/// <returns>AuthnRequest</returns>
public Saml2AuthenticationRequest CreateAuthenticateRequest(
Uri returnUrl,
AuthServicesUrls authServicesUrls,
object relayData)
{
if (authServicesUrls == null)
{
Expand All @@ -208,7 +225,7 @@ public Saml2AuthenticationRequest CreateAuthenticateRequest(
AttributeConsumingServiceIndex = spOptions.AttributeConsumingServices.Any() ? 0 : (int?)null
};

var responseData = new StoredRequestState(EntityId, returnUrl);
var responseData = new StoredRequestState(EntityId, returnUrl, relayData);

PendingAuthnRequests.Add(new Saml2Id(authnRequest.Id), responseData);

Expand Down
19 changes: 19 additions & 0 deletions Kentor.AuthServices/StoredRequestState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@ public class StoredRequestState
/// </summary>
/// <param name="idp">The EntityId of the IDP the request was sent to</param>
/// <param name="returnUrl">The Url to redirect back to after a succesful login</param>
/// <param name="data">Aux data that can be stored across the authentication request.</param>
public StoredRequestState(EntityId idp, Uri returnUrl)
{
idp = idp;
ReturnUrl = returnUrl;
}

/// <summary>
/// Creates a PendingAuthnRequestData
/// </summary>
/// <param name="idp">The EntityId of the IDP the request was sent to</param>
/// <param name="returnUrl">The Url to redirect back to after a succesful login</param>
/// <param name="data">Aux data that can be stored across the authentication request.</param>
public StoredRequestState(EntityId idp, Uri returnUrl, object data)
{
Idp = idp;
ReturnUrl = returnUrl;
Data = data;
}

/// <summary>
Expand All @@ -33,5 +47,10 @@ public StoredRequestState(EntityId idp, Uri returnUrl)
/// The Url to redirect back to after a succesful login
/// </summary>
public Uri ReturnUrl { get; private set; }

/// <summary>
/// Aux data that need to be preserved across the authentication call.
/// </summary>
public object Data { get; private set; }
}
}
5 changes: 3 additions & 2 deletions Kentor.AuthServices/WebSSO/SignInCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static CommandResult CreateResult(
EntityId idpEntityId,
string returnPath,
HttpRequestData request,
IOptions options)
IOptions options,
object relayData = null)
{
var urls = new AuthServicesUrls(request, options.SPOptions);

Expand Down Expand Up @@ -65,7 +66,7 @@ public static CommandResult CreateResult(
Uri.TryCreate(request.Url, returnPath, out returnUrl);
}

var authnRequest = idp.CreateAuthenticateRequest(returnUrl, urls);
var authnRequest = idp.CreateAuthenticateRequest(returnUrl, urls, relayData);

return idp.Bind(authnRequest);
}
Expand Down

0 comments on commit 7b34c7c

Please sign in to comment.