2021-08-20 10:58:07 -07:00
|
|
|
package auth.sso.oidc;
|
2021-08-11 07:28:28 +02:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
import static play.mvc.Results.internalServerError;
|
|
|
|
import static play.mvc.Results.unauthorized;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
2024-10-28 09:05:16 -05:00
|
|
|
import org.pac4j.core.context.CallContext;
|
|
|
|
import org.pac4j.core.context.WebContext;
|
2021-08-11 07:28:28 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import play.mvc.Result;
|
|
|
|
|
|
|
|
public class OidcResponseErrorHandler {
|
2022-05-10 18:15:53 -05:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
private OidcResponseErrorHandler() {}
|
2021-08-11 07:28:28 +02:00
|
|
|
|
2024-10-28 09:05:16 -05:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger("OidcResponseErrorHandler");
|
2021-08-11 07:28:28 +02:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
private static final String ERROR_FIELD_NAME = "error";
|
|
|
|
private static final String ERROR_DESCRIPTION_FIELD_NAME = "error_description";
|
2021-08-11 07:28:28 +02:00
|
|
|
|
2024-10-28 09:05:16 -05:00
|
|
|
public static Result handleError(final CallContext ctx) {
|
|
|
|
WebContext context = ctx.webContext();
|
|
|
|
logger.warn(
|
2023-12-06 11:02:42 +05:30
|
|
|
"OIDC responded with an error: '{}'. Error description: '{}'",
|
|
|
|
getError(context),
|
|
|
|
getErrorDescription(context));
|
2021-08-11 07:28:28 +02:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
if (getError(context).isPresent() && getError(context).get().equals("access_denied")) {
|
|
|
|
return unauthorized(
|
|
|
|
String.format(
|
|
|
|
"Access denied. "
|
|
|
|
+ "The OIDC service responded with 'Access denied'. "
|
|
|
|
+ "It seems that you don't have access to this application yet. Please apply for access. \n\n"
|
|
|
|
+ "If you already have been assigned this application, it may be so that your OIDC request is still in action. "
|
|
|
|
+ "Error details: '%s':'%s'",
|
|
|
|
context.getRequestParameter("error"),
|
|
|
|
context.getRequestParameter("error_description")));
|
2021-08-11 07:28:28 +02:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
return internalServerError(
|
|
|
|
String.format(
|
|
|
|
"Internal server error. The OIDC service responded with an error: '%s'.\n"
|
|
|
|
+ "Error description: '%s'",
|
|
|
|
getError(context).orElse(""), getErrorDescription(context).orElse("")));
|
|
|
|
}
|
2021-08-11 07:28:28 +02:00
|
|
|
|
2024-10-28 09:05:16 -05:00
|
|
|
public static boolean isError(final CallContext ctx) {
|
|
|
|
return getError(ctx.webContext()).isPresent() && !getError(ctx.webContext()).get().isEmpty();
|
2023-12-06 11:02:42 +05:30
|
|
|
}
|
2021-08-11 07:28:28 +02:00
|
|
|
|
2024-10-28 09:05:16 -05:00
|
|
|
public static Optional<String> getError(final WebContext context) {
|
2023-12-06 11:02:42 +05:30
|
|
|
return context.getRequestParameter(ERROR_FIELD_NAME);
|
|
|
|
}
|
|
|
|
|
2024-10-28 09:05:16 -05:00
|
|
|
public static Optional<String> getErrorDescription(final WebContext context) {
|
2023-12-06 11:02:42 +05:30
|
|
|
return context.getRequestParameter(ERROR_DESCRIPTION_FIELD_NAME);
|
|
|
|
}
|
2021-08-11 07:28:28 +02:00
|
|
|
}
|