@@ -306,31 +306,48 @@ private void savePreliminaryVcsAccessLogForHTTPs(HttpServletRequest request, Loc
306
306
*/
307
307
public void saveFailedAccessVcsAccessLog (Optional <HttpServletRequest > request , Optional <ServerSession > session , String repositoryTypeOrUserName , Exercise exercise ,
308
308
LocalVCRepositoryUri localVCRepositoryUri , User user , RepositoryActionType repositoryAction ) {
309
- String ipAddress = "" ;
310
- AuthenticationMechanism authenticationMechanism = AuthenticationMechanism .OTHER ;
311
309
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
+ }
312
316
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 ()) {
316
341
try {
317
- authenticationMechanism = resolveHTTPSAuthenticationMechanism (request .get ().getHeader (LocalVCServletService .AUTHORIZATION_HEADER ), user );
342
+ return resolveHTTPSAuthenticationMechanism (request .get ().getHeader (LocalVCServletService .AUTHORIZATION_HEADER ), user );
318
343
}
319
344
catch (LocalVCAuthException ignored ) {
320
- authenticationMechanism = AuthenticationMechanism .HTTPS ;
345
+ return AuthenticationMechanism .HTTPS ;
321
346
}
322
347
}
323
- // SSH
324
- if (session .isPresent ()) {
325
- ipAddress = session .get ().getClientAddress ().toString ();
326
- authenticationMechanism = AuthenticationMechanism .SSH ;
348
+ else {
349
+ return AuthenticationMechanism .OTHER ;
327
350
}
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 ));
334
351
}
335
352
336
353
/**
0 commit comments