Skip to content

Commit bbf75b4

Browse files
API design fiddling
1 parent 47a9acb commit bbf75b4

12 files changed

+376
-46
lines changed

GenHTTP.sln

+8-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenHTTP.Testing.Acceptance"
104104
EndProject
105105
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenHTTP.Testing", "Testing\Testing\GenHTTP.Testing.csproj", "{FC7F7D69-5ED0-4D3B-B201-EDBCEC71B8DB}"
106106
EndProject
107+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenHTTP.Modules.Authentication.Web", "Modules\Authentication.Web\GenHTTP.Modules.Authentication.Web.csproj", "{DE883AB8-8A0C-4CB1-B1D2-2CB0906FED03}"
108+
EndProject
107109
Global
108110
GlobalSection(SolutionConfigurationPlatforms) = preSolution
109111
Debug|Any CPU = Debug|Any CPU
@@ -258,6 +260,10 @@ Global
258260
{FC7F7D69-5ED0-4D3B-B201-EDBCEC71B8DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
259261
{FC7F7D69-5ED0-4D3B-B201-EDBCEC71B8DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
260262
{FC7F7D69-5ED0-4D3B-B201-EDBCEC71B8DB}.Release|Any CPU.Build.0 = Release|Any CPU
263+
{DE883AB8-8A0C-4CB1-B1D2-2CB0906FED03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
264+
{DE883AB8-8A0C-4CB1-B1D2-2CB0906FED03}.Debug|Any CPU.Build.0 = Debug|Any CPU
265+
{DE883AB8-8A0C-4CB1-B1D2-2CB0906FED03}.Release|Any CPU.ActiveCfg = Release|Any CPU
266+
{DE883AB8-8A0C-4CB1-B1D2-2CB0906FED03}.Release|Any CPU.Build.0 = Release|Any CPU
261267
EndGlobalSection
262268
GlobalSection(SolutionProperties) = preSolution
263269
HideSolutionNode = FALSE
@@ -302,9 +308,10 @@ Global
302308
{0D9957DE-8FE2-44A1-B9D7-EADD1DBDBD6E} = {23B23225-275E-4F52-8B29-6F44C85B6ACE}
303309
{C5067243-AFBA-4A17-BD61-DDB503367EA3} = {A7930BE4-0549-4197-B139-B1A73E74B464}
304310
{FC7F7D69-5ED0-4D3B-B201-EDBCEC71B8DB} = {A7930BE4-0549-4197-B139-B1A73E74B464}
311+
{DE883AB8-8A0C-4CB1-B1D2-2CB0906FED03} = {23B23225-275E-4F52-8B29-6F44C85B6ACE}
305312
EndGlobalSection
306313
GlobalSection(ExtensibilityGlobals) = postSolution
307-
SolutionGuid = {9C67B3AF-0BF6-4E21-8C39-3F74CFCF9632}
308314
LessCompiler = 2603124e-1287-4d61-9540-6ac3efad4eb9
315+
SolutionGuid = {9C67B3AF-0BF6-4E21-8C39-3F74CFCF9632}
309316
EndGlobalSection
310317
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using GenHTTP.Api.Content;
2+
using GenHTTP.Api.Infrastructure;
3+
using GenHTTP.Modules.Authentication.Web.Controllers;
4+
using GenHTTP.Modules.Controllers;
5+
using System;
6+
7+
namespace GenHTTP.Modules.Authentication.Web.Concern
8+
{
9+
10+
public sealed class WebAuthenticationBuilder : IConcernBuilder
11+
{
12+
private IWebAuthenticationBackend? _Backend;
13+
14+
private IHandlerBuilder? _SetupHandler;
15+
private string? _SetupRoute;
16+
17+
#region Functionality
18+
19+
public WebAuthenticationBuilder Backend(IWebAuthenticationBackend backend)
20+
{
21+
_Backend = backend;
22+
return this;
23+
}
24+
25+
public WebAuthenticationBuilder Backend<T>() where T : IWebAuthenticationBackend, new() => Backend(new T());
26+
27+
public WebAuthenticationBuilder EnableSetup(IHandlerBuilder? handler = null, string? route = null)
28+
{
29+
_SetupHandler = handler ?? Controller.From<SetupController>();
30+
_SetupRoute = route ?? "setup";
31+
32+
return this;
33+
}
34+
35+
public IConcern Build(IHandler parent, Func<IHandler, IHandler> contentFactory)
36+
{
37+
var backend = _Backend ?? throw new BuilderMissingPropertyException("Backend");
38+
39+
return new WebAuthenticationConcern(parent, contentFactory, backend, _SetupHandler, _SetupRoute);
40+
}
41+
42+
#endregion
43+
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using GenHTTP.Api.Content;
2+
using GenHTTP.Api.Content.Templating;
3+
4+
using GenHTTP.Modules.IO;
5+
using GenHTTP.Modules.Razor;
6+
7+
namespace GenHTTP.Modules.Authentication.Web.Controllers
8+
{
9+
10+
public sealed class SetupController
11+
{
12+
13+
public IHandlerBuilder Index()
14+
{
15+
return ModRazor.Page(Resource.FromAssembly("EnterAccount.cshtml"), (r, h) => new BasicModel(r, h))
16+
.Title("Setup");
17+
}
18+
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
5+
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
6+
7+
<LangVersion>10.0</LangVersion>
8+
<Nullable>enable</Nullable>
9+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
10+
11+
<AssemblyVersion>8.3.0.0</AssemblyVersion>
12+
<FileVersion>8.3.0.0</FileVersion>
13+
<Version>8.3.0</Version>
14+
15+
<Authors>Andreas Nägeli</Authors>
16+
<Company />
17+
18+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
19+
<PackageProjectUrl>https://genhttp.org/</PackageProjectUrl>
20+
21+
<Description>Adds web based login capabilities to the GenHTTP webserver.</Description>
22+
<PackageTags>HTTP Webserver C# Module Authentication Authorization Security Web Registration Login</PackageTags>
23+
24+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
25+
<IncludeSymbols>true</IncludeSymbols>
26+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
27+
28+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
29+
<NoWarn>CS1591,CS1587,CS1572,CS1573</NoWarn>
30+
31+
<PackageIcon>icon.png</PackageIcon>
32+
33+
</PropertyGroup>
34+
35+
<ItemGroup>
36+
<None Remove="Views\Setup\CreateAccount.cshtml" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<EmbeddedResource Include="Views\Setup\CreateAccount.cshtml" />
41+
</ItemGroup>
42+
43+
<ItemGroup>
44+
45+
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
46+
<None Include="..\..\Resources\icon.png" Pack="true" PackagePath="\" />
47+
48+
</ItemGroup>
49+
50+
<ItemGroup>
51+
52+
<ProjectReference Include="..\..\API\GenHTTP.Api.csproj" />
53+
54+
<ProjectReference Include="..\Controllers\GenHTTP.Modules.Controllers.csproj" />
55+
<ProjectReference Include="..\Razor\GenHTTP.Modules.Razor.csproj" />
56+
57+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
58+
59+
</ItemGroup>
60+
61+
<ItemGroup>
62+
<Folder Include="Sessions\" />
63+
</ItemGroup>
64+
65+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace GenHTTP.Modules.Authentication.Web
8+
{
9+
10+
public interface ISessionNegotiation
11+
{
12+
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using GenHTTP.Api.Protocol;
2+
using System.Threading.Tasks;
3+
4+
namespace GenHTTP.Modules.Authentication.Web
5+
{
6+
7+
public interface IWebAuthenticationBackend
8+
{
9+
10+
ValueTask<bool> CheckSetupRequired(IRequest request);
11+
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<form id="setup" method="POST" action=".">
2+
3+
4+
5+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using GenHTTP.Modules.Authentication.Web.Concern;
2+
3+
namespace GenHTTP.Modules.Authentication.Web
4+
{
5+
6+
public static class WebAuthentication
7+
{
8+
9+
public static WebAuthenticationBuilder Create() => new WebAuthenticationBuilder();
10+
11+
}
12+
13+
}

0 commit comments

Comments
 (0)