|
| 1 | +using GenHTTP.Api.Content; |
| 2 | +using GenHTTP.Api.Protocol; |
| 3 | +using GenHTTP.Api.Routing; |
| 4 | +using GenHTTP.Modules.Basics; |
| 5 | +using GenHTTP.Modules.Layouting; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace GenHTTP.Modules.Authentication.Web.Concern |
| 11 | +{ |
| 12 | + |
| 13 | + public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHandlerResolver |
| 14 | + { |
| 15 | + |
| 16 | + #region Get-/Setters |
| 17 | + |
| 18 | + public IHandler Content { get; } |
| 19 | + |
| 20 | + public IHandler Parent { get; } |
| 21 | + |
| 22 | + public IWebAuthenticationBackend Backend { get; } |
| 23 | + |
| 24 | + //public ISessionNegotiation SessionNegotiation { get; } |
| 25 | + |
| 26 | + //public string LoginRoute { get; } |
| 27 | + |
| 28 | + //public bool AllowAnonymous { get; } |
| 29 | + |
| 30 | + //public string LogoutRoute { get; } |
| 31 | + |
| 32 | + //public string? RegistrationRoute { get; } |
| 33 | + |
| 34 | + public IHandler? SetupHandler { get; } |
| 35 | + |
| 36 | + public string? SetupRoute { get; } |
| 37 | + |
| 38 | + #endregion |
| 39 | + |
| 40 | + #region Initialization |
| 41 | + |
| 42 | + public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> contentFactory, |
| 43 | + IWebAuthenticationBackend backend, //ISessionNegotiation sessionNegotiation, |
| 44 | + //IHandlerBuilder loginHandler, string loginRoute, bool allowAnonymous, |
| 45 | + //IHandlerBuilder logoutHandler, string logoutRoute, |
| 46 | + //IHandlerBuilder? registrationHandler, string? registrationRoute, |
| 47 | + IHandlerBuilder? setupHandler, string? setupRoute) |
| 48 | + { |
| 49 | + Parent = parent; |
| 50 | + Content = contentFactory(this); |
| 51 | + |
| 52 | + Backend = backend; |
| 53 | + //SessionNegotiation = sessionNegotiation; |
| 54 | + |
| 55 | + //LoginRoute = loginRoute; |
| 56 | + //AllowAnonymous = allowAnonymous; |
| 57 | + |
| 58 | + //LogoutRoute = logoutRoute; |
| 59 | + |
| 60 | + SetupRoute = setupRoute; |
| 61 | + SetupHandler = setupHandler?.Build(this); |
| 62 | + |
| 63 | + /*if ((registrationRoute != null) && (registrationHandler != null)) |
| 64 | + { |
| 65 | + RegistrationRoute = registrationRoute; |
| 66 | + overlay.Add(registrationRoute, registrationHandler); |
| 67 | + }*/ |
| 68 | + } |
| 69 | + |
| 70 | + #endregion |
| 71 | + |
| 72 | + #region Functionality |
| 73 | + |
| 74 | + public ValueTask PrepareAsync() => Content.PrepareAsync(); |
| 75 | + |
| 76 | + public IAsyncEnumerable<ContentElement> GetContentAsync(IRequest request) => Content.GetContentAsync(request); |
| 77 | + |
| 78 | + public async ValueTask<IResponse?> HandleAsync(IRequest request) |
| 79 | + { |
| 80 | + var segment = request.Target.Current; |
| 81 | + |
| 82 | + if ((SetupRoute != null) && (SetupHandler != null)) |
| 83 | + { |
| 84 | + if (await Backend.CheckSetupRequired(request)) |
| 85 | + { |
| 86 | + if (segment?.Value != SetupRoute) |
| 87 | + { |
| 88 | + return await Redirect.To("{setup}/", true) |
| 89 | + .Build(this) |
| 90 | + .HandleAsync(request); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + request.Target.Advance(); |
| 95 | + |
| 96 | + return await SetupHandler.HandleAsync(request); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return await Content.HandleAsync(request); |
| 102 | + } |
| 103 | + |
| 104 | + public void Append(PathBuilder path, IRequest request, IHandler? child = null) |
| 105 | + { |
| 106 | + if ((child == SetupHandler) && (SetupRoute != null)) |
| 107 | + { |
| 108 | + path.Preprend(SetupRoute); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public IHandler? Find(string segment) |
| 113 | + { |
| 114 | + if (segment == "{setup}") |
| 115 | + { |
| 116 | + return SetupHandler; |
| 117 | + } |
| 118 | + |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + #endregion |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments