1
1
using GenHTTP . Api . Content ;
2
+ using GenHTTP . Api . Content . Authentication ;
2
3
using GenHTTP . Api . Protocol ;
3
4
using GenHTTP . Api . Routing ;
4
5
using GenHTTP . Modules . Basics ;
6
+ using Microsoft . AspNetCore . Razor . Language . Intermediate ;
5
7
using System ;
6
8
using System . Collections . Generic ;
7
9
using System . Threading . Tasks ;
@@ -18,6 +20,10 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
18
20
19
21
public IHandler Parent { get ; }
20
22
23
+ private bool AllowAnonymous { get ; }
24
+
25
+ private SessionConfig SessionConfig { get ; }
26
+
21
27
private SetupConfig ? SetupConfig { get ; }
22
28
23
29
private IHandler ? SetupHandler { get ; }
@@ -26,12 +32,15 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
26
32
27
33
#region Initialization
28
34
29
- public WebAuthenticationConcern ( IHandler parent , Func < IHandler , IHandler > contentFactory ,
30
- SetupConfig ? setupConfig )
35
+ public WebAuthenticationConcern ( IHandler parent , Func < IHandler , IHandler > contentFactory , bool allowAnonymous ,
36
+ SessionConfig sessionConfig , SetupConfig ? setupConfig )
31
37
{
32
38
Parent = parent ;
33
39
Content = contentFactory ( this ) ;
34
40
41
+ AllowAnonymous = allowAnonymous ;
42
+ SessionConfig = sessionConfig ;
43
+
35
44
SetupConfig = setupConfig ;
36
45
SetupHandler = setupConfig ? . Handler . Build ( this ) ;
37
46
}
@@ -54,6 +63,7 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
54
63
{
55
64
if ( segment ? . Value != SetupConfig . Route )
56
65
{
66
+ // enforce setup wizard
57
67
return await Redirect . To ( "{setup}/" , true )
58
68
. Build ( this )
59
69
. HandleAsync ( request ) ;
@@ -67,9 +77,44 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
67
77
return await SetupHandler . HandleAsync ( request ) ;
68
78
}
69
79
}
80
+ else if ( segment ? . Value == SetupConfig . Route )
81
+ {
82
+ // do not allow setup to be called again
83
+ return await Redirect . To ( "{web-auth}" , true )
84
+ . Build ( this )
85
+ . HandleAsync ( request ) ;
86
+ }
70
87
}
71
88
72
- return await Content . HandleAsync ( request ) ;
89
+ var token = await SessionConfig . ReadToken ( request ) ;
90
+
91
+ if ( token != null )
92
+ {
93
+ var authenticatedUser = await SessionConfig . VerifyToken ( token ) ;
94
+
95
+ if ( authenticatedUser != null )
96
+ {
97
+ // we're logged in
98
+ return await Content . HandleAsync ( request ) ;
99
+ }
100
+ }
101
+
102
+ if ( AllowAnonymous )
103
+ {
104
+ var response = await Content . HandleAsync ( request ) ;
105
+
106
+ if ( ( response != null ) && ( token != null ) )
107
+ {
108
+ // clear the invalid cookie
109
+ SessionConfig . ClearToken ( response ) ;
110
+ }
111
+
112
+ return null ;
113
+ }
114
+
115
+ // enforce login (todo)
116
+
117
+ return null ;
73
118
}
74
119
75
120
public void Append ( PathBuilder path , IRequest request , IHandler ? child = null )
0 commit comments