Browse Source

Update HandleLogoutRequestContext to allow attaching custom sign-out parameters

pull/1325/head
Kévin Chalet 4 years ago
parent
commit
cda55862bc
  1. 17
      src/OpenIddict.Server/OpenIddictServerEvents.Session.cs
  2. 5
      src/OpenIddict.Server/OpenIddictServerEvents.cs
  3. 8
      src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs
  4. 46
      src/OpenIddict.Server/OpenIddictServerHandlers.cs
  5. 37
      test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTests.Session.cs

17
src/OpenIddict.Server/OpenIddictServerEvents.Session.cs

@ -5,6 +5,7 @@
*/
using System;
using System.Collections.Generic;
using OpenIddict.Abstractions;
using SR = OpenIddict.Abstractions.OpenIddictResources;
@ -115,10 +116,26 @@ namespace OpenIddict.Server
/// </summary>
public bool IsSignOutTriggered { get; private set; }
/// <summary>
/// Gets the additional parameters returned to the client application.
/// </summary>
public Dictionary<string, OpenIddictParameter> Parameters { get; private set; }
= new(StringComparer.Ordinal);
/// <summary>
/// Allows OpenIddict to return a sign-out response.
/// </summary>
public void SignOut() => IsSignOutTriggered = true;
/// <summary>
/// Allows OpenIddict to return a sign-out response.
/// </summary>
/// <param name="parameters">The additional parameters returned to the client application.</param>
public void SignOut(IDictionary<string, OpenIddictParameter> parameters)
{
IsSignOutTriggered = true;
Parameters = new(parameters, StringComparer.Ordinal);
}
}
/// <summary>

5
src/OpenIddict.Server/OpenIddictServerEvents.cs

@ -733,6 +733,11 @@ namespace OpenIddict.Server
get => Transaction.Response!;
set => Transaction.Response = value;
}
/// <summary>
/// Gets the additional parameters returned to the client application.
/// </summary>
public Dictionary<string, OpenIddictParameter> Parameters { get; } = new(StringComparer.Ordinal);
}
}
}

8
src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs

@ -227,6 +227,14 @@ namespace OpenIddict.Server
Response = new OpenIddictResponse()
};
if (notification.Parameters.Count > 0)
{
foreach (var parameter in notification.Parameters)
{
@event.Parameters.Add(parameter.Key, parameter.Value);
}
}
await _dispatcher.DispatchAsync(@event);
if (@event.IsRequestHandled)

46
src/OpenIddict.Server/OpenIddictServerHandlers.cs

@ -82,12 +82,13 @@ namespace OpenIddict.Server
GenerateUserCode.Descriptor,
GenerateIdentityToken.Descriptor,
AttachTokenParameters.Descriptor,
AttachSignInParameters.Descriptor,
/*
* Sign-out processing:
*/
ValidateSignOutDemand.Descriptor)
ValidateSignOutDemand.Descriptor,
AttachSignOutParameters.Descriptor)
.AddRange(Authentication.DefaultHandlers)
.AddRange(Device.DefaultHandlers)
@ -2832,16 +2833,16 @@ namespace OpenIddict.Server
}
/// <summary>
/// Contains the logic responsible of attaching the tokens and their metadata to the sign-in response.
/// Contains the logic responsible of attaching the appropriate parameters to the sign-in response.
/// </summary>
public class AttachTokenParameters : IOpenIddictServerHandler<ProcessSignInContext>
public class AttachSignInParameters : IOpenIddictServerHandler<ProcessSignInContext>
{
/// <summary>
/// Gets the default descriptor definition assigned to this handler.
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessSignInContext>()
.UseSingletonHandler<AttachTokenParameters>()
.UseSingletonHandler<AttachSignInParameters>()
.SetOrder(GenerateIdentityToken.Descriptor.Order + 1_000)
.SetType(OpenIddictServerHandlerType.BuiltIn)
.Build();
@ -3009,5 +3010,40 @@ namespace OpenIddict.Server
return default;
}
}
/// <summary>
/// Contains the logic responsible of attaching the appropriate parameters to the sign-out response.
/// </summary>
public class AttachSignOutParameters : IOpenIddictServerHandler<ProcessSignOutContext>
{
/// <summary>
/// Gets the default descriptor definition assigned to this handler.
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessSignOutContext>()
.UseSingletonHandler<AttachSignOutParameters>()
.SetOrder(ValidateSignOutDemand.Descriptor.Order + 1_000)
.SetType(OpenIddictServerHandlerType.BuiltIn)
.Build();
/// <inheritdoc/>
public ValueTask HandleAsync(ProcessSignOutContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Parameters.Count > 0)
{
foreach (var parameter in context.Parameters)
{
context.Response.SetParameter(parameter.Key, parameter.Value);
}
}
return default;
}
}
}
}

37
test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTests.Session.cs

@ -478,6 +478,43 @@ namespace OpenIddict.Server.IntegrationTests
Assert.Equal("Bob le Magnifique", (string?) response["name"]);
}
[Fact]
public async Task HandleLogoutResponse_ResponseContainsCustomParameters()
{
// Arrange
await using var server = await CreateServerAsync(options =>
{
options.EnableDegradedMode();
options.AddEventHandler<HandleLogoutRequestContext>(builder =>
builder.UseInlineHandler(context =>
{
context.SignOut();
context.Parameters["custom_parameter"] = "custom_value";
context.Parameters["parameter_with_multiple_values"] = new[]
{
"custom_value_1",
"custom_value_2"
};
return default;
}));
});
await using var client = await server.CreateClientAsync();
// Act
var response = await client.PostAsync("/connect/logout", new OpenIddictRequest
{
PostLogoutRedirectUri = "http://www.fabrikam.com/path"
});
// Assert
Assert.Equal("custom_value", (string?) response["custom_parameter"]);
Assert.Equal(new[] { "custom_value_1", "custom_value_2" }, (string[]?) response["parameter_with_multiple_values"]);
}
[Fact]
public async Task ApplyLogoutResponse_AllowsHandlingResponse()
{

Loading…
Cancel
Save