-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from FiniteReality/feature/user-info
Expose APIs for specifying user information in a platform agnostic way
- Loading branch information
Showing
9 changed files
with
172 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Security.Principal; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Finite.Commands; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace ConsoleCommands.Authentication | ||
{ | ||
internal class PlatformUserMiddleware : ICommandMiddleware | ||
{ | ||
private static readonly Random Rng = new Random(); | ||
private readonly ILogger _logger; | ||
|
||
public PlatformUserMiddleware(ILogger<PlatformUserMiddleware> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public ValueTask<ICommandResult> ExecuteAsync(CommandMiddleware next, | ||
CommandContext context, CancellationToken cancellationToken) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
if (OperatingSystem.IsWindows()) | ||
{ | ||
var identity = WindowsIdentity.GetCurrent(); | ||
|
||
context.User.AddIdentity(identity); | ||
} | ||
else | ||
{ | ||
context.User.AddIdentity( | ||
new ClaimsIdentity( | ||
new[] | ||
{ | ||
new Claim(ClaimTypes.Name, Environment.UserName), | ||
} | ||
)); | ||
} | ||
|
||
if (Rng.NextDouble() > 0.5) | ||
{ | ||
_logger.LogDebug("Adding cool role"); | ||
context.User.AddIdentity( | ||
new ClaimsIdentity( | ||
new[] | ||
{ | ||
new Claim(ClaimTypes.Role, "Cool") | ||
} | ||
)); | ||
} | ||
|
||
var nameClaim = context.User | ||
.FindFirst(x => x.Type == ClaimTypes.Name); | ||
|
||
if (nameClaim != null) | ||
_logger.LogInformation("Loaded user identity as {name}", | ||
nameClaim.Value); | ||
else | ||
_logger.LogWarning("Unable to load user identity"); | ||
|
||
return next(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Finite.Commands | ||
{ | ||
/// <summary> | ||
/// Extension methods for adding middleware to | ||
/// an <see cref="ICommandsBuilder"/>. | ||
/// </summary> | ||
public static class CommandsBuilderMiddlewareExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a middleware object to the command pipeline. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The commands builder to add middleware to. | ||
/// </param> | ||
/// <typeparam name="TMiddleware"> | ||
/// The type of middleware to add to the command pipeline. | ||
/// </typeparam> | ||
/// <returns> | ||
/// The builder. | ||
/// </returns> | ||
public static ICommandsBuilder Use<TMiddleware>( | ||
this ICommandsBuilder builder) | ||
where TMiddleware : ICommandMiddleware | ||
{ | ||
|
||
return builder.Use( | ||
static (n, c, ct) => ExecuteMiddleware(n, c, ct)); | ||
|
||
static ValueTask<ICommandResult> ExecuteMiddleware( | ||
CommandMiddleware next, CommandContext context, | ||
CancellationToken cancellationToken) | ||
{ | ||
var middleware = context.Services | ||
.GetRequiredService<TMiddleware>(); | ||
|
||
return middleware.ExecuteAsync(next, context, cancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Finite.Commands | ||
{ | ||
/// <summary> | ||
/// Defines an interface which can be used to define middleware using a | ||
/// type. | ||
/// </summary> | ||
public interface ICommandMiddleware | ||
{ | ||
/// <summary> | ||
/// Executes the middleware. | ||
/// </summary> | ||
/// <param name="next"> | ||
/// A callback used to transfer control in the middleware pipeline. | ||
/// </param> | ||
/// <param name="context"> | ||
/// The command context to execute. | ||
/// </param> | ||
/// <param name="cancellationToken"> | ||
/// A cancellation token, indicating cancellation of processing. | ||
/// </param> | ||
/// <returns> | ||
/// A task that represents the completion of command processing. | ||
/// </returns> | ||
public ValueTask<ICommandResult> ExecuteAsync( | ||
CommandMiddleware next, CommandContext context, | ||
CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters