Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename result generators #1450

Merged
merged 2 commits into from
Oct 30, 2023
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 @@ -125,37 +125,36 @@ public static IIdentityServerBuilder AddDefaultEndpoints(this IIdentityServerBui
builder.AddEndpoint<TokenEndpoint>(EndpointNames.Token, ProtocolRoutePaths.Token.EnsureLeadingSlash());
builder.AddEndpoint<UserInfoEndpoint>(EndpointNames.UserInfo, ProtocolRoutePaths.UserInfo.EnsureLeadingSlash());

builder.AddEndpointResultGenerator<AuthorizeInteractionPageResult, AuthorizeInteractionPageResultGenerator>();
builder.AddEndpointResultGenerator<AuthorizeResult, AuthorizeResultGenerator>();
builder.AddEndpointResultGenerator<BackchannelAuthenticationResult, BackchannelAuthenticationResultGenerator>();
builder.AddEndpointResultGenerator<BadRequestResult, BadRequestResultGenerator>();
builder.AddEndpointResultGenerator<CheckSessionResult, CheckSessionResultGenerator>();
builder.AddEndpointResultGenerator<DeviceAuthorizationResult, DeviceAuthorizationResultGenerator>();
builder.AddEndpointResultGenerator<DiscoveryDocumentResult, DiscoveryDocumentResultGenerator>();
builder.AddEndpointResultGenerator<EndSessionCallbackResult, EndSessionCallbackResultGenerator>();
builder.AddEndpointResultGenerator<EndSessionResult, EndSessionResultGenerator>();
builder.AddEndpointResultGenerator<IntrospectionResult, IntrospectionResultGenerator>();
builder.AddEndpointResultGenerator<JsonWebKeysResult, JsonWebKeysResultGenerator>();
builder.AddEndpointResultGenerator<ProtectedResourceErrorResult, ProtectedResourceErrorResultGenerator>();
builder.AddEndpointResultGenerator<PushedAuthorizationResult, PushedAuthorizationResultGenerator>();
builder.AddEndpointResultGenerator<PushedAuthorizationErrorResult, PushedAuthorizationErrorResultGenerator>();
builder.AddEndpointResultGenerator<StatusCodeResult, StatusCodeResultGenerator>();
builder.AddEndpointResultGenerator<TokenErrorResult, TokenErrorResultGenerator>();
builder.AddEndpointResultGenerator<TokenResult, TokenResultGenerator>();
builder.AddEndpointResultGenerator<TokenRevocationErrorResult, TokenRevocationErrorResultGenerator>();
builder.AddEndpointResultGenerator<UserInfoResult, UserInfoResultGenerator>();
builder.AddHttpWriter<AuthorizeInteractionPageResult, AuthorizeInteractionPageHttpWriter>();
builder.AddHttpWriter<AuthorizeResult, AuthorizeHttpWriter>();
builder.AddHttpWriter<BackchannelAuthenticationResult, BackchannelAuthenticationHttpWriter>();
builder.AddHttpWriter<BadRequestResult, BadRequestHttpWriter>();
builder.AddHttpWriter<CheckSessionResult, CheckSessionHttpWriter>();
builder.AddHttpWriter<DeviceAuthorizationResult, DeviceAuthorizationHttpWriter>();
builder.AddHttpWriter<DiscoveryDocumentResult, DiscoveryDocumentHttpWriter>();
builder.AddHttpWriter<EndSessionCallbackResult, EndSessionCallbackHttpWriter>();
builder.AddHttpWriter<EndSessionResult, EndSessionHttpWriter>();
builder.AddHttpWriter<IntrospectionResult, IntrospectionHttpWriter>();
builder.AddHttpWriter<JsonWebKeysResult, JsonWebKeysHttpWriter>();
builder.AddHttpWriter<ProtectedResourceErrorResult, ProtectedResourceErrorHttpWriter>();
builder.AddHttpWriter<PushedAuthorizationResult, PushedAuthorizationHttpWriter>();
builder.AddHttpWriter<PushedAuthorizationErrorResult, PushedAuthorizationErrorHttpWriter>();
builder.AddHttpWriter<StatusCodeResult, StatusCodeHttpWriter>();
builder.AddHttpWriter<TokenErrorResult, TokenErrorHttpWriter>();
builder.AddHttpWriter<TokenResult, TokenHttpWriter>();
builder.AddHttpWriter<TokenRevocationErrorResult, TokenRevocationErrorHttpWriter>();
builder.AddHttpWriter<UserInfoResult, UserInfoHttpWriter>();

return builder;
}

