1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Threading . Tasks ;
4
-
5
- using GenHTTP . Api . Content ;
1
+ using GenHTTP . Api . Content ;
6
2
using GenHTTP . Api . Content . Authentication ;
7
3
using GenHTTP . Api . Protocol ;
8
4
using GenHTTP . Api . Routing ;
9
-
10
5
using GenHTTP . Modules . Basics ;
6
+ using System ;
7
+ using System . Collections . Generic ;
8
+ using System . Threading . Tasks ;
11
9
12
10
namespace GenHTTP . Modules . Authentication . Web . Concern
13
11
{
@@ -21,36 +19,29 @@ public sealed class WebAuthenticationConcern : IConcern, IRootPathAppender, IHan
21
19
22
20
public IHandler Parent { get ; }
23
21
24
- private bool AllowAnonymous { get ; }
25
-
26
- private SessionConfig SessionConfig { get ; }
22
+ private IWebAuthIntegration Integration { get ; }
27
23
28
- private LoginConfig LoginConfig { get ; }
24
+ private ISessionHandling SessionHandling { get ; }
29
25
30
26
private IHandler LoginHandler { get ; }
31
27
32
- private SetupConfig ? SetupConfig { get ; }
33
-
34
- private IHandler ? SetupHandler { get ; }
28
+ private IHandler SetupHandler { get ; }
35
29
36
30
#endregion
37
31
38
32
#region Initialization
39
33
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 )
42
36
{
43
37
Parent = parent ;
44
38
Content = contentFactory ( this ) ;
45
39
46
- AllowAnonymous = allowAnonymous ;
47
- SessionConfig = sessionConfig ;
48
-
49
- LoginConfig = loginConfig ;
50
- LoginHandler = loginConfig . Handler . Build ( this ) ;
40
+ Integration = integration ;
41
+ SessionHandling = sessionHandling ;
51
42
52
- SetupConfig = setupConfig ;
53
- SetupHandler = setupConfig ? . Handler . Build ( this ) ;
43
+ LoginHandler = integration . LoginHandler . Build ( this ) ;
44
+ SetupHandler = integration . SetupHandler . Build ( this ) ;
54
45
}
55
46
56
47
#endregion
@@ -63,45 +54,37 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
63
54
64
55
public async ValueTask < IResponse ? > HandleAsync ( IRequest request )
65
56
{
66
- Login . SetConfig ( request , LoginConfig ) ;
67
- SessionHandling . SetConfig ( request , SessionConfig ) ;
68
-
69
57
var segment = request . Target . Current ;
70
58
71
- if ( ( SetupConfig != null ) && ( SetupHandler != null ) )
59
+ if ( await Integration . CheckSetupRequired ( request ) . ConfigureAwait ( false ) )
72
60
{
73
- if ( await SetupConfig . SetupRequired ( request ) )
61
+ if ( segment ? . Value != Integration . SetupRoute )
74
62
{
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 )
95
65
. Build ( this )
96
66
. HandleAsync ( request ) ;
97
67
}
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 ) ;
98
81
}
99
82
100
- var token = await SessionConfig . ReadToken ( request ) ;
83
+ var token = SessionHandling . ReadToken ( request ) ;
101
84
102
85
if ( token != null )
103
86
{
104
- var authenticatedUser = await SessionConfig . VerifyToken ( token ) ;
87
+ var authenticatedUser = await Integration . VerifyTokenAsync ( token ) ;
105
88
106
89
if ( authenticatedUser != null )
107
90
{
@@ -115,15 +98,15 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
115
98
if ( response != null )
116
99
{
117
100
// refresh the token, so the user will not be logged out eventually
118
- SessionConfig . WriteToken ( response , token ) ;
101
+ SessionHandling . WriteToken ( response , token ) ;
119
102
}
120
103
121
104
return response ;
122
105
}
123
106
}
124
107
125
108
// handle login and registration (todo)
126
- if ( segment ? . Value == LoginConfig . Route )
109
+ if ( segment ? . Value == Integration . LoginRoute )
127
110
{
128
111
request . Target . Advance ( ) ;
129
112
@@ -136,27 +119,27 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
136
119
137
120
if ( authenticatedUser != null )
138
121
{
139
- var generatedToken = await SessionConfig . StartSession ( request , authenticatedUser ) ;
122
+ var generatedToken = await Integration . StartSessionAsync ( request , authenticatedUser ) ;
140
123
141
124
// actually tell the client about the token
142
- SessionConfig . WriteToken ( loginResponse , generatedToken ) ;
125
+ SessionHandling . WriteToken ( loginResponse , generatedToken ) ;
143
126
}
144
127
}
145
128
146
129
return loginResponse ;
147
130
}
148
131
149
- if ( AllowAnonymous )
132
+ if ( Integration . AllowAnonymous )
150
133
{
151
134
var response = await Content . HandleAsync ( request ) ;
152
135
153
136
if ( ( response != null ) && ( token != null ) )
154
137
{
155
138
// clear the invalid cookie
156
- SessionConfig . ClearToken ( response ) ;
139
+ SessionHandling . ClearToken ( response ) ;
157
140
}
158
141
159
- return null ;
142
+ return response ;
160
143
}
161
144
else
162
145
{
@@ -171,15 +154,12 @@ public void Append(PathBuilder path, IRequest request, IHandler? child = null)
171
154
{
172
155
if ( child == LoginHandler )
173
156
{
174
- path . Preprend ( LoginConfig . Route ) ;
157
+ path . Preprend ( Integration . LoginRoute ) ;
175
158
}
176
159
177
- if ( SetupConfig != null )
160
+ if ( child == SetupHandler )
178
161
{
179
- if ( child == SetupHandler )
180
- {
181
- path . Preprend ( SetupConfig . Route ) ;
182
- }
162
+ path . Preprend ( Integration . SetupRoute ) ;
183
163
}
184
164
}
185
165
0 commit comments