Skip to content

Commit 89c9b92

Browse files
make method more compact
1 parent c655f7f commit 89c9b92

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

src/main/java/de/tum/cit/aet/artemis/programming/service/localvc/LocalVCServletService.java

+34-17
Original file line numberDiff line numberDiff line change
@@ -306,31 +306,48 @@ private void savePreliminaryVcsAccessLogForHTTPs(HttpServletRequest request, Loc
306306
*/
307307
public void saveFailedAccessVcsAccessLog(Optional<HttpServletRequest> request, Optional<ServerSession> session, String repositoryTypeOrUserName, Exercise exercise,
308308
LocalVCRepositoryUri localVCRepositoryUri, User user, RepositoryActionType repositoryAction) {
309-
String ipAddress = "";
310-
AuthenticationMechanism authenticationMechanism = AuthenticationMechanism.OTHER;
311309
var participation = tryToLoadParticipation(false, repositoryTypeOrUserName, localVCRepositoryUri, (ProgrammingExercise) exercise);
310+
var commitHash = getCommitHash(localVCRepositoryUri);
311+
var authenticationMechanism = resolveAuthenticationMechanismFromSessionOrRequest(request, session, user);
312+
var action = repositoryAction == RepositoryActionType.WRITE ? RepositoryActionType.PUSH_FAIL : RepositoryActionType.CLONE_FAIL;
313+
var ipAddress = request.isPresent() ? request.get().getRemoteAddr() : (session.isPresent() ? session.get().getClientAddress().toString() : "");
314+
vcsAccessLogService.ifPresent(service -> service.saveAccessLog(user, participation, action, authenticationMechanism, commitHash, ipAddress));
315+
}
312316

313-
// HTTPS
314-
if (request.isPresent()) {
315-
ipAddress = request.get().getRemoteAddr();
317+
/**
318+
* Determines the authentication mechanism based on the provided session or request.
319+
*
320+
* <p>
321+
* If a {@link ServerSession} is present, the authentication mechanism is assumed to be SSH.
322+
* </p>
323+
* <p>
324+
* If an {@link HttpServletRequest} is present, the method attempts to resolve the authentication
325+
* mechanism using the authorization header. If an exception occurs, HTTPS authentication is assumed by default.
326+
* </p>
327+
* <p>
328+
* If neither a session nor a request is available, the authentication mechanism defaults to OTHER.
329+
* </p>
330+
*
331+
* @param request an {@link Optional} containing the HTTP request, if available
332+
* @param session an {@link Optional} containing the server session, if available
333+
* @param user the user for whom authentication is being determined
334+
* @return the resolved {@link AuthenticationMechanism}
335+
*/
336+
private AuthenticationMechanism resolveAuthenticationMechanismFromSessionOrRequest(Optional<HttpServletRequest> request, Optional<ServerSession> session, User user) {
337+
if (session.isPresent()) {
338+
return AuthenticationMechanism.SSH;
339+
}
340+
else if (request.isPresent()) {
316341
try {
317-
authenticationMechanism = resolveHTTPSAuthenticationMechanism(request.get().getHeader(LocalVCServletService.AUTHORIZATION_HEADER), user);
342+
return resolveHTTPSAuthenticationMechanism(request.get().getHeader(LocalVCServletService.AUTHORIZATION_HEADER), user);
318343
}
319344
catch (LocalVCAuthException ignored) {
320-
authenticationMechanism = AuthenticationMechanism.HTTPS;
345+
return AuthenticationMechanism.HTTPS;
321346
}
322347
}
323-
// SSH
324-
if (session.isPresent()) {
325-
ipAddress = session.get().getClientAddress().toString();
326-
authenticationMechanism = AuthenticationMechanism.SSH;
348+
else {
349+
return AuthenticationMechanism.OTHER;
327350
}
328-
329-
String finalCommitHash = getCommitHash(localVCRepositoryUri);
330-
var finalAuthenticationMechanism = authenticationMechanism;
331-
var finalIpAddress = ipAddress;
332-
RepositoryActionType finalRepositoryAction = repositoryAction == RepositoryActionType.WRITE ? RepositoryActionType.PUSH_FAIL : RepositoryActionType.CLONE_FAIL;
333-
vcsAccessLogService.ifPresent(service -> service.saveAccessLog(user, participation, finalRepositoryAction, finalAuthenticationMechanism, finalCommitHash, finalIpAddress));
334351
}
335352

336353
/**

0 commit comments

Comments
 (0)