/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System.ComponentModel;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace OpenIddict.Validation;
public static partial class OpenIddictValidationEvents
{
///
/// Represents an abstract base class used for certain event contexts.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class BaseContext
{
///
/// Creates a new instance of the class.
///
protected BaseContext(OpenIddictValidationTransaction transaction)
=> Transaction = transaction ?? throw new ArgumentNullException(nameof(transaction));
///
/// Gets the environment associated with the current request being processed.
///
public OpenIddictValidationTransaction Transaction { get; }
///
/// Gets the cancellation token used to determine if the operation was aborted.
///
public CancellationToken CancellationToken => Transaction.CancellationToken;
///
/// Gets or sets the endpoint type that handled the request, if applicable.
///
public OpenIddictValidationEndpointType EndpointType
{
get => Transaction.EndpointType;
set => Transaction.EndpointType = value;
}
///
/// Gets or sets the request of the current transaction, if available.
///
public Uri? RequestUri
{
get => Transaction.RequestUri;
set => Transaction.RequestUri = value;
}
///
/// Gets or sets the base of the host, if available.
///
public Uri? BaseUri
{
get => Transaction.BaseUri;
set => Transaction.BaseUri = value;
}
///
/// Gets or sets the server configuration used for the current request.
///
public OpenIddictConfiguration Configuration
{
get => Transaction.Configuration;
set => Transaction.Configuration = value;
}
///
/// Gets the logger responsible for logging processed operations.
///
public ILogger Logger
=> field ??= Transaction.ServiceProvider.GetRequiredService>();
///
/// Gets the OpenIddict validation options.
///
public OpenIddictValidationOptions Options => Transaction.Options;
///
/// Gets the service provider associated with the current transaction.
///
[EditorBrowsable(EditorBrowsableState.Advanced)]
public IServiceProvider ServiceProvider => Transaction.ServiceProvider;
}
///
/// Represents an abstract base class used for certain event contexts.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class BaseRequestContext : BaseContext
{
///
/// Creates a new instance of the class.
///
protected BaseRequestContext(OpenIddictValidationTransaction transaction)
: base(transaction)
{
}
///
/// Gets a boolean indicating whether the request was fully handled.
///
public bool IsRequestHandled { get; private set; }
///
/// Gets a boolean indicating whether the request processing was skipped.
///
public bool IsRequestSkipped { get; private set; }
///
/// Marks the request as fully handled. Once declared handled,
/// a request shouldn't be processed further by the underlying host.
///
public void HandleRequest() => IsRequestHandled = true;
///
/// Marks the request as skipped. Once declared skipped, a request
/// shouldn't be processed further by OpenIddict but should be allowed
/// to go through the next components in the processing pipeline
/// (if this pattern is supported by the underlying host).
///
public void SkipRequest() => IsRequestSkipped = true;
}
///
/// Represents an abstract base class used for certain event contexts.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class BaseExternalContext : BaseValidatingContext
{
///
/// Creates a new instance of the class.
///
protected BaseExternalContext(OpenIddictValidationTransaction transaction)
: base(transaction)
{
}
///
/// Gets or sets the URI of the external endpoint to communicate with.
///
public Uri? RemoteUri { get; set; }
///
/// Gets or sets the client authentication method used
/// when communicating with the external endpoint, if applicable.
///
public string? ClientAuthenticationMethod { get; set; }
///
/// Gets or sets the X.509 client certificate that will be used to authenticate
/// this peer when communicating with the external endpoint, if applicable.
///
public X509Certificate2? LocalCertificate { get; set; }
}
///
/// Represents an abstract base class used for certain event contexts.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class BaseValidatingContext : BaseRequestContext
{
///
/// Creates a new instance of the class.
///
protected BaseValidatingContext(OpenIddictValidationTransaction transaction)
: base(transaction)
{
}
///
/// Gets a boolean indicating whether the request will be rejected.
///
public bool IsRejected { get; protected set; }
///
/// Gets or sets the "error" parameter returned to the client application.
///
public string? Error { get; private set; }
///
/// Gets or sets the "error_description" parameter returned to the client application.
///
public string? ErrorDescription { get; private set; }
///
/// Gets or sets the "error_uri" parameter returned to the client application.
///
public string? ErrorUri { get; private set; }
///
/// Rejects the request.
///
/// The "error" parameter returned to the client application.
/// The "error_description" parameter returned to the client application.
/// The "error_uri" parameter returned to the client application.
public virtual void Reject(string? error = null, string? description = null, string? uri = null)
{
Error = error;
ErrorDescription = description;
ErrorUri = uri;
IsRejected = true;
}
}
///
/// Represents an event called when processing an incoming request.
///
public sealed class ProcessRequestContext : BaseValidatingContext
{
///
/// Creates a new instance of the class.
///
public ProcessRequestContext(OpenIddictValidationTransaction transaction)
: base(transaction)
{
}
}
///
/// Represents an event called when processing an errored response.
///
public sealed class ProcessErrorContext : BaseRequestContext
{
///
/// Creates a new instance of the class.
///
public ProcessErrorContext(OpenIddictValidationTransaction transaction)
: base(transaction)
{
}
///
/// Gets or sets the request, or if it couldn't be extracted.
///
public OpenIddictRequest? Request
{
get => Transaction.Request;
set => Transaction.Request = value;
}
///
/// Gets or sets the response.
///
public OpenIddictResponse Response
{
get => Transaction.Response!;
set => Transaction.Response = value;
}
///
/// Gets or sets the error returned to the caller.
///
public string? Error { get; set; }
///
/// Gets or sets the error description returned to the caller.
///
public string? ErrorDescription { get; set; }
///
/// Gets or sets the error URI returned to the caller.
///
public string? ErrorUri { get; set; }
///
/// Gets the additional parameters returned to the caller.
///
public Dictionary Parameters { get; } = new(StringComparer.Ordinal);
}
///
/// Represents an event called when processing an authentication operation.
///
public sealed class ProcessAuthenticationContext : BaseValidatingContext
{
///
/// Creates a new instance of the class.
///
public ProcessAuthenticationContext(OpenIddictValidationTransaction transaction)
: base(transaction)
{
}
///
/// Gets or sets the request.
///
public OpenIddictRequest Request
{
get => Transaction.Request!;
set => Transaction.Request = value;
}
///
/// Gets or sets the URI of the introspection endpoint, if applicable.
///
public Uri? IntrospectionEndpoint { get; set; }
///
/// Gets or sets the client authentication method used when
/// communicating with the introspection endpoint, if applicable.
///
public string? IntrospectionEndpointClientAuthenticationMethod { get; set; }
///
/// Gets or sets the X.509 client certificate used when
/// communicating with the introspection endpoint, if applicable.
///
public X509Certificate2? IntrospectionEndpointClientCertificate { get; set; }
///
/// Gets or sets a boolean indicating whether an introspection request should be sent.
///
public bool SendIntrospectionRequest { get; set; }
///
/// Gets or sets the principal extracted from the access token, if applicable.
///
public ClaimsPrincipal? AccessTokenPrincipal { get; set; }
///
/// Gets or sets the access token to validate, if applicable.
///
public string? AccessToken { get; set; }
///
/// Gets or sets a boolean indicating whether an access
/// token should be extracted from the current context.
///
///
/// Note: overriding the value of this property is generally not recommended.
///
public bool ExtractAccessToken { get; set; }
///
/// Gets or sets a boolean indicating whether an access token
/// must be resolved for the authentication to be considered valid.
///
///
/// Note: overriding the value of this property is generally not recommended.
///
public bool RequireAccessToken { get; set; }
///
/// Gets or sets a boolean indicating whether the access
/// token extracted from the current context should be validated.
///
///
/// Note: overriding the value of this property is generally not recommended.
///
public bool ValidateAccessToken { get; set; }
///
/// Gets or sets a boolean indicating whether an invalid access token
/// will cause the authentication demand to be rejected or will be ignored.
///
///
/// Note: overriding the value of this property is generally not recommended.
///
public bool RejectAccessToken { get; set; }
///
/// Gets or sets the request sent to the introspection endpoint, if applicable.
///
public OpenIddictRequest? IntrospectionRequest { get; set; }
///
/// Gets or sets the response returned by the introspection endpoint, if applicable.
///
public OpenIddictResponse? IntrospectionResponse { get; set; }
///
/// Gets or sets a boolean indicating whether a client assertion
/// token should be generated (and optionally included in the request).
///
///
/// Note: overriding the value of this property is generally not recommended.
///
public bool GenerateClientAssertion { get; set; }
///
/// Gets or sets a boolean indicating whether the generated client
/// assertion should be included as part of the request.
///
///
/// Note: overriding the value of this property is generally not recommended.
///
public bool IncludeClientAssertion { get; set; }
///
/// Gets or sets the generated client assertion, if applicable.
/// The client assertion will only be returned if
/// is set to .
///
public string? ClientAssertion { get; set; }
///
/// Gets or sets type of the generated client assertion, if applicable.
/// The client assertion type will only be returned if
/// is set to .
///
public string? ClientAssertionType { get; set; }
///
/// Gets or sets the principal containing the claims that will be
/// used to create the client assertion, if applicable.
///
public ClaimsPrincipal? ClientAssertionPrincipal { get; set; }
}
///
/// Represents an event called when processing a challenge operation.
///
public sealed class ProcessChallengeContext : BaseValidatingContext
{
///
/// Creates a new instance of the class.
///
public ProcessChallengeContext(OpenIddictValidationTransaction transaction)
: base(transaction)
{
}
///
/// Gets or sets the request.
///
public OpenIddictRequest Request
{
get => Transaction.Request!;
set => Transaction.Request = value;
}
///
/// Gets or sets the response.
///
public OpenIddictResponse Response
{
get => Transaction.Response!;
set => Transaction.Response = value;
}
///
/// Gets the user-defined authentication properties, if available.
///
public Dictionary Properties { get; } = new(StringComparer.Ordinal);
///
/// Gets the additional parameters returned to the caller.
///
public Dictionary Parameters { get; } = new(StringComparer.Ordinal);
}
}