| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WindowsAuthenticationService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Runtime.Versioning; |
| | | 9 | | using System.Security.Principal; |
| | | 10 | | using System.Threading; |
| | | 11 | | using MyNet.Utilities.Authentication; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Platform.Windows.Authentication; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Windows-specific authentication service using <see cref="WindowsUserPrincipal"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | [SupportedOSPlatform("windows")] |
| | | 19 | | public class WindowsAuthenticationService : WindowsAuthenticationService<WindowsUserPrincipal> |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Shared anonymous principal used when no authenticated principal is available. |
| | | 23 | | /// </summary> |
| | | 24 | | public static readonly WindowsUserPrincipal Anonymous = new(new GenericIdentity(string.Empty), []); |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | protected override WindowsUserPrincipal CreatePrincipal(IIdentity identity) => new(identity, []); |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | protected override WindowsUserPrincipal GetAnonymous() => Anonymous; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Base implementation of a Windows authentication service. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <typeparam name="TPrincipal">The type of principal used by the service.</typeparam> |
| | | 37 | | [SupportedOSPlatform("windows")] |
| | | 38 | | public abstract class WindowsAuthenticationService<TPrincipal> : IAuthenticationService<TPrincipal> |
| | | 39 | | where TPrincipal : IPrincipal |
| | | 40 | | { |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public event EventHandler<AuthenticatedEventArgs>? Authenticated; |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 63 | 45 | | public bool IsAuthenticated => Thread.CurrentPrincipal?.Identity?.IsAuthenticated ?? false; |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | 12 | 48 | | public TPrincipal CurrentPrincipal => Thread.CurrentPrincipal is TPrincipal principal ? principal : GetAnonymous(); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Authenticates using the current Windows identity and sets the principal. |
| | | 52 | | /// </summary> |
| | 6 | 53 | | public virtual void Authenticate() => Authenticate(CreatePrincipal(WindowsIdentity.GetCurrent())); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Unauthenticates and resets to an anonymous principal. |
| | | 57 | | /// </summary> |
| | 12 | 58 | | public virtual void Unauthenticate() => Authenticate(GetAnonymous()); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Sets the provided principal as the current thread principal and raises the <see cref="Authenticated"/> event. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="principal">The principal to set for the current thread.</param> |
| | | 64 | | protected virtual void Authenticate(TPrincipal principal) |
| | | 65 | | { |
| | 27 | 66 | | ArgumentNullException.ThrowIfNull(principal); |
| | | 67 | | |
| | 27 | 68 | | var wasAuthenticated = IsAuthenticated; |
| | | 69 | | |
| | 27 | 70 | | Thread.CurrentPrincipal = principal; |
| | | 71 | | |
| | 27 | 72 | | var isAuthenticated = IsAuthenticated; |
| | | 73 | | |
| | 27 | 74 | | if (wasAuthenticated != isAuthenticated) |
| | 21 | 75 | | Authenticated?.Invoke(this, new AuthenticatedEventArgs(isAuthenticated)); |
| | 12 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Returns an anonymous principal instance used when no authenticated principal is available. |
| | | 80 | | /// </summary> |
| | | 81 | | protected abstract TPrincipal GetAnonymous(); |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Creates a principal instance from an identity. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="identity">The identity to create the principal from.</param> |
| | | 87 | | protected abstract TPrincipal CreatePrincipal(IIdentity identity); |
| | | 88 | | } |
| | | 89 | | |