|
|
|
|
package kr.re.etri.autoflow.controllers;
|
|
|
|
|
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import kr.re.etri.autoflow.payload.request.EdgePkgInfoRequest;
|
|
|
|
|
import kr.re.etri.autoflow.service.ExternalAuthService;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/external-auth")
|
|
|
|
|
@Tag(name = "ExternalAuthController", description = "외부 백엔드 로그인 및 Bearer 토큰 조회 API")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ExternalAuthController {
|
|
|
|
|
|
|
|
|
|
private final ExternalAuthService externalAuthService;
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "외부 로그인 후 사용자 정보 조회", description = "외부 Spring 백엔드에 로그인 요청 후 id, name, token을 반환합니다.")
|
|
|
|
|
@PostMapping("/signin")
|
|
|
|
|
public ResponseEntity<ApiResponse> signin(@RequestBody SigninRequest request) {
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> userInfo = externalAuthService.getUserInfo(request.id(), request.password());
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(userInfo));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.failure(e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "Edge 패키지 목록 조회", description = "로그인 후 받은 id와 token을 사용해 외부 Edge 패키지 검색 API를 호출합니다.")
|
|
|
|
|
@GetMapping("/edge-search")
|
|
|
|
|
public ResponseEntity<ApiResponse> edgeSearch(
|
|
|
|
|
@RequestParam String id,
|
|
|
|
|
@RequestParam String token
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> edgeResult = externalAuthService.getEdgePackageList(id, token);
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(edgeResult));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.failure(e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Operation(
|
|
|
|
|
summary = "외부 Edge 패키지 등록",
|
|
|
|
|
description = "외부 서버로 Edge 패키지 정보를 파일과 함께 전송하여 등록합니다.",
|
|
|
|
|
security = @SecurityRequirement(name = "bearerAuth") // 이 컨트롤러만 Bearer 적용
|
|
|
|
|
)
|
|
|
|
|
@PostMapping(value = "/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
|
|
|
public ResponseEntity<?> addEdgePackage(
|
|
|
|
|
@Parameter(description = "로그인 시 발급받은 Bearer 토큰")
|
|
|
|
|
@RequestHeader("Authorization") String bearerToken,
|
|
|
|
|
|
|
|
|
|
@Parameter(description = "Edge 패키지 등록 요청 데이터(JSON 형식)")
|
|
|
|
|
@RequestPart(value = "edgePkgInfoVO", required = true) EdgePkgInfoRequest edgePkgInfoRequest,
|
|
|
|
|
|
|
|
|
|
@Parameter(description = "업로드할 패키지 파일")
|
|
|
|
|
@RequestPart(value = "file", required = false) MultipartFile file
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
// 서비스 호출
|
|
|
|
|
Object response = externalAuthService.uploadEdgePackage(bearerToken, edgePkgInfoRequest, file);
|
|
|
|
|
return ResponseEntity.ok(response);
|
|
|
|
|
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(
|
|
|
|
|
Map.of("success", false, "message", "잘못된 요청: " + e.getMessage())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.internalServerError().body(
|
|
|
|
|
Map.of("success", false, "message", "파일 업로드 실패: " + e.getMessage())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DTO: 요청
|
|
|
|
|
public static record SigninRequest(String id, String password) { }
|
|
|
|
|
|
|
|
|
|
// DTO: 응답
|
|
|
|
|
public static class ApiResponse {
|
|
|
|
|
private boolean success;
|
|
|
|
|
private Object data;
|
|
|
|
|
private String errorMessage;
|
|
|
|
|
|
|
|
|
|
public ApiResponse(boolean success, Object data, String errorMessage) {
|
|
|
|
|
this.success = success;
|
|
|
|
|
this.data = data;
|
|
|
|
|
this.errorMessage = errorMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ApiResponse success(Object data) {
|
|
|
|
|
return new ApiResponse(true, data, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ApiResponse failure(String errorMessage) {
|
|
|
|
|
return new ApiResponse(false, null, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isSuccess() { return success; }
|
|
|
|
|
public Object getData() { return data; }
|
|
|
|
|
public String getErrorMessage() { return errorMessage; }
|
|
|
|
|
}
|
|
|
|
|
}
|