1
- using GenHTTP . Api . Content ;
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Threading . Tasks ;
4
+
5
+ using GenHTTP . Api . Content ;
2
6
using GenHTTP . Api . Content . Authentication ;
3
7
using GenHTTP . Api . Protocol ;
4
8
using GenHTTP . Api . Routing ;
9
+
5
10
using GenHTTP . Modules . Basics ;
6
- using Microsoft . AspNetCore . Razor . Language . Intermediate ;
7
- using System ;
8
- using System . Collections . Generic ;
9
- using System . Threading . Tasks ;
10
11
11
12
namespace GenHTTP . Modules . Authentication . Web . Concern
12
13
{
@@ -24,6 +25,10 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
24
25
25
26
private SessionConfig SessionConfig { get ; }
26
27
28
+ private LoginConfig LoginConfig { get ; }
29
+
30
+ private IHandler LoginHandler { get ; }
31
+
27
32
private SetupConfig ? SetupConfig { get ; }
28
33
29
34
private IHandler ? SetupHandler { get ; }
@@ -33,14 +38,17 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
33
38
#region Initialization
34
39
35
40
public WebAuthenticationConcern ( IHandler parent , Func < IHandler , IHandler > contentFactory , bool allowAnonymous ,
36
- SessionConfig sessionConfig , SetupConfig ? setupConfig )
41
+ SessionConfig sessionConfig , LoginConfig loginConfig , SetupConfig ? setupConfig )
37
42
{
38
43
Parent = parent ;
39
44
Content = contentFactory ( this ) ;
40
45
41
46
AllowAnonymous = allowAnonymous ;
42
47
SessionConfig = sessionConfig ;
43
48
49
+ LoginConfig = loginConfig ;
50
+ LoginHandler = loginConfig . Handler . Build ( this ) ;
51
+
44
52
SetupConfig = setupConfig ;
45
53
SetupHandler = setupConfig ? . Handler . Build ( this ) ;
46
54
}
@@ -55,6 +63,9 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
55
63
56
64
public async ValueTask < IResponse ? > HandleAsync ( IRequest request )
57
65
{
66
+ Login . SetConfig ( request , LoginConfig ) ;
67
+ SessionHandling . SetConfig ( request , SessionConfig ) ;
68
+
58
69
var segment = request . Target . Current ;
59
70
60
71
if ( ( SetupConfig != null ) && ( SetupHandler != null ) )
@@ -77,7 +88,7 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
77
88
return await SetupHandler . HandleAsync ( request ) ;
78
89
}
79
90
}
80
- else if ( segment ? . Value == SetupConfig . Route )
91
+ else if ( segment ? . Value == SetupConfig . Route )
81
92
{
82
93
// do not allow setup to be called again
83
94
return await Redirect . To ( "{web-auth}" , true )
@@ -95,10 +106,46 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
95
106
if ( authenticatedUser != null )
96
107
{
97
108
// we're logged in
98
- return await Content . HandleAsync ( request ) ;
109
+ request . SetUser ( authenticatedUser ) ;
110
+
111
+ // deny login and registration (todo)
112
+
113
+ var response = await Content . HandleAsync ( request ) ;
114
+
115
+ if ( response != null )
116
+ {
117
+ // refresh the token, so the user will not be logged out eventually
118
+ SessionConfig . WriteToken ( response , token ) ;
119
+ }
120
+
121
+ return response ;
99
122
}
100
123
}
101
124
125
+ // handle login and registration (todo)
126
+ if ( segment ? . Value == LoginConfig . Route )
127
+ {
128
+ request . Target . Advance ( ) ;
129
+
130
+ var loginResponse = await LoginHandler . HandleAsync ( request ) ;
131
+
132
+ if ( loginResponse != null )
133
+ {
134
+ // establish the session if the user was authenticated
135
+ var authenticatedUser = request . GetUser < IUser > ( ) ;
136
+
137
+ if ( authenticatedUser != null )
138
+ {
139
+ var generatedToken = await SessionConfig . StartSession ( request , authenticatedUser ) ;
140
+
141
+ // actually tell the client about the token
142
+ SessionConfig . WriteToken ( loginResponse , generatedToken ) ;
143
+ }
144
+ }
145
+
146
+ return loginResponse ;
147
+ }
148
+
102
149
if ( AllowAnonymous )
103
150
{
104
151
var response = await Content . HandleAsync ( request ) ;
@@ -111,14 +158,22 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
111
158
112
159
return null ;
113
160
}
114
-
115
- // enforce login (todo)
116
-
117
- return null ;
161
+ else
162
+ {
163
+ // enforce login
164
+ return await Redirect . To ( "{login}/" , true )
165
+ . Build ( this )
166
+ . HandleAsync ( request ) ;
167
+ }
118
168
}
119
169
120
170
public void Append ( PathBuilder path , IRequest request , IHandler ? child = null )
121
171
{
172
+ if ( child == LoginHandler )
173
+ {
174
+ path . Preprend ( LoginConfig . Route ) ;
175
+ }
176
+
122
177
if ( SetupConfig != null )
123
178
{
124
179
if ( child == SetupHandler )
@@ -135,6 +190,11 @@ public void Append(PathBuilder path, IRequest request, IHandler? child = null)
135
190
return this ;
136
191
}
137
192
193
+ if ( segment == "{login}" )
194
+ {
195
+ return LoginHandler ;
196
+ }
197
+
138
198
if ( segment == "{setup}" )
139
199
{
140
200
return SetupHandler ;
0 commit comments