|
| 1 | +using System; |
| 2 | +using System.Net.Http.Headers; |
| 3 | +using System.Security.Claims; |
| 4 | +using System.Text; |
| 5 | +using System.Text.Encodings.Web; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Authentication; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | +using Microsoft.Net.Http.Headers; |
| 11 | + |
| 12 | +namespace Mihir.AspNetCore.Authentication.Basic |
| 13 | +{ |
| 14 | + public class BasicHandler : AuthenticationHandler<BasicOptions> |
| 15 | + { |
| 16 | + private readonly IBasicUserValidationService _basicUserValidationService; |
| 17 | + |
| 18 | + public BasicHandler(IOptionsMonitor<BasicOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IBasicUserValidationService basicUserValidationService) |
| 19 | + : base(options, logger, encoder, clock) |
| 20 | + { |
| 21 | + _basicUserValidationService = basicUserValidationService; |
| 22 | + } |
| 23 | + |
| 24 | + //protected new BasicEvents Events { get => (BasicEvents)base.Events; set => base.Events = value; } |
| 25 | + //protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new BasicEvents()); |
| 26 | + |
| 27 | + protected override async Task<AuthenticateResult> HandleAuthenticateAsync() |
| 28 | + { |
| 29 | + if (!Request.Headers.ContainsKey(HeaderNames.Authorization)) |
| 30 | + { |
| 31 | + return AuthenticateResult.NoResult(); |
| 32 | + } |
| 33 | + |
| 34 | + if (!AuthenticationHeaderValue.TryParse(Request.Headers[HeaderNames.Authorization], out var headerValue)) |
| 35 | + { |
| 36 | + return AuthenticateResult.NoResult(); |
| 37 | + } |
| 38 | + |
| 39 | + if (!headerValue.Scheme.Equals(BasicDefaults.AuthenticationScheme, StringComparison.OrdinalIgnoreCase)) |
| 40 | + { |
| 41 | + return AuthenticateResult.NoResult(); |
| 42 | + } |
| 43 | + |
| 44 | + var usernameAndPassword = Encoding.UTF8.GetString(Convert.FromBase64String(headerValue.Parameter)); |
| 45 | + var usernameAndPasswordSplit = usernameAndPassword.Split(':'); |
| 46 | + if (usernameAndPasswordSplit.Length != 2) |
| 47 | + { |
| 48 | + return AuthenticateResult.Fail("Invalid Basic authentication header"); |
| 49 | + } |
| 50 | + var username = usernameAndPasswordSplit[0]; |
| 51 | + var password = usernameAndPasswordSplit[1]; |
| 52 | + |
| 53 | + var isValidUser = await _basicUserValidationService.IsValidAsync(username, password); |
| 54 | + if (!isValidUser) |
| 55 | + { |
| 56 | + return AuthenticateResult.Fail("Invalid username or password"); |
| 57 | + } |
| 58 | + |
| 59 | + var claims = new[] { new Claim(ClaimTypes.Name, username) }; |
| 60 | + var identity = new ClaimsIdentity(claims, Scheme.Name); |
| 61 | + var principal = new ClaimsPrincipal(identity); |
| 62 | + var ticket = new AuthenticationTicket(principal, Scheme.Name); |
| 63 | + return AuthenticateResult.Success(ticket); |
| 64 | + } |
| 65 | + |
| 66 | + protected override async Task HandleChallengeAsync(AuthenticationProperties properties) |
| 67 | + { |
| 68 | + Response.Headers[HeaderNames.WWWAuthenticate] = Options.Challenge; |
| 69 | + await base.HandleChallengeAsync(properties); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments