Skip to content

Commit cd0e661

Browse files
Draft evolution
1 parent bbf75b4 commit cd0e661

8 files changed

+105
-99
lines changed

Modules/Authentication.Web/Concern/WebAuthenticationBuilder.cs

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
11
using GenHTTP.Api.Content;
2-
using GenHTTP.Api.Infrastructure;
3-
using GenHTTP.Modules.Authentication.Web.Controllers;
4-
using GenHTTP.Modules.Controllers;
52
using System;
63

74
namespace GenHTTP.Modules.Authentication.Web.Concern
85
{
96

107
public sealed class WebAuthenticationBuilder : IConcernBuilder
118
{
12-
private IWebAuthenticationBackend? _Backend;
13-
14-
private IHandlerBuilder? _SetupHandler;
15-
private string? _SetupRoute;
9+
private SetupConfig? _SetupConfig;
1610

1711
#region Functionality
1812

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)
13+
public WebAuthenticationBuilder EnableSetup(SetupConfig setupConfig)
2814
{
29-
_SetupHandler = handler ?? Controller.From<SetupController>();
30-
_SetupRoute = route ?? "setup";
31-
15+
_SetupConfig = setupConfig;
3216
return this;
3317
}
3418

3519
public IConcern Build(IHandler parent, Func<IHandler, IHandler> contentFactory)
3620
{
37-
var backend = _Backend ?? throw new BuilderMissingPropertyException("Backend");
38-
39-
return new WebAuthenticationConcern(parent, contentFactory, backend, _SetupHandler, _SetupRoute);
21+
return new WebAuthenticationConcern(parent, contentFactory, _SetupConfig);
4022
}
4123

4224
#endregion

Modules/Authentication.Web/Concern/WebAuthenticationConcern.cs

+20-41
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using GenHTTP.Api.Protocol;
33
using GenHTTP.Api.Routing;
44
using GenHTTP.Modules.Basics;
5-
using GenHTTP.Modules.Layouting;
65
using System;
76
using System.Collections.Generic;
87
using System.Threading.Tasks;
@@ -19,52 +18,22 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
1918

2019
public IHandler Parent { get; }
2120

22-
public IWebAuthenticationBackend Backend { get; }
21+
private SetupConfig? SetupConfig { get; }
2322

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; }
23+
private IHandler? SetupHandler { get; }
3724

3825
#endregion
3926

4027
#region Initialization
4128

4229
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)
30+
SetupConfig? setupConfig)
4831
{
4932
Parent = parent;
5033
Content = contentFactory(this);
5134

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-
}*/
35+
SetupConfig = setupConfig;
36+
SetupHandler = setupConfig?.Handler.Build(this);
6837
}
6938

