77import com .park .utmstack .domain .User ;
88import com .park .utmstack .domain .application_events .enums .ApplicationEventType ;
99import com .park .utmstack .domain .federation_service .UtmFederationServiceClient ;
10+ import com .park .utmstack .loggin .LogContextBuilder ;
1011import com .park .utmstack .repository .federation_service .UtmFederationServiceClientRepository ;
1112import com .park .utmstack .security .TooMuchLoginAttemptsException ;
1213import com .park .utmstack .security .jwt .JWTFilter ;
3637import org .springframework .util .StringUtils ;
3738import org .springframework .web .bind .annotation .*;
3839
40+ import javax .servlet .http .HttpServletRequest ;
3941import javax .validation .Valid ;
4042import java .util .List ;
43+ import java .util .Map ;
4144import java .util .stream .Collectors ;
4245
4346/**
@@ -57,18 +60,17 @@ public class UserJWTController {
5760 private final MailService mailService ;
5861 private final LoginAttemptService loginAttemptService ;
5962 private final UtmFederationServiceClientRepository fsClientRepository ;
60- private final PasswordEncoder passwordEncoder ;
63+ private final LogContextBuilder logContextBuilder ;
6164
6265
6366 @ AuditEvent (
6467 attemptType = ApplicationEventType .AUTH_ATTEMPT ,
6568 attemptMessage = "Authentication attempt registered" ,
66- successType = ApplicationEventType .AUTH_SUCCESS ,
67- successMessage = "Authentication successful "
69+ successType = ApplicationEventType .UNDEFINED ,
70+ successMessage = ""
6871 )
6972 @ PostMapping ("/authenticate" )
70- public ResponseEntity <JWTToken > authorize (@ Valid @ RequestBody LoginVM loginVM ) {
71-
73+ public ResponseEntity <JWTToken > authorize (@ Valid @ RequestBody LoginVM loginVM , HttpServletRequest request ) {
7274
7375 if (loginAttemptService .isBlocked ()) {
7476 String ip = loginAttemptService .getClientIP ();
@@ -85,12 +87,24 @@ public ResponseEntity<JWTToken> authorize(@Valid @RequestBody LoginVM loginVM) {
8587 SecurityContextHolder .getContext ().setAuthentication (authentication );
8688
8789 String jwt = tokenProvider .createToken (authentication , rememberMe , authenticated );
90+ Map <String , Object > args = logContextBuilder .buildArgs (request );
8891
8992 if (!authenticated ) {
9093 String secret = tfaService .generateSecret ();
9194 String code = tfaService .generateCode (secret );
9295 User user = userService .updateUserTfaSecret (loginVM .getUsername (), secret );
96+
97+ applicationEventService .createEvent (
98+ "TFA challenge issued for user '" + user .getLogin (),
99+ ApplicationEventType .TFA_CODE_SENT ,
100+ args
101+ );
93102 mailService .sendTfaVerificationCode (user , code );
103+ } else {
104+ applicationEventService .createEvent (
105+ "Login successfully completed for user '" + loginVM .getUsername () + "'" ,
106+ ApplicationEventType .AUTH_SUCCESS ,
107+ args );
94108 }
95109
96110 HttpHeaders httpHeaders = new HttpHeaders ();
@@ -146,6 +160,12 @@ public ResponseEntity<JWTToken> authorizeFederationServiceManager(@Valid @Reques
146160
147161 }
148162
163+ @ AuditEvent (
164+ attemptType = ApplicationEventType .TFA_CODE_VERIFY_ATTEMPT ,
165+ attemptMessage = "Verification attempt for second-factor authentication" ,
166+ successType = ApplicationEventType .AUTH_SUCCESS ,
167+ successMessage = "Login successfully completed"
168+ )
149169 @ GetMapping ("/tfa/verifyCode" )
150170 public ResponseEntity <JWTToken > verifyCode (@ RequestParam String code ) {
151171
0 commit comments