| | | 1 | | using System.Reflection; |
| | | 2 | | using Kestrun.Hosting; |
| | | 3 | | using Microsoft.AspNetCore.Authentication.Negotiate; |
| | | 4 | | |
| | | 5 | | namespace Kestrun.Authentication; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Options for Windows Authentication. |
| | | 9 | | /// </summary> |
| | | 10 | | public class WindowsAuthOptions : NegotiateOptions, IOpenApiAuthenticationOptions, IAuthenticationHostOptions |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc/> |
| | 1 | 13 | | public KestrunHost Host { get; set; } = default!; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc/> |
| | 1 | 16 | | public bool GlobalScheme { get; set; } |
| | | 17 | | |
| | | 18 | | /// <inheritdoc/> |
| | 1 | 19 | | public string? Description { get; set; } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc/> |
| | 5 | 22 | | public string[] DocumentationId { get; set; } = []; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc/> |
| | 0 | 25 | | public string? DisplayName { get; set; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The protocol to use for Windows Authentication. |
| | | 29 | | /// </summary> |
| | 1 | 30 | | public WindowsAuthProtocol Protocol { get; set; } = WindowsAuthProtocol.Negotiate; |
| | | 31 | | |
| | | 32 | | private Serilog.ILogger? _logger; |
| | | 33 | | /// <inheritdoc/> |
| | | 34 | | public Serilog.ILogger Logger |
| | | 35 | | { |
| | 0 | 36 | | get => _logger ?? (Host is null ? Serilog.Log.Logger : Host.Logger); set => _logger = value; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Helper to copy values from a user-supplied WindowsAuthOptions instance to the instance |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="target"></param> |
| | | 43 | | public void ApplyTo(WindowsAuthOptions target) |
| | | 44 | | { |
| | 0 | 45 | | ApplyTo((NegotiateOptions)target); |
| | 0 | 46 | | target.GlobalScheme = GlobalScheme; |
| | 0 | 47 | | target.Description = Description; |
| | 0 | 48 | | target.DocumentationId = DocumentationId; |
| | 0 | 49 | | target.DisplayName = DisplayName; |
| | 0 | 50 | | target.Host = Host; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Helper to copy values from a user-supplied WindowsAuthOptions instance to the instance |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="target"></param> |
| | | 57 | | public void ApplyTo(NegotiateOptions target) |
| | | 58 | | { |
| | 0 | 59 | | target.PersistKerberosCredentials = PersistKerberosCredentials; |
| | 0 | 60 | | target.PersistNtlmCredentials = PersistNtlmCredentials; |
| | | 61 | | |
| | 0 | 62 | | var ldapSettingsProp = typeof(NegotiateOptions) |
| | 0 | 63 | | .GetProperty("LdapSettings", BindingFlags.Instance | BindingFlags.NonPublic); |
| | | 64 | | |
| | 0 | 65 | | if (ldapSettingsProp?.GetValue(target) is object ldapSettings) |
| | | 66 | | { |
| | 0 | 67 | | var domainProp = ldapSettings.GetType() |
| | 0 | 68 | | .GetProperty("Domain", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); |
| | 0 | 69 | | var domain = domainProp?.GetValue(ldapSettings) as string; |
| | 0 | 70 | | if (!string.IsNullOrEmpty(domain)) |
| | | 71 | | { |
| | 0 | 72 | | target.EnableLdap(domain); |
| | | 73 | | } |
| | | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | } |