|
| 1 | +package com.app.toaster.admin.controller; |
| 2 | + |
| 3 | +import com.app.toaster.admin.common.RedirectResponse; |
| 4 | +import com.app.toaster.admin.controller.dto.command.VerifyNewAdminCommand; |
| 5 | +import com.app.toaster.admin.controller.dto.request.AdminTotpDto; |
| 6 | +import com.app.toaster.admin.controller.dto.request.SignInDto; |
| 7 | +import com.app.toaster.admin.controller.dto.response.AdminResponse; |
| 8 | +import com.app.toaster.admin.entity.ToasterAdmin; |
| 9 | +import com.app.toaster.admin.service.AdminService; |
| 10 | +import com.app.toaster.admin.config.QrMfaAuthenticator; |
| 11 | +import com.app.toaster.exception.Error; |
| 12 | +import com.app.toaster.exception.Success; |
| 13 | +import com.app.toaster.exception.model.CustomException; |
| 14 | +import com.app.toaster.external.client.aws.S3Service; |
| 15 | +import com.app.toaster.external.client.discord.DiscordMessageProvider; |
| 16 | + |
| 17 | +import com.app.toaster.external.client.discord.NotificationDto; |
| 18 | +import com.app.toaster.external.client.discord.NotificationType; |
| 19 | + |
| 20 | +import jakarta.servlet.http.HttpServletResponse; |
| 21 | +import jakarta.servlet.http.HttpSession; |
| 22 | +import lombok.RequiredArgsConstructor; |
| 23 | +import org.springframework.stereotype.Controller; |
| 24 | +import org.springframework.ui.Model; |
| 25 | +import org.springframework.web.bind.annotation.*; |
| 26 | +import org.springframework.web.multipart.MultipartFile; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +@Controller |
| 31 | +@RequestMapping("/admin") |
| 32 | +@RequiredArgsConstructor |
| 33 | +class AdminController { |
| 34 | + |
| 35 | + private final DiscordMessageProvider discordMessageProvider; |
| 36 | + private final S3Service s3Service; |
| 37 | + private final QrMfaAuthenticator qrMfaAuthenticator; |
| 38 | + private final AdminService adminService; |
| 39 | + |
| 40 | + @PostMapping("/register") |
| 41 | + @ResponseBody |
| 42 | + public RedirectResponse<?> registerAdmin(@RequestBody SignInDto signInDto, HttpSession session) { |
| 43 | + |
| 44 | + VerifyNewAdminCommand res = adminService.registerAdmin(signInDto.username(), signInDto.password()); |
| 45 | + |
| 46 | + String key = res.key(); |
| 47 | + Long adminId = res.id(); |
| 48 | + boolean isNewAdmin = res.isNewAdmin(); |
| 49 | + |
| 50 | + if (isNewAdmin){ |
| 51 | + key = executeDiscordQrOperation(key); |
| 52 | + } |
| 53 | + |
| 54 | + session.setAttribute("VerifyId", adminId); |
| 55 | + session.setAttribute("QrUrl", key); |
| 56 | + |
| 57 | + return RedirectResponse.success(Success.LOGIN_SUCCESS, "verify",null); |
| 58 | + } |
| 59 | + |
| 60 | + @GetMapping("/register") |
| 61 | + public String getRegisterAdmin(Model model, HttpServletResponse response) throws IOException { |
| 62 | + return "basic/register"; |
| 63 | + } |
| 64 | + |
| 65 | + @GetMapping("/verify") |
| 66 | + public String responseIsAdminCodeView(Model model) { |
| 67 | + return "basic/qrForm"; |
| 68 | + } |
| 69 | + |
| 70 | + @GetMapping("/main") |
| 71 | + public String adminMain(Model model) { |
| 72 | +// model.addAttribute("imageUrl", imageUrl); // imageUrl을 모델에 추가 |
| 73 | + return "basic/admin"; |
| 74 | + } |
| 75 | + |
| 76 | + @PostMapping("/verify-code") |
| 77 | + @ResponseBody |
| 78 | + public RedirectResponse<AdminResponse> responseIsAdminView(HttpSession session, @RequestBody AdminTotpDto request) throws IOException { |
| 79 | + //admin인지 판단 |
| 80 | + Long verifyId = (Long) session.getAttribute("VerifyId"); |
| 81 | + |
| 82 | + if (verifyId == null) { |
| 83 | + throw new CustomException(Error.BAD_REQUEST_ID, "세션에 VerifyId가 없습니다."); |
| 84 | + } |
| 85 | + |
| 86 | + ToasterAdmin toasterAdmin = qrMfaAuthenticator.verifyGoogleTotpCode(Integer.valueOf(request.code()), verifyId); |
| 87 | + |
| 88 | + if (toasterAdmin == null){ |
| 89 | + throw new CustomException(Error.BAD_REQUEST_ID, "잘못된 유저 입니다."); |
| 90 | + } |
| 91 | + AdminResponse result = new AdminResponse(toasterAdmin.getUsername()); |
| 92 | + s3Service.deleteImage((String) session.getAttribute("QrUrl")); |
| 93 | + return RedirectResponse.success(Success.LOGIN_SUCCESS,"main", result); |
| 94 | + } |
| 95 | + |
| 96 | + private String executeDiscordQrOperation(String key){ |
| 97 | + MultipartFile qrImage = qrMfaAuthenticator.generateQrCode(key); |
| 98 | + String imageKey = s3Service.uploadImage(qrImage, "admin/"); |
| 99 | + String qrUrl = s3Service.getURL(imageKey); |
| 100 | + discordMessageProvider.sendAdmin(new NotificationDto(NotificationType.ADMIN,null, qrUrl)); |
| 101 | + return qrUrl; |
| 102 | + } |
| 103 | +} |
0 commit comments