Skip to content

Commit fee6626

Browse files
Refine APIs and documentation
1 parent 884a4b0 commit fee6626

11 files changed

+413
-210
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
1-
using GenHTTP.Api.Content;
2-
using GenHTTP.Api.Infrastructure;
3-
using GenHTTP.Modules.Authentication.Web.Integration;
4-
using System;
5-
6-
namespace GenHTTP.Modules.Authentication.Web.Concern
7-
{
8-
9-
public sealed class WebAuthenticationBuilder : IConcernBuilder
10-
{
11-
private IWebAuthIntegration? _Integration;
12-
13-
private ISessionHandling? _SessionHandling;
14-
15-
#region Functionality
16-
17-
public WebAuthenticationBuilder Integration(IWebAuthIntegration integration)
18-
{
19-
_Integration = integration;
20-
return this;
21-
}
22-
23-
public WebAuthenticationBuilder SessionHandling(ISessionHandling sessionHandling)
24-
{
25-
_SessionHandling = sessionHandling;
26-
return this;
27-
}
28-
29-
public IConcern Build(IHandler parent, Func<IHandler, IHandler> contentFactory)
30-
{
31-
var integration = _Integration ?? throw new BuilderMissingPropertyException("Integration");
32-
33-
var sessionHandling = _SessionHandling ?? new DefaultSessionHandling();
34-
35-
return new WebAuthenticationConcern(parent, contentFactory, integration, sessionHandling);
36-
}
37-
38-
#endregion
39-
40-
}
41-
42-
}
1+
using System;
2+
3+
using GenHTTP.Api.Content;
4+
using GenHTTP.Api.Content.Authentication;
5+
using GenHTTP.Api.Infrastructure;
6+
7+
namespace GenHTTP.Modules.Authentication.Web.Concern
8+
{
9+
10+
public sealed class WebAuthenticationBuilder<TUser> : IConcernBuilder where TUser : class, IUser
11+
{
12+
private readonly IWebAuthIntegration<TUser> _Integration;
13+
14+
private ISessionHandling? _SessionHandling;
15+
16+
#region Functionality
17+
18+
public WebAuthenticationBuilder(IWebAuthIntegration<TUser> integration)
19+
{
20+
_Integration = integration;
21+
}
22+
23+
/// <summary>
24+
/// Configures the session handline to be used by this concern.
25+
/// </summary>
26+
/// <typeparam name="T">The class used to implement session handling</typeparam>
27+
public WebAuthenticationBuilder<TUser> SessionHandling<T>() where T : ISessionHandling, new() => SessionHandling(new T());
28+
29+
/// <summary>
30+
/// Configures the session handline instance to be used by this concern.
31+
/// </summary>
32+
/// <param name="sessionHandling">The session handling instance to be used</param>
33+
public WebAuthenticationBuilder<TUser> SessionHandling(ISessionHandling sessionHandling)
34+
{
35+
_SessionHandling = sessionHandling;
36+
return this;
37+
}
38+
39+
public IConcern Build(IHandler parent, Func<IHandler, IHandler> contentFactory)
40+
{
41+
var sessionHandling = _SessionHandling ?? throw new BuilderMissingPropertyException("Session Handling");
42+
43+
return new WebAuthenticationConcern<TUser>(parent, contentFactory, _Integration, sessionHandling);
44+
}
45+
46+
#endregion
47+
48+
}
49+
50+
}

