|
22 | 22 | import com.google.apphosting.api.ApiProxy; |
23 | 23 | import com.google.common.flogger.GoogleLogger; |
24 | 24 | import java.io.IOException; |
25 | | -import java.security.Principal; |
26 | 25 | import java.util.Arrays; |
27 | 26 | import java.util.HashSet; |
28 | 27 | import java.util.function.Function; |
29 | | -import javax.security.auth.Subject; |
30 | 28 | import javax.servlet.ServletRequest; |
31 | 29 | import javax.servlet.ServletResponse; |
32 | 30 | import javax.servlet.http.HttpServletRequest; |
33 | 31 | import javax.servlet.http.HttpServletResponse; |
34 | 32 | import javax.servlet.http.HttpSession; |
35 | 33 | import org.eclipse.jetty.ee8.nested.Authentication; |
36 | | -import org.eclipse.jetty.ee8.security.Authenticator; |
37 | 34 | import org.eclipse.jetty.ee8.security.ConstraintSecurityHandler; |
38 | | -import org.eclipse.jetty.ee8.security.SecurityHandler; |
39 | 35 | import org.eclipse.jetty.ee8.security.ServerAuthException; |
40 | 36 | import org.eclipse.jetty.ee8.security.UserAuthentication; |
41 | 37 | import org.eclipse.jetty.ee8.security.authentication.DeferredAuthentication; |
@@ -77,9 +73,10 @@ public class AppEngineAuthentication { |
77 | 73 | * Any authenticated user is a member of the {@code "*"} role, and any administrators are members |
78 | 74 | * of the {@code "admin"} role. Any other roles will be logged and ignored. |
79 | 75 | */ |
80 | | - private static final String USER_ROLE = "*"; |
81 | 76 |
|
82 | | - private static final String ADMIN_ROLE = "admin"; |
| 77 | + static final String USER_ROLE = "*"; |
| 78 | + |
| 79 | + static final String ADMIN_ROLE = "admin"; |
83 | 80 |
|
84 | 81 | /** |
85 | 82 | * Inject custom {@link LoginService} and {@link Authenticator} implementations into the specified |
@@ -245,170 +242,5 @@ private static String getFullURL(HttpServletRequest request) { |
245 | 242 | return buffer.toString(); |
246 | 243 | } |
247 | 244 |
|
248 | | - /** |
249 | | - * {@code AppEngineLoginService} is a custom Jetty {@link LoginService} that is aware of the two |
250 | | - * special role names implemented by Google App Engine. Any authenticated user is a member of the |
251 | | - * {@code "*"} role, and any administrators are members of the {@code "admin"} role. Any other |
252 | | - * roles will be logged and ignored. |
253 | | - */ |
254 | | - private static class AppEngineLoginService implements LoginService { |
255 | | - private IdentityService identityService; |
256 | | - |
257 | | - /** |
258 | | - * @return Get the name of the login service (aka Realm name) |
259 | | - */ |
260 | | - @Override |
261 | | - public String getName() { |
262 | | - return REALM_NAME; |
263 | | - } |
264 | | - |
265 | | - @Override |
266 | | - public UserIdentity login( |
267 | | - String s, Object o, Request request, Function<Boolean, Session> function) { |
268 | | - return loadUser(); |
269 | | - } |
270 | | - |
271 | | - /** |
272 | | - * Creates a new AppEngineUserIdentity based on information retrieved from the Users API. |
273 | | - * |
274 | | - * @return A AppEngineUserIdentity if a user is logged in, or null otherwise. |
275 | | - */ |
276 | | - private AppEngineUserIdentity loadUser() { |
277 | | - UserService userService = UserServiceFactory.getUserService(); |
278 | | - User engineUser = userService.getCurrentUser(); |
279 | | - if (engineUser == null) { |
280 | | - return null; |
281 | | - } |
282 | | - return new AppEngineUserIdentity(new AppEnginePrincipal(engineUser)); |
283 | | - } |
284 | | - |
285 | | - @Override |
286 | | - public IdentityService getIdentityService() { |
287 | | - return identityService; |
288 | | - } |
289 | | - |
290 | | - @Override |
291 | | - public void logout(UserIdentity user) { |
292 | | - // Jetty calls this on every request -- even if user is null! |
293 | | - if (user != null) { |
294 | | - logger.atFine().log("Ignoring logout call for: %s", user); |
295 | | - } |
296 | | - } |
297 | | - |
298 | | - @Override |
299 | | - public void setIdentityService(IdentityService identityService) { |
300 | | - this.identityService = identityService; |
301 | | - } |
302 | | - |
303 | | - @Override |
304 | | - public boolean validate(UserIdentity user) { |
305 | | - logger.atInfo().log("validate(%s) throwing UnsupportedOperationException.", user); |
306 | | - throw new UnsupportedOperationException(); |
307 | | - } |
308 | | - } |
309 | | - |
310 | | - /** |
311 | | - * {@code AppEnginePrincipal} is an implementation of {@link Principal} that represents a |
312 | | - * logged-in Google App Engine user. |
313 | | - */ |
314 | | - public static class AppEnginePrincipal implements Principal { |
315 | | - private final User user; |
316 | | - |
317 | | - public AppEnginePrincipal(User user) { |
318 | | - this.user = user; |
319 | | - } |
320 | | - |
321 | | - public User getUser() { |
322 | | - return user; |
323 | | - } |
324 | | - |
325 | | - @Override |
326 | | - public String getName() { |
327 | | - if ((user.getFederatedIdentity() != null) && (!user.getFederatedIdentity().isEmpty())) { |
328 | | - return user.getFederatedIdentity(); |
329 | | - } |
330 | | - return user.getEmail(); |
331 | | - } |
332 | | - |
333 | | - @Override |
334 | | - public boolean equals(Object other) { |
335 | | - if (other instanceof AppEnginePrincipal) { |
336 | | - return user.equals(((AppEnginePrincipal) other).user); |
337 | | - } else { |
338 | | - return false; |
339 | | - } |
340 | | - } |
341 | | - |
342 | | - @Override |
343 | | - public String toString() { |
344 | | - return user.toString(); |
345 | | - } |
346 | | - |
347 | | - @Override |
348 | | - public int hashCode() { |
349 | | - return user.hashCode(); |
350 | | - } |
351 | | - } |
352 | | - |
353 | | - /** |
354 | | - * {@code AppEngineUserIdentity} is an implementation of {@link UserIdentity} that represents a |
355 | | - * logged-in Google App Engine user. |
356 | | - */ |
357 | | - public static class AppEngineUserIdentity implements UserIdentity { |
358 | | - |
359 | | - private final AppEnginePrincipal userPrincipal; |
360 | | - |
361 | | - public AppEngineUserIdentity(AppEnginePrincipal userPrincipal) { |
362 | | - this.userPrincipal = userPrincipal; |
363 | | - } |
364 | | - |
365 | | - /* |
366 | | - * Only used by jaas and jaspi. |
367 | | - */ |
368 | | - @Override |
369 | | - public Subject getSubject() { |
370 | | - logger.atInfo().log("getSubject() throwing UnsupportedOperationException."); |
371 | | - throw new UnsupportedOperationException(); |
372 | | - } |
373 | | - |
374 | | - @Override |
375 | | - public Principal getUserPrincipal() { |
376 | | - return userPrincipal; |
377 | | - } |
378 | | - |
379 | | - @Override |
380 | | - public boolean isUserInRole(String role) { |
381 | | - UserService userService = UserServiceFactory.getUserService(); |
382 | | - logger.atFine().log("Checking if principal %s is in role %s", userPrincipal, role); |
383 | | - if (userPrincipal == null) { |
384 | | - logger.atInfo().log("isUserInRole() called with null principal."); |
385 | | - return false; |
386 | | - } |
387 | | - |
388 | | - if (USER_ROLE.equals(role)) { |
389 | | - return true; |
390 | | - } |
391 | | - |
392 | | - if (ADMIN_ROLE.equals(role)) { |
393 | | - User user = userPrincipal.getUser(); |
394 | | - if (user.equals(userService.getCurrentUser())) { |
395 | | - return userService.isUserAdmin(); |
396 | | - } else { |
397 | | - // TODO: I'm not sure this will happen in |
398 | | - // practice. If it does, we may need to pass an |
399 | | - // application's admin list down somehow. |
400 | | - logger.atSevere().log("Cannot tell if non-logged-in user %s is an admin.", user); |
401 | | - return false; |
402 | | - } |
403 | | - } else { |
404 | | - logger.atWarning().log("Unknown role: %s.", role); |
405 | | - return false; |
406 | | - } |
407 | | - } |
408 | | - |
409 | | - @Override |
410 | | - public String toString() { |
411 | | - return AppEngineUserIdentity.class.getSimpleName() + "('" + userPrincipal + "')"; |
412 | | - } |
413 | | - } |
| 245 | + private AppEngineAuthentication() {} |
414 | 246 | } |
0 commit comments