Skip to content

Commit 4431f42

Browse files
Rework API
1 parent 27f9343 commit 4431f42

18 files changed

+323
-338
lines changed

Modules/Authentication.Web/Concern/WebAuthenticationBuilder.cs

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,38 @@
11
using GenHTTP.Api.Content;
22
using GenHTTP.Api.Infrastructure;
3+
using GenHTTP.Modules.Authentication.Web.Integration;
34
using System;
45

56
namespace GenHTTP.Modules.Authentication.Web.Concern
67
{
78

89
public sealed class WebAuthenticationBuilder : IConcernBuilder
910
{
10-
private bool _AllowAnonymous;
11+
private IWebAuthIntegration? _Integration;
1112

12-
private SessionConfig? _SessionConfig;
13-
14-
private LoginConfig? _LoginConfig;
15-
16-
private SetupConfig? _SetupConfig;
13+
private ISessionHandling? _SessionHandling;
1714

1815
#region Functionality
1916

20-
public WebAuthenticationBuilder AllowAnonymous()
21-
{
22-
_AllowAnonymous = true;
23-
return this;
24-
}
25-
26-
public WebAuthenticationBuilder SessionHandling(SessionConfig sessionConfig)
27-
{
28-
_SessionConfig = sessionConfig;
29-
return this;
30-
}
31-
32-
public WebAuthenticationBuilder EnableSetup(SetupConfig setupConfig)
17+
public WebAuthenticationBuilder Integration(IWebAuthIntegration integration)
3318
{
34-
_SetupConfig = setupConfig;
19+
_Integration = integration;
3520
return this;
3621
}
3722

38-
public WebAuthenticationBuilder Login(LoginConfig loginConfig)
23+
public WebAuthenticationBuilder SessionHandling(ISessionHandling sessionHandling)
3924
{
40-
_LoginConfig = loginConfig;
25+
_SessionHandling = sessionHandling;
4126
return this;
4227
}
4328

4429
public IConcern Build(IHandler parent, Func<IHandler, IHandler> contentFactory)
4530
{
46-
var sessionConfig = _SessionConfig ?? throw new BuilderMissingPropertyException("Sessions");
31+
var integration = _Integration ?? throw new BuilderMissingPropertyException("Integration");
4732

48-
var loginConfig = _LoginConfig ?? throw new BuilderMissingPropertyException("Login");
33+
var sessionHandling = _SessionHandling ?? new DefaultSessionHandling();
4934

50-
return new WebAuthenticationConcern(parent, contentFactory, _AllowAnonymous, sessionConfig, loginConfig, _SetupConfig);
35+
return new WebAuthenticationConcern(parent, contentFactory, integration, sessionHandling);
5136
}
5237

5338
#endregion

Modules/Authentication.Web/Concern/WebAuthenticationConcern.cs

+42-62
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Threading.Tasks;
4-
5-
using GenHTTP.Api.Content;
1+
using GenHTTP.Api.Content;
62
using GenHTTP.Api.Content.Authentication;
73
using GenHTTP.Api.Protocol;
84
using GenHTTP.Api.Routing;
9-
105
using GenHTTP.Modules.Basics;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
119

1210
namespace GenHTTP.Modules.Authentication.Web.Concern
1311
{
@@ -21,36 +19,29 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
2119

2220
public IHandler Parent { get; }
2321

24-
private bool AllowAnonymous { get; }
25-
26-
private SessionConfig SessionConfig { get; }
22+
private IWebAuthIntegration Integration { get; }
2723

28-
private LoginConfig LoginConfig { get; }
24+
private ISessionHandling SessionHandling { get; }
2925

3026
private IHandler LoginHandler { get; }
3127

32-
private SetupConfig? SetupConfig { get; }
33-
34-
private IHandler? SetupHandler { get; }
28+
private IHandler SetupHandler { get; }
3529

3630
#endregion
3731

3832
#region Initialization
3933

40-
public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> contentFactory, bool allowAnonymous,
41-
SessionConfig sessionConfig, LoginConfig loginConfig, SetupConfig? setupConfig)
34+
public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> contentFactory,
35+
IWebAuthIntegration integration, ISessionHandling sessionHandling)
4236
{
4337
Parent = parent;
4438
Content = contentFactory(this);
4539

46-
AllowAnonymous = allowAnonymous;
47-
SessionConfig = sessionConfig;
48-
49-
LoginConfig = loginConfig;
50-
LoginHandler = loginConfig.Handler.Build(this);
40+
Integration = integration;
41+
SessionHandling = sessionHandling;
5142

52-
SetupConfig = setupConfig;
53-
SetupHandler = setupConfig?.Handler.Build(this);
43+
LoginHandler = integration.LoginHandler.Build(this);
44+
SetupHandler = integration.SetupHandler.Build(this);
5445
}
5546

5647
#endregion
@@ -63,45 +54,37 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
6354

6455
public async ValueTask<IResponse?> HandleAsync(IRequest request)
6556
{
66-
Login.SetConfig(request, LoginConfig);
67-
SessionHandling.SetConfig(request, SessionConfig);
68-
6957
var segment = request.Target.Current;
7058

71-
if ((SetupConfig != null) && (SetupHandler != null))
59+
if (await Integration.CheckSetupRequired(request).ConfigureAwait(false))
7260
{
73-
if (await SetupConfig.SetupRequired(request))
61+
if (segment?.Value != Integration.SetupRoute)
7462
{
75-
if (segment?.Value != SetupConfig.Route)
76-
{
77-
// enforce setup wizard
78-
return await Redirect.To("{setup}/", true)
79-
.Build(this)
80-
.HandleAsync(request);
81-
}
82-
else
83-
{
84-
request.Target.Advance();
85-
86-
Setup.SetConfig(request, SetupConfig);
87-
88-
return await SetupHandler.HandleAsync(request);
89-
}
90-
}
91-
else if (segment?.Value == SetupConfig.Route)
92-
{
93-
// do not allow setup to be called again
94-
return await Redirect.To("{web-auth}", true)
63+
// enforce setup wizard
64+
return await Redirect.To("{setup}/", true)
9565
.Build(this)
9666
.HandleAsync(request);
9767
}
68+
else
69+
{
70+
request.Target.Advance();
71+
72+
return await SetupHandler.HandleAsync(request);
73+
}
74+
}
75+
else if (segment?.Value == Integration.SetupRoute)
76+
{
77+
// do not allow setup to be called again
78+
return await Redirect.To("{web-auth}", true)
79+
.Build(this)
80+
.HandleAsync(request);
9881
}
9982

100-
var token = await SessionConfig.ReadToken(request);
83+
var token = SessionHandling.ReadToken(request);
10184

10285
if (token != null)
10386
{
104-
var authenticatedUser = await SessionConfig.VerifyToken(token);
87+
var authenticatedUser = await Integration.VerifyTokenAsync(token);
10588

10689
if (authenticatedUser != null)
10790
{
@@ -115,15 +98,15 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
11598
if (response != null)
11699
{
117100
// refresh the token, so the user will not be logged out eventually
118-
SessionConfig.WriteToken(response, token);
101+
SessionHandling.WriteToken(response, token);
119102
}
120103

121104
return response;
122105
}
123106
}
124107

125108
// handle login and registration (todo)
126-
if (segment?.Value == LoginConfig.Route)
109+
if (segment?.Value == Integration.LoginRoute)
127110
{
128111
request.Target.Advance();
129112

@@ -136,27 +119,27 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
136119

137120
if (authenticatedUser != null)
138121
{
139-
var generatedToken = await SessionConfig.StartSession(request, authenticatedUser);
122+
var generatedToken = await Integration.StartSessionAsync(request, authenticatedUser);
140123

141124
// actually tell the client about the token
142-
SessionConfig.WriteToken(loginResponse, generatedToken);
125+
SessionHandling.WriteToken(loginResponse, generatedToken);
143126
}
144127
}
145128

146129
return loginResponse;
147130
}
148131

149-
if (AllowAnonymous)
132+
if (Integration.AllowAnonymous)
150133
{
151134
var response = await Content.HandleAsync(request);
152135

153136
if ((response != null) && (token != null))
154137
{
155138
// clear the invalid cookie
156-
SessionConfig.ClearToken(response);
139+
SessionHandling.ClearToken(response);
157140
}
158141

159-
return null;
142+
return response;
160143
}
161144
else
162145
{
@@ -171,15 +154,12 @@ public void Append(PathBuilder path, IRequest request, IHandler? child = null)
171154
{
172155
if (child == LoginHandler)
173156
{
174-
path.Preprend(LoginConfig.Route);
157+
path.Preprend(Integration.LoginRoute);
175158
}
176159

177-
if (SetupConfig != null)
160+
if (child == SetupHandler)
178161
{
179-
if (child == SetupHandler)
180-
{
181-
path.Preprend(SetupConfig.Route);
182-
}
162+
path.Preprend(Integration.SetupRoute);
183163
}
184164
}
185165

Modules/Authentication.Web/Controllers/LoginController.cs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23

34
using GenHTTP.Api.Content;
5+
using GenHTTP.Api.Content.Authentication;
46
using GenHTTP.Api.Content.Templating;
57
using GenHTTP.Api.Protocol;
68
using GenHTTP.Modules.Basics;
@@ -14,6 +16,13 @@ namespace GenHTTP.Modules.Authentication.Web.Controllers
1416
public class LoginController
1517
{
1618

19+
private Func<IRequest, string, string, ValueTask<IUser?>> PerformLogin { get; }
20+
21+
public LoginController(Func<IRequest, string, string, ValueTask<IUser?>> performLogin)
22+
{
23+
PerformLogin = performLogin;
24+
}
25+
1726
public IHandlerBuilder Index()
1827
{
1928
// ToDo: already logged in
@@ -23,13 +32,11 @@ public IHandlerBuilder Index()
2332
[ControllerAction(RequestMethod.POST)]
2433
public async Task<IHandlerBuilder> Index(string user, string password, IRequest request)
2534
{
26-
var loginConfig = Login.GetConfig(request);
27-
28-
var result = await loginConfig.PerformLogin!(request, user, password);
35+
var authenticatedUser = await PerformLogin(request, user, password);
2936

30-
if (result.Status == LoginStatus.Success)
37+
if (authenticatedUser != null)
3138
{
32-
request.SetUser(result.AuthenticatedUser!);
39+
request.SetUser(authenticatedUser);
3340

3441
return Redirect.To("{web-auth}/", true);
3542
}

Modules/Authentication.Web/Controllers/SetupController.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23

34
using GenHTTP.Api.Content;
45
using GenHTTP.Api.Content.Templating;
@@ -15,6 +16,13 @@ namespace GenHTTP.Modules.Authentication.Web.Controllers
1516
public sealed class SetupController
1617
{
1718

19+
private Func<IRequest, string, string, ValueTask> PerformSetup { get; }
20+
21+
public SetupController(Func<IRequest, string, string, ValueTask> performSetup)
22+
{
23+
PerformSetup = performSetup;
24+
}
25+
1826
public IHandlerBuilder Index()
1927
{
2028
return ModRazor.Page(Resource.FromAssembly("EnterAccount.cshtml"), (r, h) => new BasicModel(r, h))
@@ -24,9 +32,7 @@ public IHandlerBuilder Index()
2432
[ControllerAction(RequestMethod.POST)]
2533
public async Task<IHandlerBuilder> Index(string user, string password, IRequest request)
2634
{
27-
var setupConfig = Setup.GetConfig(request);
28-
29-
var result = await setupConfig.PerformSetup!(request, user, password);
35+
await PerformSetup(request, user, password);
3036

3137
return Redirect.To("{web-auth}/", true);
3238
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using GenHTTP.Api.Protocol;
2+
3+
namespace GenHTTP.Modules.Authentication.Web
4+
{
5+
6+
public interface ISessionHandling
7+
{
8+
9+
string? ReadToken(IRequest request);
10+
11+
void WriteToken(IResponse response, string sessionToken);
12+
13+
void ClearToken(IResponse response);
14+
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using GenHTTP.Api.Content.Authentication;
2+
using GenHTTP.Api.Protocol;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace GenHTTP.Modules.Authentication.Web
10+
{
11+
12+
public interface ISimpleWebAuthIntegration
13+
{
14+
15+
bool AllowAnonymous { get => false; }
16+
17+
string SetupRoute { get => "setup"; }
18+
19+
string LoginRoute { get => "login"; }
20+
21+
ValueTask<bool> CheckSetupRequired(IRequest request);
22+
23+
ValueTask PerformSetup(IRequest request, string username, string password);
24+
25+
ValueTask<IUser?> VerifyTokenAsync(string sessionToken);
26+
27+
ValueTask<string> StartSessionAsync(IRequest request, IUser user);
28+
29+
ValueTask<IUser?> PerformLogin(IRequest request, string username, string password);
30+
31+
}
32+
33+
}

0 commit comments

Comments
 (0)