|
|
|
@@ -3,13 +3,14 @@ package com.ffii.lioner.modules.lioner.web; |
|
|
|
import dev.samstevens.totp.code.CodeVerifier; |
|
|
|
import dev.samstevens.totp.qr.QrData; |
|
|
|
import dev.samstevens.totp.secret.SecretGenerator; |
|
|
|
|
|
|
|
import jakarta.annotation.security.PermitAll; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.security.core.Authentication; |
|
|
|
import org.springframework.security.core.userdetails.UserDetails; |
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
@@ -99,6 +100,28 @@ public class TwoFactorController { |
|
|
|
abilities.add(auth.get("authority").toString()) |
|
|
|
); |
|
|
|
|
|
|
|
System.out.println("Validating code: " + code + " for user: " + username); |
|
|
|
System.out.println("Secret: " + user.getTwoFactorSecret()); |
|
|
|
boolean valid = codeVerifier.isValidCode(user.getTwoFactorSecret(), code); |
|
|
|
System.out.println("Validation result: " + valid); |
|
|
|
|
|
|
|
return ResponseEntity.ok(new JwtResponse(accessToken, refreshToken, null, user, abilities)); |
|
|
|
} |
|
|
|
|
|
|
|
@GetMapping("/status") |
|
|
|
public ResponseEntity<Map<String, Boolean>> get2FAStatus(Authentication authentication) { |
|
|
|
User user = userService.getCurrentUser(authentication); |
|
|
|
return ResponseEntity.ok(Map.of("enabled", user.isTwoFactorEnabled())); |
|
|
|
} |
|
|
|
|
|
|
|
@PostMapping("/disable") |
|
|
|
public ResponseEntity<Map<String, String>> disable2FA(Authentication authentication) { |
|
|
|
User user = userService.getCurrentUser(authentication); |
|
|
|
|
|
|
|
user.setTwoFactorSecret(null); |
|
|
|
user.setTwoFactorEnabled(false); |
|
|
|
userService.save(user); |
|
|
|
|
|
|
|
return ResponseEntity.ok(Map.of("message", "2FA disabled successfully")); |
|
|
|
} |
|
|
|
} |