Skip to content

Commit effa99d

Browse files
committed
fix(tfa): extend expiration time for TFA setup state and improve error handling
1 parent 42300da commit effa99d

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

backend/src/main/java/com/park/utmstack/service/tfa/EmailTfaService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void generateChallenge(User user) {
8686
String secret = user.getTfaSecret();
8787
String code = tfaService.generateCode(secret);
8888

89-
TfaSetupState state = new TfaSetupState(secret, System.currentTimeMillis() + Constants.EXPIRES_IN_SECONDS * 1000);
89+
TfaSetupState state = new TfaSetupState(secret, System.currentTimeMillis() + Constants.EXPIRES_IN_SECONDS * 1000 * 10);
9090
cache.storeState(user.getLogin(), TfaMethod.EMAIL, state);
9191

9292
mailService.sendTfaVerificationCode(user, code);

backend/src/main/java/com/park/utmstack/service/tfa/TotpTfaService.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ public TfaVerifyResponse verifyCode(User user, String code) {
6464

6565
boolean expired = tfaSetupState.isExpired();
6666
boolean valid = !expired && authenticator.authorize(tfaSetupState.getSecret(), Integer.parseInt(code)) && !code.equals(tfaSetupState.getLastUsedCode());
67-
68-
if(expired){
69-
tfaSetupState.setLastUsedCode(code);
70-
tfaSetupState.setExpiresAt(System.currentTimeMillis() + Constants.EXPIRES_IN_SECONDS * 1000);
71-
cache.storeState(user.getLogin(), TfaMethod.TOTP, tfaSetupState);
72-
}
73-
7467
return new TfaVerifyResponse(
7568
valid,
7669
expired,
@@ -91,8 +84,9 @@ public void persistConfiguration(User user) throws Exception {
9184

9285
@Override
9386
public void generateChallenge(User user) {
87+
cache.clear(user.getLogin(), TfaMethod.TOTP);
9488
String secret = user.getTfaSecret();
95-
TfaSetupState state = new TfaSetupState(secret, System.currentTimeMillis() + Constants.EXPIRES_IN_SECONDS * 1000);
89+
TfaSetupState state = new TfaSetupState(secret, System.currentTimeMillis() + (Constants.EXPIRES_IN_SECONDS + 10) * 1000);
9690
cache.storeState(user.getLogin(), TfaMethod.TOTP, state);
9791
}
9892

backend/src/main/java/com/park/utmstack/web/rest/tfa/TfaController.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
3232
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3333
import org.springframework.web.bind.annotation.*;
34+
import tech.jhipster.web.util.ResponseUtil;
3435

3536
import java.util.List;
3637
import java.util.stream.Collectors;
@@ -62,7 +63,7 @@ public ResponseEntity<TfaInitResponse> initTfa(@RequestBody TfaInitRequest reque
6263
String msg = ctx + ": " + e.getMessage();
6364
log.error(msg);
6465
applicationEventService.createEvent(msg, ApplicationEventType.ERROR);
65-
throw e;
66+
return UtilResponse.buildInternalServerErrorResponse(msg);
6667
}
6768

6869
}
@@ -74,11 +75,25 @@ public ResponseEntity<TfaVerifyResponse> verifyTfa(@RequestBody TfaVerifyRequest
7475
User user = userService.getCurrentUserLogin();
7576
TfaVerifyResponse response = tfaService.verifyCode(user, request);
7677
return ResponseEntity.ok(response);
78+
} catch (Exception e) {
79+
String msg = ctx + ": " + e.getMessage();
80+
log.error(msg);
81+
return UtilResponse.buildInternalServerErrorResponse(msg);
82+
}
83+
}
84+
85+
@GetMapping("/generate-challenge")
86+
public ResponseEntity<Void> generateChallenge() {
87+
final String ctx = CLASSNAME + ".generateChallenge";
88+
try {
89+
User user = userService.getCurrentUserLogin();
90+
tfaService.generateChallenge(user);
91+
return ResponseEntity.ok().build();
7792
} catch (Exception e) {
7893
String msg = ctx + ": " + e.getMessage();
7994
log.error(msg);
8095
applicationEventService.createEvent(msg, ApplicationEventType.ERROR);
81-
throw e;
96+
return UtilResponse.buildInternalServerErrorResponse(msg);
8297
}
8398
}
8499

@@ -100,10 +115,6 @@ public ResponseEntity<Void> completeTfa(@RequestBody TfaSaveRequest request) {
100115
}
101116
}
102117

103-
104-
105-
106-
107118
tfaService.persistConfiguration(request.getMethod());
108119
User user = userService.getCurrentUserLogin();
109120
utmConfigurationParameterService.saveAllConfigParams(tfaParams);

0 commit comments

Comments
 (0)