Skip to content

Commit

Permalink
add custom exception
Browse files Browse the repository at this point in the history
  • Loading branch information
basak-akan committed Oct 6, 2023
1 parent dbb9a44 commit 1787912
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1787912

Please sign in to comment.