7039
#endregion
@@ -79,11 +48,11 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
7948
{
8049
var segment = request.Target.Current;
8150

82-
if ((SetupRoute != null) && (SetupHandler != null))
51+
if ((SetupConfig != null) && (SetupHandler != null))
8352
{
84-
if (await Backend.CheckSetupRequired(request))
53+
if (await SetupConfig.SetupRequired(request))
8554
{
86-
if (segment?.Value != SetupRoute)
55+
if (segment?.Value != SetupConfig.Route)
8756
{
8857
return await Redirect.To("{setup}/", true)
8958
.Build(this)
@@ -93,6 +62,8 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
9362
{
9463
request.Target.Advance();
9564

65+
Setup.SetConfig(request, SetupConfig);
66+
9667
return await SetupHandler.HandleAsync(request);
9768
}
9869
}
@@ -103,14 +74,22 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
10374

10475
public void Append(PathBuilder path, IRequest request, IHandler? child = null)
10576
{
106-
if ((child == SetupHandler) && (SetupRoute != null))
77+
if (SetupConfig != null)
10778
{
108-
path.Preprend(SetupRoute);
79+
if (child == SetupHandler)
80+
{
81+
path.Preprend(SetupConfig.Route);
82+
}
10983
}
11084
}
11185

11286
public IHandler? Find(string segment)
11387
{
88+
if (segment == "{web-auth}")
89+
{
90+
return this;
91+
}
92+
11493
if (segment == "{setup}")
11594
{
11695
return SetupHandler;

Modules/Authentication.Web/Controllers/SetupController.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using GenHTTP.Api.Content;
22
using GenHTTP.Api.Content.Templating;
3-
3+
using GenHTTP.Api.Protocol;
4+
using GenHTTP.Modules.Basics;
5+
using GenHTTP.Modules.Controllers;
46
using GenHTTP.Modules.IO;
57
using GenHTTP.Modules.Razor;
8+
using System.Threading.Tasks;
69

710
namespace GenHTTP.Modules.Authentication.Web.Controllers
811
{
@@ -16,6 +19,16 @@ public IHandlerBuilder Index()
1619
.Title("Setup");
1720
}
1821

22+
[ControllerAction(RequestMethod.POST)]
23+
public async Task<IHandlerBuilder> Index(string username, string password, IRequest request)
24+
{
25+
var setupConfig = Setup.GetConfig(request);
26+
27+
var result = await setupConfig.PerformSetup!(request, username, password);
28+
29+
return Redirect.To("{web-auth}/", true);
30+
}
31+
1932
}
2033

2134
}

Modules/Authentication.Web/GenHTTP.Modules.Authentication.Web.csproj

+1-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
</PropertyGroup>
3434

3535
<ItemGroup>
36-
<None Remove="Views\Setup\CreateAccount.cshtml" />
37-
</ItemGroup>
38-
39-
<ItemGroup>
40-
<EmbeddedResource Include="Views\Setup\CreateAccount.cshtml" />
36+
<EmbeddedResource Include="Views\EnterAccount.cshtml" />
4137
</ItemGroup>
4238

4339
<ItemGroup>

Modules/Authentication.Web/IWebAuthenticationBackend.cs

-14
This file was deleted.

Modules/Authentication.Web/Setup.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using GenHTTP.Api.Content;
2+
using GenHTTP.Api.Protocol;
3+
using GenHTTP.Modules.Authentication.Web.Concern;
4+
using GenHTTP.Modules.Authentication.Web.Controllers;
5+
using GenHTTP.Modules.Controllers;
6+
using System;
7+
using System.Threading.Tasks;
8+
9+
namespace GenHTTP.Modules.Authentication.Web
10+
{
11+
12+
public enum SetupResult
13+
{
14+
Success
15+
}
16+
17+
public record class SetupConfig
18+
(
19+
20+
IHandlerBuilder Handler,
21+
22+
string Route,
23+
24+
Func<IRequest, ValueTask<bool>> SetupRequired,
25+
26+
Func<IRequest, string, string, ValueTask<SetupResult>>? PerformSetup
27+
28+
);
29+
30+
public static class Setup
31+
{
32+
33+
private const string SETUP_CONFIG = "webAuth_setupConfig";
34+
35+
public static SetupConfig BuiltIn(Func<IRequest, ValueTask<bool>> setupRequired, Func<IRequest, string, string, ValueTask<SetupResult>> performSetup, string route = "setup")
36+
{
37+
return new SetupConfig(Controller.From<SetupController>(), route, setupRequired, performSetup);
38+
}
39+
40+
public static SetupConfig Custom(Func<IRequest, ValueTask<bool>> setupRequired, IHandlerBuilder handler, string route = "setup")
41+
{
42+
return new SetupConfig(handler, route, setupRequired, null);
43+
}
44+
45+
public static SetupConfig GetConfig(IRequest request) => (SetupConfig)request.Properties[SETUP_CONFIG];
46+
47+
public static void SetConfig(IRequest request, SetupConfig config) => request.Properties[SETUP_CONFIG] = config;
48+
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
<form id="setup" method="POST" action=".">
1+
<form id="credentials" method="POST" action=".">
22

3+
<input type="text" id="user" placeholder="Username" />
34

5+
<input type="password" id="password" />
6+
7+
<input type="submit" value="Send" />
48

59
</form>

Playground/Program.cs

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
using GenHTTP.Api.Protocol;
2-
using GenHTTP.Engine;
1+
using GenHTTP.Engine;
32
using GenHTTP.Modules.Authentication.Web;
43
using GenHTTP.Modules.IO;
54
using GenHTTP.Modules.Practices;
6-
using System.Threading.Tasks;
5+
6+
var setupDone = false;
7+
8+
var setup = Setup.BuiltIn
9+
(
10+
setupRequired: (req) => new(!setupDone),
11+
performSetup: (req, u, p) => { setupDone = true; return new(SetupResult.Success); }
12+
);
713

814
var auth = WebAuthentication.Create()
9-
.Backend<MyBackend>()
10-
.EnableSetup();
15+
.EnableSetup(setup);
1116

1217
Host.Create()
1318
.Handler(Content.From(Resource.FromString("Hello World")).Add(auth))
1419
.Defaults()
1520
.Development()
1621
.Console()
1722
.Run();
18-
19-
public class MyBackend : IWebAuthenticationBackend
20-
{
21-
22-
public ValueTask<bool> CheckSetupRequired(IRequest request)
23-
{
24-
return new(true);
25-
}
26-
27-
}

0 commit comments

Comments
 (0)