2021-08-16 12:19:44 +07:00
|
|
|
package controllers;
|
|
|
|
|
|
|
|
import com.typesafe.config.Config;
|
2022-08-23 09:54:34 -07:00
|
|
|
import java.net.URLEncoder;
|
|
|
|
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
|
|
|
|
|
|
|
import javax.inject.Inject;
|
2022-12-08 20:27:51 -06:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-08-16 12:19:44 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2022-10-31 16:39:26 -07:00
|
|
|
private static final String DEFAULT_BASE_URL_PATH = "/login";
|
2021-08-16 12:19:44 +07:00
|
|
|
private static Boolean _isOidcEnabled = false;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
public CentralLogoutController(Config config) {
|
|
|
|
_isOidcEnabled = config.hasPath("auth.oidc.enabled") && config.getBoolean("auth.oidc.enabled");
|
|
|
|
|
2022-10-31 16:39:26 -07:00
|
|
|
setDefaultUrl(DEFAULT_BASE_URL_PATH);
|
|
|
|
setLogoutUrlPattern(DEFAULT_BASE_URL_PATH + ".*");
|
2021-08-16 12:19:44 +07:00
|
|
|
setLocalLogout(true);
|
|
|
|
setCentralLogout(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2021-08-16 12:19:44 +07:00
|
|
|
if (_isOidcEnabled) {
|
2022-08-23 09:54:34 -07:00
|
|
|
try {
|
2022-12-08 20:27:51 -06:00
|
|
|
return Results.redirect(DEFAULT_BASE_URL_PATH)
|
|
|
|
.removingFromSession(request);
|
2022-08-23 09:54:34 -07:00
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("Caught exception while attempting to perform SSO logout! It's likely that SSO integration is mis-configured.", e);
|
|
|
|
return redirect(
|
|
|
|
String.format("/login?error_msg=%s",
|
|
|
|
URLEncoder.encode("Failed to sign out using Single Sign-On provider. Please contact your DataHub Administrator, "
|
2022-12-08 20:27:51 -06:00
|
|
|
+ "or refer to server logs for more information.", StandardCharsets.UTF_8)));
|
2022-08-23 09:54:34 -07:00
|
|
|
}
|
2021-08-16 12:19:44 +07:00
|
|
|
}
|
2022-12-08 20:27:51 -06:00
|
|
|
return Results.redirect(DEFAULT_BASE_URL_PATH)
|
|
|
|
.withNewSession();
|
2021-08-16 12:19:44 +07:00
|
|
|
}
|
|
|
|
}
|