Commit 5ef0fed8 authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Remove identity middleware

parent 233780e4
Loading
Loading
Loading
Loading
+0 −30
Original line number Diff line number Diff line
using Microsoft.Extensions.Options;

namespace Cals.Visor.Api.Identity;

public static class ApplicationBuildingExtensions
{
    /// <summary>
    /// Enables use of the Visor user identity.
    /// </summary>
    /// <param name="app"></param>
    /// <returns></returns>
    public static IApplicationBuilder UseAppIdentity(this IApplicationBuilder app)
    {
        ArgumentNullException.ThrowIfNull(app);

        return app.UseMiddleware<IdentityMiddleware>();
    }

    /// <summary>
    /// Enables Visor user with the given options.
    /// </summary>
    public static IApplicationBuilder UseAppIdentity(this IApplicationBuilder app, IdentityUserOptions options)
    {
        ArgumentNullException.ThrowIfNull(app);

        ArgumentNullException.ThrowIfNull(options);

        return app.UseMiddleware<IdentityMiddleware>(Options.Create(options));
    }
}
+0 −80
Original line number Diff line number Diff line
using Cals.Authentication;
using Cals.Visor.Identity;
using Cals.Visor.Models.Identity;
using Microsoft.Extensions.Options;

namespace Cals.Visor.Api.Identity;

/// <summary>
/// Middleware to add this app user's identity to the ClaimsPrincipal.
/// </summary>
public class IdentityMiddleware
{
    private readonly IdentityUserOptions _options;
    private readonly RequestDelegate _next;
    private readonly ILogger _logger;

    /// <summary>
    /// Initializes a new instance of the <see cref="IdentityMiddleware"/> class.
    /// </summary>
    /// <param name="next">The next middleware in the pipeline.</param>
    /// <param name="options">The configuration options.</param>
    /// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
    public IdentityMiddleware(RequestDelegate next, IOptions<IdentityUserOptions> options, ILoggerFactory loggerFactory)
    {
        ArgumentNullException.ThrowIfNull(options);

        ArgumentNullException.ThrowIfNull(loggerFactory);

        _next = next ?? throw new ArgumentNullException(nameof(next));
        _options = options.Value;
        _logger = loggerFactory.CreateLogger<IdentityMiddleware>();
    }

    /// <summary>
    /// Processes a request to add the Visor identity to the user principal.
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public async Task Invoke(HttpContext context)
    {
        if (context.User.Identity == null || !context.User.Identity.IsAuthenticated)
        {
            await _next(context);
            return;
        }

        // if additional Visor-specific fields are needed, create a new identity and add to principal here
        var user = new VisorUser(context.User);

        //var command = new EnsureUserIsRegisteredCommand(user);

        //await requestProcessor.ExecuteCommandAsync(command);

        await _next(context);
    }

    private async Task<AppUser> EnsureUserIsRegistered(IApplicationUser user)
    {
        // unique is required to do anything with user registration
        if (user.UniqueId == null)
            throw new Exception("User must have an non-null UniqueId to be registered");

        AppUser userToUpdate = user.ToAppUser();

        // is this user already registered?
        AppUser? userAtApi = await _apiClient.AppUsers.GetUserAsync(user.UniqueId);

        if (userAtApi != null)
        {
            userToUpdate.AppUserId = userAtApi.AppUserId;
            await _apiClient.AppUsers.UpdateUserAsync(userToUpdate);
            return userToUpdate;
        }

        userAtApi = await _apiClient.AppUsers.RegisterUserAsync(userToUpdate);

        return userAtApi;
    }

}
+0 −2
Original line number Diff line number Diff line
@@ -127,8 +127,6 @@ app.UseCors();
app.UseAuthentication();
app.UseHttpsRedirection();

app.UseAppIdentity();

app.UseSwagger();
app.UseSwaggerUI();