/// <summary>
/// Adds the endpoint.
/// Adds an endpoint.
/// </summary>
/// <typeparam name="TEndpoint"></typeparam>
/// <param name="builder">The builder.</param>
/// <param name="name">The name.</param>
/// <param name="path">The path.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddEndpoint<TEndpoint>(this IIdentityServerBuilder builder, string name, PathString path)
where TEndpoint : class, IEndpointHandler
{
Expand All @@ -166,21 +165,20 @@ public static IIdentityServerBuilder AddEndpoint<TEndpoint>(this IIdentityServer
}

/// <summary>
/// Adds the endpoint.
/// Adds an <see cref="IHttpResponseWriter{T}"/> for an <see cref="IEndpointResult"/>.
/// </summary>
public static IIdentityServerBuilder AddEndpointResultGenerator<TResult, TResultGenerator>(this IIdentityServerBuilder builder)
public static IIdentityServerBuilder AddHttpWriter<TResult, TWriter>(this IIdentityServerBuilder builder)
where TResult : class, IEndpointResult
where TResultGenerator : class, Duende.IdentityServer.Hosting.IEndpointResultGenerator<TResult>
where TWriter : class, IHttpResponseWriter<TResult>
{
builder.Services.AddTransient<Duende.IdentityServer.Hosting.IEndpointResultGenerator<TResult>, TResultGenerator>();
builder.Services.AddTransient<IHttpResponseWriter<TResult>, TWriter>();
return builder;
}

/// <summary>
/// Adds the core services.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddCoreServices(this IIdentityServerBuilder builder)
{
builder.Services.AddTransient<IServerUrls, DefaultServerUrls>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public AuthorizeInteractionPageResult(ValidatedAuthorizeRequest request, string
public string ReturnUrlParameterName { get; }
}

class AuthorizeInteractionPageResultGenerator : IEndpointResultGenerator<AuthorizeInteractionPageResult>
class AuthorizeInteractionPageHttpWriter : IHttpResponseWriter<AuthorizeInteractionPageResult>
{
private readonly IServerUrls _urls;
private readonly IAuthorizationParametersMessageStore _authorizationParametersMessageStore;

/// <summary>
/// Initializes a new instance of the <see cref="AuthorizeInteractionPageResult"/> class.
/// </summary>
public AuthorizeInteractionPageResultGenerator(
public AuthorizeInteractionPageHttpWriter(
IServerUrls urls,
IAuthorizationParametersMessageStore authorizationParametersMessageStore = null)
{
Expand All @@ -70,7 +70,7 @@ public AuthorizeInteractionPageResultGenerator(
}

/// <inheritdoc/>
public async Task ExecuteAsync(AuthorizeInteractionPageResult result, HttpContext context)
public async Task WriteHttpResponse(AuthorizeInteractionPageResult result, HttpContext context)
{
var returnUrl = _urls.BasePath.EnsureTrailingSlash() + ProtocolRoutePaths.AuthorizeCallback;

Expand Down
42 changes: 34 additions & 8 deletions src/IdentityServer/Endpoints/Results/AuthorizeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ public AuthorizeResult(AuthorizeResponse response)
}
}

internal class AuthorizeResultGenerator : IEndpointResultGenerator<AuthorizeResult>
/// <summary>
/// Writes http responses for <see cref="AuthorizeResult"/>s.
/// </summary>
public class AuthorizeHttpWriter : IHttpResponseWriter<AuthorizeResult>
{
public AuthorizeResultGenerator(
/// <summary>
/// Initializes a new instance of the <see cref="AuthorizeHttpWriter"/> class.
/// </summary>
public AuthorizeHttpWriter(
IdentityServerOptions options,
IUserSession userSession,
IPushedAuthorizationService pushedAuthorizationService,
Expand All @@ -63,7 +69,8 @@ public AuthorizeResultGenerator(
private readonly IServerUrls _urls;
private readonly IClock _clock;

public async Task ExecuteAsync(AuthorizeResult result, HttpContext context)
/// <inheritdoc />
public async Task WriteHttpResponse(AuthorizeResult result, HttpContext context)
{
await ConsumePushedAuthorizationRequest(result);

Expand All @@ -86,7 +93,6 @@ private async Task ConsumePushedAuthorizationRequest(AuthorizeResult result)
}
}


private async Task ProcessErrorAsync(AuthorizeResponse response, HttpContext context)
{
// these are the conditions where we can send a response
Expand All @@ -111,7 +117,7 @@ private async Task ProcessErrorAsync(AuthorizeResponse response, HttpContext con
}
}

protected async Task ProcessResponseAsync(AuthorizeResponse response, HttpContext context)
private async Task ProcessResponseAsync(AuthorizeResponse response, HttpContext context)
{
if (!response.IsError)
{
Expand Down Expand Up @@ -178,11 +184,31 @@ private string BuildRedirectUri(AuthorizeResponse response)
return uri;
}

private const string FormPostHtml = "<html><head><meta http-equiv='X-UA-Compatible' content='IE=edge' /><base target='_self'/></head><body><form method='post' action='{uri}'>{body}<noscript><button>Click to continue</button></noscript></form><script>window.addEventListener('load', function(){document.forms[0].submit();});</script></body></html>";
private const string DefaultFormPostHeadTags = "<head><meta http-equiv='X-UA-Compatible' content='IE=edge' /><base target='_self'/></head>";
private const string DefaultFormPostBodyTags = "<body><form method='post' action='{uri}'>{body}<noscript><button>Click to continue</button></noscript></form><script>window.addEventListener('load', function(){document.forms[0].submit();});</script></body>";

private string GetFormPostHtml(AuthorizeResponse response)
/// <summary>
/// Gets the header tags that will be included in the response when
/// response_mode is form_post.
/// </summary>
protected virtual string FormPostHeader => DefaultFormPostHeadTags;

/// <summary>
/// Gets the body tags that will be included in the response when
/// response_mode is form_post. The string "{body}" (including the curly
/// braces) within this string will be replaced with the response
/// parameters, serialized as form data.
/// </summary>
protected virtual string FormPostBody => DefaultFormPostBodyTags;

/// <summary>
/// Gets the html that will set as the response when response_mode is
/// form_post.
/// </summary>
/// <param name="response"></param>
protected virtual string GetFormPostHtml(AuthorizeResponse response)
{
var html = FormPostHtml;
var html = $"<html>{FormPostHeader}{FormPostBody}</html>";

var url = response.Request.RedirectUri;
url = HtmlEncoder.Default.Encode(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public BackchannelAuthenticationResult(BackchannelAuthenticationResponse respons
}
}

internal class BackchannelAuthenticationResultGenerator : IEndpointResultGenerator<BackchannelAuthenticationResult>
internal class BackchannelAuthenticationHttpWriter : IHttpResponseWriter<BackchannelAuthenticationResult>
{
public async Task ExecuteAsync(BackchannelAuthenticationResult result, HttpContext context)
public async Task WriteHttpResponse(BackchannelAuthenticationResult result, HttpContext context)
{
context.Response.SetNoCache();

Expand Down
4 changes: 2 additions & 2 deletions src/IdentityServer/Endpoints/Results/BadRequestResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public BadRequestResult(string error = null, string errorDescription = null)
}
}

internal class BadRequestResultGenerator : IEndpointResultGenerator<BadRequestResult>
internal class BadRequestHttpWriter : IHttpResponseWriter<BadRequestResult>
{
public async Task ExecuteAsync(BadRequestResult result, HttpContext context)
public async Task WriteHttpResponse(BadRequestResult result, HttpContext context)
{
context.Response.StatusCode = 400;
context.Response.SetNoCache();
Expand Down
6 changes: 3 additions & 3 deletions src/IdentityServer/Endpoints/Results/CheckSessionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class CheckSessionResult : EndpointResult<CheckSessionResult>
}


internal class CheckSessionResultGenerator : IEndpointResultGenerator<CheckSessionResult>
internal class CheckSessionHttpWriter : IHttpResponseWriter<CheckSessionResult>
{
public CheckSessionResultGenerator(IdentityServerOptions options)
public CheckSessionHttpWriter(IdentityServerOptions options)
{
_options = options;
}
Expand All @@ -30,7 +30,7 @@ public CheckSessionResultGenerator(IdentityServerOptions options)
private static readonly object Lock = new object();
private static volatile string LastCheckSessionCookieName;

public async Task ExecuteAsync(CheckSessionResult result, HttpContext context)
public async Task WriteHttpResponse(CheckSessionResult result, HttpContext context)
{
AddCspHeaders(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public DeviceAuthorizationResult(DeviceAuthorizationResponse response)
}
}

internal class DeviceAuthorizationResultGenerator : IEndpointResultGenerator<DeviceAuthorizationResult>
internal class DeviceAuthorizationHttpWriter : IHttpResponseWriter<DeviceAuthorizationResult>
{
public async Task ExecuteAsync(DeviceAuthorizationResult result, HttpContext context)
public async Task WriteHttpResponse(DeviceAuthorizationResult result, HttpContext context)
{
context.Response.SetNoCache();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public DiscoveryDocumentResult(Dictionary<string, object> entries, int? maxAge =
}
}

class DiscoveryDocumentResultGenerator : IEndpointResultGenerator<DiscoveryDocumentResult>
class DiscoveryDocumentHttpWriter : IHttpResponseWriter<DiscoveryDocumentResult>
{
/// <inheritdoc/>
public Task ExecuteAsync(DiscoveryDocumentResult result, HttpContext context)
public Task WriteHttpResponse(DiscoveryDocumentResult result, HttpContext context)
{
if (result.MaxAge.HasValue && result.MaxAge.Value >= 0)
{
Expand Down
12 changes: 7 additions & 5 deletions src/IdentityServer/Endpoints/Results/EndPointResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace Duende.IdentityServer.Endpoints.Results;

/// <summary>
/// Provides the base implementation of IEndpointResult that invokes the corresponding IEndpointResultGenerator<typeparamref name="T"/>.
/// Provides the base implementation of <see cref="IEndpointResult"/> that
/// invokes the corresponding <see cref="IHttpResponseWriter{T}"/> to write the
/// result as an http response.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class EndpointResult<T> : IEndpointResult
Expand All @@ -20,16 +22,16 @@ public abstract class EndpointResult<T> : IEndpointResult
/// <inheritdoc/>
public async Task ExecuteAsync(HttpContext context)
{
var generator = context.RequestServices.GetService<IEndpointResultGenerator<T>>();
if (generator != null)
var writer = context.RequestServices.GetService<IHttpResponseWriter<T>>();
if (writer != null)
{
T target = this as T;
if (target == null)
{
throw new Exception($"Type paramter {typeof(T)} must be the class derived from 'EndPointResult<T>'.");
throw new Exception($"Type parameter {typeof(T)} must be the class derived from 'EndpointResult<T>'.");
}

await generator.ExecuteAsync(target, context);
await writer.WriteHttpResponse(target, context);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public EndSessionCallbackResult(EndSessionCallbackValidationResult result)
}
}

class EndSessionCallbackResultGenerator : IEndpointResultGenerator<EndSessionCallbackResult>
class EndSessionCallbackHttpWriter : IHttpResponseWriter<EndSessionCallbackResult>
{
public EndSessionCallbackResultGenerator(IdentityServerOptions options)
public EndSessionCallbackHttpWriter(IdentityServerOptions options)
{
_options = options;
}

private IdentityServerOptions _options;

public async Task ExecuteAsync(EndSessionCallbackResult result, HttpContext context)
public async Task WriteHttpResponse(EndSessionCallbackResult result, HttpContext context)
{
if (result.Result.IsError)
{
Expand Down
6 changes: 3 additions & 3 deletions src/IdentityServer/Endpoints/Results/EndSessionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public EndSessionResult(EndSessionValidationResult result)
}


class EndSessionResultGenerator : IEndpointResultGenerator<EndSessionResult>
class EndSessionHttpWriter : IHttpResponseWriter<EndSessionResult>
{
public EndSessionResultGenerator(
public EndSessionHttpWriter(
IdentityServerOptions options,
IClock clock,
IServerUrls urls,
Expand All @@ -57,7 +57,7 @@ public EndSessionResultGenerator(
private IServerUrls _urls;
private IMessageStore<LogoutMessage> _logoutMessageStore;

public async Task ExecuteAsync(EndSessionResult result, HttpContext context)
public async Task WriteHttpResponse(EndSessionResult result, HttpContext context)
{
var validatedRequest = result.Result.IsError ? null : result.Result.ValidatedRequest;

Expand Down
4 changes: 2 additions & 2 deletions src/IdentityServer/Endpoints/Results/IntrospectionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public IntrospectionResult(Dictionary<string, object> entries)
}


class IntrospectionResultGenerator : IEndpointResultGenerator<IntrospectionResult>
class IntrospectionHttpWriter : IHttpResponseWriter<IntrospectionResult>
{
public Task ExecuteAsync(IntrospectionResult result, HttpContext context)
public Task WriteHttpResponse(IntrospectionResult result, HttpContext context)
{
context.Response.SetNoCache();

Expand Down
4 changes: 2 additions & 2 deletions src/IdentityServer/Endpoints/Results/JsonWebKeysResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public JsonWebKeysResult(IEnumerable<JsonWebKey> webKeys, int? maxAge)
}
}

class JsonWebKeysResultGenerator : IEndpointResultGenerator<JsonWebKeysResult>
class JsonWebKeysHttpWriter : IHttpResponseWriter<JsonWebKeysResult>
{
public Task ExecuteAsync(JsonWebKeysResult result, HttpContext context)
public Task WriteHttpResponse(JsonWebKeysResult result, HttpContext context)
{
if (result.MaxAge.HasValue && result.MaxAge.Value >= 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public ProtectedResourceErrorResult(string error, string errorDescription = null
}
}

internal class ProtectedResourceErrorResultGenerator : IEndpointResultGenerator<ProtectedResourceErrorResult>
internal class ProtectedResourceErrorHttpWriter : IHttpResponseWriter<ProtectedResourceErrorResult>
{
public Task ExecuteAsync(ProtectedResourceErrorResult result, HttpContext context)
public Task WriteHttpResponse(ProtectedResourceErrorResult result, HttpContext context)
{
context.Response.StatusCode = 401;
context.Response.SetNoCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public PushedAuthorizationErrorResult(PushedAuthorizationFailure response)
}
}

internal class PushedAuthorizationErrorResultGenerator : IEndpointResultGenerator<PushedAuthorizationErrorResult>
internal class PushedAuthorizationErrorHttpWriter : IHttpResponseWriter<PushedAuthorizationErrorResult>
{
public async Task ExecuteAsync(PushedAuthorizationErrorResult result, HttpContext context)
public async Task WriteHttpResponse(PushedAuthorizationErrorResult result, HttpContext context)
{
context.Response.SetNoCache();
context.Response.StatusCode = (int) HttpStatusCode.BadRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public PushedAuthorizationResult(PushedAuthorizationSuccess response)
}
}

internal class PushedAuthorizationResultGenerator : IEndpointResultGenerator<PushedAuthorizationResult>
internal class PushedAuthorizationHttpWriter : IHttpResponseWriter<PushedAuthorizationResult>
{
public async Task ExecuteAsync(PushedAuthorizationResult result, HttpContext context)
public async Task WriteHttpResponse(PushedAuthorizationResult result, HttpContext context)
{
context.Response.SetNoCache();
context.Response.StatusCode = (int) HttpStatusCode.Created;
Expand Down
Loading