14
14
package org .eclipse .jetty .test ;
15
15
16
16
import java .io .PrintWriter ;
17
+ import java .net .URI ;
17
18
import java .nio .file .Path ;
18
19
import java .util .Map ;
19
20
21
+ import org .eclipse .jetty .client .ContentResponse ;
22
+ import org .eclipse .jetty .client .FormRequestContent ;
20
23
import org .eclipse .jetty .client .HttpClient ;
21
24
import org .eclipse .jetty .http .HttpHeader ;
25
+ import org .eclipse .jetty .http .HttpStatus ;
22
26
import org .eclipse .jetty .io .Content ;
23
27
import org .eclipse .jetty .security .AnyUserLoginService ;
24
28
import org .eclipse .jetty .security .AuthenticationState ;
48
52
import org .junit .jupiter .api .BeforeEach ;
49
53
import org .junit .jupiter .api .Test ;
50
54
55
+ import static org .hamcrest .MatcherAssert .assertThat ;
56
+ import static org .hamcrest .Matchers .containsString ;
57
+ import static org .hamcrest .Matchers .equalTo ;
58
+
51
59
public class MultiAuthenticatorTest
52
60
{
53
61
private Server _server ;
@@ -64,7 +72,6 @@ public void before() throws Exception
64
72
65
73
_server = new Server ();
66
74
_connector = new ServerConnector (_server );
67
- _connector .setPort (8080 ); // TODO: remove.
68
75
_server .addConnector (_connector );
69
76
70
77
OpenIdConfiguration config = new OpenIdConfiguration (_provider .getProvider (), _provider .getClientId (), _provider .getClientSecret ());
@@ -77,6 +84,7 @@ public void before() throws Exception
77
84
securityHandler .setHandler (new AuthTestHandler ());
78
85
79
86
MultiAuthenticator multiAuthenticator = new MultiAuthenticator ();
87
+ multiAuthenticator .setLoginPath ("/login" );
80
88
81
89
OpenIdAuthenticator openIdAuthenticator = new OpenIdAuthenticator (config , "/error" );
82
90
openIdAuthenticator .setRedirectPath ("/redirect_path" );
@@ -116,15 +124,63 @@ public void after() throws Exception
116
124
}
117
125
118
126
@ Test
119
- public void test () throws Exception
127
+ public void testMultiAuthentication () throws Exception
120
128
{
121
- _server .join ();
129
+ URI uri = URI .create ("http://localhost:" + _connector .getLocalPort ());
130
+ ContentResponse response = _client .GET (uri );
131
+ assertThat (response .getStatus (), equalTo (HttpStatus .OK_200 ));
132
+ assertThat (response .getContentAsString (), containsString ("<h1>Multi Login Page</h1>" ));
133
+ assertThat (response .getContentAsString (), containsString ("/login/openid" ));
134
+ assertThat (response .getContentAsString (), containsString ("/login/form" ));
135
+
136
+ // Try Form Login.
137
+ response = _client .GET (uri .resolve ("/login/form" ));
138
+ assertThat (response .getStatus (), equalTo (HttpStatus .OK_200 ));
139
+ assertThat (response .getContentAsString (), containsString ("<form action=\" j_security_check\" method=\" POST\" >" ));
140
+
141
+ // Form login is successful.
142
+ Fields fields = new Fields ();
143
+ fields .put ("j_username" , "user" );
144
+ fields .put ("j_password" , "password" );
145
+ response = _client .POST (uri .resolve ("/j_security_check" ))
146
+ .body (new FormRequestContent (fields ))
147
+ .send ();
148
+ assertThat (response .getStatus (), equalTo (HttpStatus .OK_200 ));
149
+ assertThat (response .getContentAsString (), containsString ("userPrincipal: user" ));
150
+ assertThat (response .getContentAsString (), containsString ("MultiAuthenticator$MultiSucceededAuthenticationState" ));
151
+
152
+ // Logout is successful.
153
+ response = _client .GET (uri .resolve ("/logout" ));
154
+ assertThat (response .getStatus (), equalTo (HttpStatus .OK_200 ));
155
+ assertThat (response .getContentAsString (), containsString ("<h1>Multi Login Page</h1>" ));
156
+ assertThat (response .getContentAsString (), containsString ("/login/openid" ));
157
+ assertThat (response .getContentAsString (), containsString ("/login/form" ));
158
+
159
+ // We can now log in with OpenID.
160
+ _provider .setUser (new OpenIdProvider .User ("UserId1234" , "openIdUser" ));
161
+ response = _client .GET (uri .resolve ("/login/openid" ));
162
+ assertThat (response .getStatus (), equalTo (HttpStatus .OK_200 ));
163
+ assertThat (response .getContentAsString (), containsString ("userPrincipal: UserId1234" ));
164
+ assertThat (response .getContentAsString (), containsString ("Authenticated with OpenID" ));
165
+ assertThat (response .getContentAsString (), containsString ("name: openIdUser" ));
166
+
167
+ // Logout is successful.
168
+ response = _client .GET (uri .resolve ("/logout" ));
169
+ assertThat (response .getStatus (), equalTo (HttpStatus .OK_200 ));
170
+ assertThat (response .getContentAsString (), containsString ("<h1>Multi Login Page</h1>" ));
171
+ assertThat (response .getContentAsString (), containsString ("/login/openid" ));
172
+ assertThat (response .getContentAsString (), containsString ("/login/form" ));
122
173
}
123
174
124
- @ Test
125
- public void test2 () throws Exception
175
+ private static AuthenticationState .Succeeded getAuthentication (Request request )
126
176
{
127
- _server .join ();
177
+ AuthenticationState authenticationState = AuthenticationState .getAuthenticationState (request );
178
+ AuthenticationState .Succeeded auth = null ;
179
+ if (authenticationState instanceof AuthenticationState .Succeeded succeeded )
180
+ auth = succeeded ;
181
+ else if (authenticationState instanceof AuthenticationState .Deferred deferred )
182
+ auth = deferred .authenticate (request );
183
+ return auth ;
128
184
}
129
185
130
186
private static class AuthTestHandler extends Handler .Abstract
@@ -139,51 +195,67 @@ else if (pathInContext.startsWith("/logout"))
139
195
return onLogout (request , response , callback );
140
196
else if (pathInContext .startsWith ("/login/form" ))
141
197
return onFormLogin (request , response , callback );
198
+ else if (pathInContext .startsWith ("/login/openid" ))
199
+ return onOpenIdLogin (request , response , callback );
142
200
143
201
try (PrintWriter writer = new PrintWriter (Content .Sink .asOutputStream (response )))
144
202
{
145
- AuthenticationState authenticationState = AuthenticationState . getAuthenticationState ( request );
203
+
146
204
response .getHeaders ().put (HttpHeader .CONTENT_TYPE , "text/html" );
147
- writer . println ( "<b>authState: " + authenticationState + "</b><br>" );
148
- if (authenticationState instanceof AuthenticationState . Deferred deferred )
205
+ AuthenticationState . Succeeded auth = getAuthentication ( request );
206
+ if (auth != null )
149
207
{
150
- AuthenticationState .Succeeded succeeded = deferred .authenticate (request );
151
- if (succeeded != null )
152
- writer .println ("<b>userPrincipal: " + succeeded .getUserPrincipal () + "</b><br>" );
153
- else
154
- writer .println ("<b>userPrincipal: null</b><br>" );
155
- }
156
- else if (authenticationState != null )
157
- {
158
- writer .println ("<b>userPrincipal: " + authenticationState .getUserPrincipal () + "</b><br>" );
159
- }
208
+ writer .println ("<b>authState: " + auth + "</b><br>" );
209
+ writer .println ("<b>userPrincipal: " + auth .getUserPrincipal () + "</b><br>" );
160
210
161
- Session session = request .getSession (true );
162
- @ SuppressWarnings ("unchecked" )
163
- Map <String , Object > claims = (Map <String , Object >)session .getAttribute (OpenIdAuthenticator .CLAIMS );
164
- if (claims != null )
211
+ Session session = request .getSession (true );
212
+ @ SuppressWarnings ("unchecked" )
213
+ Map <String , Object > claims = (Map <String , Object >)session .getAttribute (OpenIdAuthenticator .CLAIMS );
214
+ if (claims != null )
215
+ {
216
+ writer .printf ("""
217
+ <br><b>Authenticated with OpenID</b><br>
218
+ userId: %s<br>
219
+ name: %s<br>
220
+ email: %s<br>
221
+ """ , claims .get ("sub" ), claims .get ("name" ), claims .get ("email" ));
222
+ }
223
+
224
+ writer .println ("""
225
+ <hr>
226
+ <a href="/logout">Logout</a><br>
227
+ """ );
228
+ }
229
+ else
165
230
{
166
- writer .printf ("""
167
- <br><b>Authenticated with OpenID</b><br >
168
- userId: %s <br>
169
- name: %s <br>
170
- email: %s <br>
171
- """ , claims . get ( "sub" ), claims . get ( "name" ), claims . get ( "email" ) );
231
+ writer .println ("""
232
+ <h1>Multi Login Page</h1 >
233
+ <a href="/login/openid">OpenID Login</a> <br>
234
+ <a href="/login/form">Form Login</a> <br>
235
+ <a href="/logout">Logout</a> <br>
236
+ """ );
172
237
}
173
-
174
- writer .println ("""
175
- <a href="/login/openid">OpenID Login</a><br>
176
- <a href="/login/form">Form Login</a><br>
177
- <a href="/logout">Logout</a><br>
178
- """ );
179
238
}
180
239
181
240
callback .succeeded ();
182
241
return true ;
183
242
}
184
243
244
+ private boolean onOpenIdLogin (Request request , Response response , Callback callback ) throws Exception
245
+ {
246
+ Response .sendRedirect (request , response , callback , "/" );
247
+ return true ;
248
+ }
249
+
185
250
private boolean onFormLogin (Request request , Response response , Callback callback ) throws Exception
186
251
{
252
+ AuthenticationState .Succeeded authentication = getAuthentication (request );
253
+ if (authentication != null )
254
+ {
255
+ Response .sendRedirect (request , response , callback , "/" );
256
+ return true ;
257
+ }
258
+
187
259
String content = """
188
260
<h2>Login</h2>
189
261
<form action="j_security_check" method="POST">
@@ -199,6 +271,8 @@ private boolean onFormLogin(Request request, Response response, Callback callbac
199
271
<button type="submit">Login</button>
200
272
</div>
201
273
</form>
274
+ <p>Username: user or admin<br>
275
+ Password: password</p>
202
276
""" ;
203
277
response .write (true , BufferUtil .toBuffer (content ), callback );
204
278
return true ;
0 commit comments