Modules/Authentication.Web/Concern/WebAuthenticationConcern.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace GenHTTP.Modules.Authentication.Web.Concern
1111
{
1212

13-
public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHandlerResolver
13+
public sealed class WebAuthenticationConcern<TUser> : IConcern, IRootPathAppender, IHandlerResolver where TUser : class, IUser
1414
{
1515

1616
#region Get-/Setters
@@ -19,7 +19,7 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
1919

2020
public IHandler Parent { get; }
2121

22-
private IWebAuthIntegration Integration { get; }
22+
private IWebAuthIntegration<TUser> Integration { get; }
2323

2424
private ISessionHandling SessionHandling { get; }
2525

@@ -36,7 +36,7 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
3636
#region Initialization
3737

3838
public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> contentFactory,
39-
IWebAuthIntegration integration, ISessionHandling sessionHandling)
39+
IWebAuthIntegration<TUser> integration, ISessionHandling sessionHandling)
4040
{
4141
Parent = parent;
4242
Content = contentFactory(this);
@@ -101,7 +101,7 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
101101

102102
if (token != null)
103103
{
104-
var authenticatedUser = await Integration.VerifyTokenAsync(token);
104+
var authenticatedUser = await Integration.VerifyTokenAsync(request, token);
105105

106106
if (authenticatedUser != null)
107107
{
@@ -122,7 +122,7 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
122122
if (loginResponse != null)
123123
{
124124
// establish the session if the user was authenticated
125-
var authenticatedUser = request.GetUser<IUser>();
125+
var authenticatedUser = request.GetUser<TUser>();
126126

127127
if (authenticatedUser != null)
128128
{
@@ -145,7 +145,7 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
145145

146146
if (response != null)
147147
{
148-
if (request.GetUser<IUser>() == null)
148+
if (request.GetUser<TUser>() == null)
149149
{
150150
SessionHandling.ClearToken(response);
151151
}

Modules/Authentication.Web/Controllers/LoginController.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace GenHTTP.Modules.Authentication.Web.Controllers
1313
{
1414

15-
public class LoginController
15+
public class LoginController<TUser> where TUser : IUser
1616
{
1717

18-
private Func<IRequest, string, string, ValueTask<IUser?>> PerformLogin { get; }
18+
private Func<IRequest, string, string, ValueTask<TUser?>> PerformLogin { get; }
1919

20-
public LoginController(Func<IRequest, string, string, ValueTask<IUser?>> performLogin)
20+
public LoginController(Func<IRequest, string, string, ValueTask<TUser?>> performLogin)
2121
{
2222
PerformLogin = performLogin;
2323
}

Modules/Authentication.Web/ISessionHandling.cs

+20
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,33 @@
33
namespace GenHTTP.Modules.Authentication.Web
44
{
55

6+
/// <summary>
7+
/// Defines how session references are sent to the client
8+
/// and read from the incoming requests.
9+
/// </summary>
610
public interface ISessionHandling
711
{
812

13+
/// <summary>
14+
/// Attempts to read a session token from the given request.
15+
/// </summary>
16+
/// <param name="request">The request to be analyzed</param>
17+
/// <returns>The session token read from the request (if present)</returns>
918
string? ReadToken(IRequest request);
1019

20+
/// <summary>
21+
/// Modifies the given response to instruct the client to use the
22+
/// given session token.
23+
/// </summary>
24+
/// <param name="response">The response to be modified</param>
25+
/// <param name="sessionToken">The token to be written to the client</param>
1126
void WriteToken(IResponse response, string sessionToken);
1227

28+
/// <summary>
29+
/// Configures the response to instruct the client that the
30+
/// session token is no longer valid and should be discarded.
31+
/// </summary>
32+
/// <param name="response">The response to be modified</param>
1333
void ClearToken(IResponse response);
1434

1535
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,130 @@
1-
using System.Threading.Tasks;
2-
3-
using GenHTTP.Api.Content.Authentication;
4-
using GenHTTP.Api.Protocol;
5-
6-
namespace GenHTTP.Modules.Authentication.Web
7-
{
8-
9-
public interface ISimpleWebAuthIntegration
10-
{
11-
12-
bool AllowAnonymous { get => false; }
13-
14-
string SetupRoute { get => "setup"; }
15-
16-
string LoginRoute { get => "login"; }
17-
18-
string LogoutRoute { get => "logout"; }
19-
20-
string ResourceRoute { get => "auth-resources"; }
21-
22-
ValueTask<bool> CheckSetupRequired(IRequest request);
23-
24-
ValueTask PerformSetup(IRequest request, string username, string password);
25-
26-
ValueTask<IUser?> VerifyTokenAsync(string sessionToken);
27-
28-
ValueTask<string> StartSessionAsync(IRequest request, IUser user);
29-
30-
ValueTask<IUser?> PerformLogin(IRequest request, string username, string password);
31-
32-
}
33-
34-
}
1+
using System.Threading.Tasks;
2+
3+
using GenHTTP.Api.Content.Authentication;
4+
using GenHTTP.Api.Protocol;
5+
6+
namespace GenHTTP.Modules.Authentication.Web
7+
{
8+
9+
/// <summary>
10+
/// Authentication and authorization logic to be used by the
11+
/// web authentication concern.
12+
/// </summary>
13+
/// <typeparam name="TUser">The type of user managed by this integration</typeparam>
14+
/// <remarks>
15+
/// Use this kind of integration if you would like to quickly
16+
/// add login forms to your application. This integration will
17+
/// use a default set of controllers that render very simple
18+
/// UIs to you app users.
19+
///
20+
/// If you would like to customize the authentication workflow,
21+
/// use <see cref="IWebAuthIntegration{TUser}" /> instead.
22+
/// </remarks>
23+
public interface ISimpleWebAuthIntegration<TUser> where TUser : IUser
24+
{
25+
26+
/// <summary>
27+
/// False if you would like to force non logged in users
28+
/// to log in.
29+
/// </summary>
30+
bool AllowAnonymous { get => false; }
31+
32+
/// <summary>
33+
/// The route the setup functionality will be available
34+
/// from (defaults to "/setup/").
35+
/// </summary>
36+
string SetupRoute { get => "setup"; }
37+
38+
/// <summary>
39+
/// The route the login page will be available from
40+
/// (defaults to "/login/").
41+
/// </summary>
42+
string LoginRoute { get => "login"; }
43+
44+
/// <summary>
45+
/// The route the logout page will be available from
46+
/// (defaults to "/logout/").
47+
/// </summary>
48+
string LogoutRoute { get => "logout"; }
49+
50+
/// <summary>
51+
/// The route used to serve additional resources required
52+
/// by the default controllers.
53+
/// </summary>
54+
/// <remarks>
55+
/// This is used by the default controllers which implement
56+
/// the simple integration flow to serve additional style sheets
57+
/// to style the login page.
58+
/// </remarks>
59+
string ResourceRoute { get => "auth-resources"; }
60+
61+
/// <summary>
62+
/// Return true to redirect users to a setup page that allows
63+
/// an administrator setting up your application to initially
64+
/// create an accont with.
65+
/// </summary>
66+
/// <param name="request">The currently handled request</param>
67+
/// <returns>true if the application needs to be set up</returns>
68+
/// <remarks>
69+
/// This feature allows you to provision your application without
70+
/// the need of using fixed user accounts which would compromise
71+
/// the security of your deployments.
72+
///
73+
/// Typically you want to return true while there are no users
74+
/// yet and false as soon as there are some.
75+
///
76+
/// To disable the feature completely, just return false here.
77+
/// </remarks>
78+
ValueTask<bool> CheckSetupRequired(IRequest request);
79+
80+
/// <summary>
81+
/// Called by the setup controller to initialize your application
82+
/// for the given admin user.
83+
/// </summary>
84+
/// <param name="request">The currently handled request</param>
85+
/// <param name="username">The name entered by the user</param>
86+
/// <param name="password">The password entered by the user</param>
87+
/// <remarks>
88+
/// After this call, <see cref="CheckSetupRequired(IRequest)" /> is
89+
/// expected to return false on subsequent calls.
90+
/// </remarks>
91+
ValueTask PerformSetup(IRequest request, string username, string password);
92+
93+
/// <summary>
94+
/// Invoked with the session token read from the client connection
95+
/// to actually load and check the session.
96+
/// </summary>
97+
/// <param name="request">The currently handled request</param>
98+
/// <param name="sessionToken">The token read from the client connection</param>
99+
/// <returns>The user record the session belongs to or null, if the session is not valid anymore</returns>
100+
/// <remarks>
101+
/// In this method you will need to verify the session specified by the client
102+
/// against some session storage, e.g. a Redis or database server.
103+
/// </remarks>
104+
ValueTask<TUser?> VerifyTokenAsync(IRequest request, string sessionToken);
105+
106+
/// <summary>
107+
/// Invoked to generate a new session token for the authenticated user.
108+
/// </summary>
109+
/// <param name="request">The currently handled request</param>
110+
/// <param name="user">The user which just performed a login</param>
111+
/// <returns>The newly created (or re-used) session token</returns>
112+
ValueTask<string> StartSessionAsync(IRequest request, TUser user);
113+
114+
/// <summary>
115+
/// Invoked to actually authenticate an user who entered their
116+
/// credentials in the login form.
117+
/// </summary>
118+
/// <param name="request">The currently handled request</param>
119+
/// <param name="username">The name of the user</param>
120+
/// <param name="password">The password of the user</param>
121+
/// <returns>The matching user record if the credentials are valid</returns>
122+
/// <remarks>
123+
/// If the user account does not exist or the credentials are incorrect,
124+
/// return null to cause the controller to render an error message.
125+
/// </remarks>
126+
ValueTask<TUser?> PerformLoginAsync(IRequest request, string username, string password);
127+
128+
}
129+
130+
}

0 commit comments

Comments
 (0)