Skip to content

Commit 967c8d3

Browse files
Additional tests
1 parent 173dfa0 commit 967c8d3

File tree

4 files changed

+105
-74
lines changed

4 files changed

+105
-74
lines changed

Modules/Authentication.Web/Concern/WebAuthenticationConcern.cs

+16-9
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
9191
.HandleAsync(request);
9292
}
9393

94+
// try to fetch and validate the token
95+
var authenticated = false;
96+
9497
var token = SessionHandling.ReadToken(request);
9598

9699
if (token != null)
@@ -101,16 +104,8 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
101104
{
102105
// we're logged in
103106
request.SetUser(authenticatedUser);
104-
105-
var response = await Content.HandleAsync(request);
106-
107-
if (response != null)
108-
{
109-
// refresh the token, so the user will not be logged out eventually
110-
SessionHandling.WriteToken(response, token);
111-
}
112107

113-
return response;
108+
authenticated = true;
114109
}
115110
}
116111

@@ -138,6 +133,18 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
138133
return loginResponse;
139134
}
140135

136+
if (authenticated)
137+
{
138+
var response = await Content.HandleAsync(request);
139+
140+
if ((response != null) && (token != null))
141+
{
142+
// refresh the token, so the user will not be logged out eventually
143+
SessionHandling.WriteToken(response, token);
144+
}
145+
146+
return response;
147+
}
141148
if (Integration.AllowAnonymous)
142149
{
143150
var response = await Content.HandleAsync(request);

Modules/Authentication.Web/Controllers/LoginController.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using GenHTTP.Modules.Basics;
99
using GenHTTP.Modules.Controllers;
10+
using GenHTTP.Modules.Placeholders;
1011

1112
namespace GenHTTP.Modules.Authentication.Web.Controllers
1213
{
@@ -21,9 +22,15 @@ public LoginController(Func<IRequest, string, string, ValueTask<IUser?>> perform
2122
PerformLogin = performLogin;
2223
}
2324

24-
public IHandlerBuilder Index()
25+
public IHandlerBuilder Index(IRequest request)
2526
{
26-
// ToDo: already logged in
27+
var user = request.GetUser<IUser>();
28+
29+
if (user != null)
30+
{
31+
return Page.From("Login", "You are already logged in.");
32+
}
33+
2734
return RenderLogin(status: ResponseStatus.Unauthorized);
2835
}
2936

Original file line numberDiff line numberDiff line change
@@ -1,63 +1,64 @@
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="Resources\style.css" />
37-
</ItemGroup>
38-
39-
<ItemGroup>
40-
<EmbeddedResource Include="Resources\style.css" />
41-
<EmbeddedResource Include="Views\EnterAccount.cshtml" />
42-
</ItemGroup>
43-
44-
<ItemGroup>
45-
46-
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
47-
<None Include="..\..\Resources\icon.png" Pack="true" PackagePath="\" />
48-
49-
</ItemGroup>
50-
51-
<ItemGroup>
52-
53-
<ProjectReference Include="..\..\API\GenHTTP.Api.csproj" />
54-
55-
<ProjectReference Include="..\Authentication\GenHTTP.Modules.Authentication.csproj" />
56-
<ProjectReference Include="..\Controllers\GenHTTP.Modules.Controllers.csproj" />
57-
<ProjectReference Include="..\Razor\GenHTTP.Modules.Razor.csproj" />
58-
59-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
60-
61-
</ItemGroup>
62-
63-
</Project>
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="Resources\style.css" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<EmbeddedResource Include="Resources\style.css" />
41+
<EmbeddedResource Include="Views\EnterAccount.cshtml" />
42+
</ItemGroup>
43+
44+
<ItemGroup>
45+
46+
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" />
47+
<None Include="..\..\Resources\icon.png" Pack="true" PackagePath="\" />
48+
49+
</ItemGroup>
50+
51+
<ItemGroup>
52+
53+
<ProjectReference Include="..\..\API\GenHTTP.Api.csproj" />
54+
55+
<ProjectReference Include="..\Authentication\GenHTTP.Modules.Authentication.csproj" />
56+
<ProjectReference Include="..\Controllers\GenHTTP.Modules.Controllers.csproj" />
57+
<ProjectReference Include="..\Razor\GenHTTP.Modules.Razor.csproj" />
58+
<ProjectReference Include="..\Placeholders\GenHTTP.Modules.Placeholders.csproj" />
59+
60+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
61+
62+
</ItemGroup>
63+
64+
</Project>

Testing/Acceptance/Modules/Authentication/Web/WebAuthenticationTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ public async Task TestResourcesCanBeFetched()
185185
await response.AssertStatusAsync(HttpStatusCode.OK);
186186
}
187187

188+
[TestMethod]
189+
public async Task TestCannotLoginIfAlreadyDone()
190+
{
191+
var integration = new TestIntegration().AddUser("a", "b");
192+
193+
using var host = GetHost(integration);
194+
195+
await Post(host, "/login/", "a", "b");
196+
197+
var response = await host.GetResponseAsync("/login/");
198+
199+
await response.AssertStatusAsync(HttpStatusCode.OK);
200+
201+
AssertX.Contains("You are already logged in.", await response.GetContentAsync());
202+
}
203+
188204
#endregion
189205

190206
#region Test setup

0 commit comments

Comments
 (0)