From 17879128bd568d4a36ff01da9d6d0bc41bd909d7 Mon Sep 17 00:00:00 2001 From: Basak Akan Date: Fri, 6 Oct 2023 15:28:41 +0200 Subject: [PATCH] add custom exception --- .../exception/LtiEmailAlreadyInUseException.java | 10 ++++++++++ .../www1/artemis/security/lti/Lti13LaunchFilter.java | 6 +++--- .../artemis/service/connectors/lti/LtiService.java | 3 ++- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/tum/in/www1/artemis/exception/LtiEmailAlreadyInUseException.java diff --git a/src/main/java/de/tum/in/www1/artemis/exception/LtiEmailAlreadyInUseException.java b/src/main/java/de/tum/in/www1/artemis/exception/LtiEmailAlreadyInUseException.java new file mode 100644 index 000000000000..e5414b5bdc8c --- /dev/null +++ b/src/main/java/de/tum/in/www1/artemis/exception/LtiEmailAlreadyInUseException.java @@ -0,0 +1,10 @@ +package de.tum.in.www1.artemis.exception; + +import org.springframework.security.authentication.AuthenticationServiceException; + +public class LtiEmailAlreadyInUseException extends AuthenticationServiceException { + + public LtiEmailAlreadyInUseException() { + super("Email address is already in use by Artemis. Please login again to access Artemis content."); + } +} diff --git a/src/main/java/de/tum/in/www1/artemis/security/lti/Lti13LaunchFilter.java b/src/main/java/de/tum/in/www1/artemis/security/lti/Lti13LaunchFilter.java index 303862f593e3..2b590a5ea542 100644 --- a/src/main/java/de/tum/in/www1/artemis/security/lti/Lti13LaunchFilter.java +++ b/src/main/java/de/tum/in/www1/artemis/security/lti/Lti13LaunchFilter.java @@ -11,7 +11,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Profile; -import org.springframework.security.authentication.InternalAuthenticationServiceException; import org.springframework.security.oauth2.core.OAuth2AuthenticationException; import org.springframework.security.oauth2.core.oidc.OidcIdToken; import org.springframework.security.oauth2.core.oidc.user.OidcUser; @@ -21,6 +20,7 @@ import org.springframework.web.util.UriComponentsBuilder; import de.tum.in.www1.artemis.domain.lti.Claims; +import de.tum.in.www1.artemis.exception.LtiEmailAlreadyInUseException; import de.tum.in.www1.artemis.service.connectors.lti.Lti13Service; import net.minidev.json.JSONObject; import uk.ac.ox.ctl.lti13.security.oauth2.client.lti.authentication.OidcAuthenticationToken; @@ -71,8 +71,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse log.error("Error during LTI 1.3 launch request: {}", ex.getMessage()); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "LTI 1.3 Launch failed"); } - catch (InternalAuthenticationServiceException ex) { - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, ex.getMessage()); + catch (LtiEmailAlreadyInUseException ex) { + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "LTI 1.3 user authentication failed"); } } diff --git a/src/main/java/de/tum/in/www1/artemis/service/connectors/lti/LtiService.java b/src/main/java/de/tum/in/www1/artemis/service/connectors/lti/LtiService.java index b8819ce035c8..53899b59f1bd 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/connectors/lti/LtiService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/connectors/lti/LtiService.java @@ -28,6 +28,7 @@ import de.tum.in.www1.artemis.domain.Exercise; import de.tum.in.www1.artemis.domain.User; import de.tum.in.www1.artemis.exception.ArtemisAuthenticationException; +import de.tum.in.www1.artemis.exception.LtiEmailAlreadyInUseException; import de.tum.in.www1.artemis.repository.UserRepository; import de.tum.in.www1.artemis.security.ArtemisAuthenticationProvider; import de.tum.in.www1.artemis.security.Role; @@ -100,7 +101,7 @@ public void authenticateLtiUser(String email, String username, String firstName, // 2. Case: Lookup user with the LTI email address and make sure it's not in use final var usernameLookupByEmail = artemisAuthenticationProvider.getUsernameForEmail(email); if (usernameLookupByEmail.isPresent()) { - throw new InternalAuthenticationServiceException("Please login again as " + usernameLookupByEmail); + throw new LtiEmailAlreadyInUseException(); } // 3. Case: Create new user if an existing user is not required