| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  | package controllers;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  | import auth.sso.SsoManager;
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:54:34 -07:00
										 |  |  | import java.net.URLEncoder;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  | import java.nio.charset.StandardCharsets;
 | 
					
						
							|  |  |  | import javax.inject.Inject;
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:54:34 -07:00
										 |  |  | import lombok.extern.slf4j.Slf4j;
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  | import org.pac4j.play.LogoutController;
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  | import play.mvc.Http;
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  | import play.mvc.Result;
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  | import play.mvc.Results;
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  | /** Responsible for handling logout logic with oidc providers */
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:54:34 -07:00
										 |  |  | @Slf4j
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  | public class CentralLogoutController extends LogoutController {
 | 
					
						
							| 
									
										
										
										
											2023-09-03 04:37:26 +03:00
										 |  |  |   private static final String AUTH_URL_CONFIG_PATH = "/login";
 | 
					
						
							|  |  |  |   private static final String DEFAULT_BASE_URL_PATH = "/";
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |   @Inject private SsoManager ssoManager;
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |   public CentralLogoutController() {
 | 
					
						
							|  |  |  |     setDefaultUrl(AUTH_URL_CONFIG_PATH);
 | 
					
						
							| 
									
										
										
										
											2022-10-31 16:39:26 -07:00
										 |  |  |     setLogoutUrlPattern(DEFAULT_BASE_URL_PATH + ".*");
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  |     setLocalLogout(true);
 | 
					
						
							|  |  |  |     setCentralLogout(true);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |   /** logout() method should not be called if oidc is not enabled */
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |   public Result executeLogout(Http.Request request) {
 | 
					
						
							| 
									
										
										
										
											2024-10-28 09:05:16 -05:00
										 |  |  |     if (ssoManager.isSsoEnabled()) {
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:54:34 -07:00
										 |  |  |       try {
 | 
					
						
							| 
									
										
										
										
											2023-09-03 04:37:26 +03:00
										 |  |  |         return logout(request).toCompletableFuture().get().withNewSession();
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:54:34 -07:00
										 |  |  |       } catch (Exception e) {
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |         log.error(
 | 
					
						
							|  |  |  |             "Caught exception while attempting to perform SSO logout! It's likely that SSO integration is mis-configured.",
 | 
					
						
							|  |  |  |             e);
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:54:34 -07:00
										 |  |  |         return redirect(
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |                 String.format(
 | 
					
						
							|  |  |  |                     "/login?error_msg=%s",
 | 
					
						
							|  |  |  |                     URLEncoder.encode(
 | 
					
						
							|  |  |  |                         "Failed to sign out using Single Sign-On provider. Please contact your DataHub Administrator, "
 | 
					
						
							|  |  |  |                             + "or refer to server logs for more information.",
 | 
					
						
							|  |  |  |                         StandardCharsets.UTF_8)))
 | 
					
						
							|  |  |  |             .withNewSession();
 | 
					
						
							| 
									
										
										
										
											2022-08-23 09:54:34 -07:00
										 |  |  |       }
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:02:42 +05:30
										 |  |  |     return Results.redirect(AUTH_URL_CONFIG_PATH).withNewSession();
 | 
					
						
							| 
									
										
										
										
											2021-08-16 12:19:44 +07:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2023-01-19 16:58:25 -08:00
										 |  |  | }
 |