package com.amplywealth.auth.controller; import com.amplywealth.auth.constant.AppConstant; import com.amplywealth.auth.constant.MessageConstant; import com.amplywealth.auth.data.payload.request.ServiceTokenRequest; import com.amplywealth.auth.data.payload.request.ServiceTokenVerificationRequest; import com.amplywealth.auth.data.payload.response.ServiceLoginResponse; import com.amplywealth.auth.service.S2SAuthService; import com.amplywealth.auth.util.ResponseHandler; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; /** *

The controller get delegation for all rest calls pertaining to service (here, internal backend services) authentication. */ @Slf4j @RestController @RequestMapping("/api/v1/auth/service") @Tag(name = "Service to Service Auth", description = "Operations pertaining to fetch and verify token for service2service communication.") public class S2SAuthController { private final S2SAuthService s2SAuthService; @Autowired public S2SAuthController(S2SAuthService service) { this.s2SAuthService = service; } @Operation(summary = "Fetch access token") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Successfully fetched access token", content = {@Content(array = @ArraySchema( schema = @Schema(implementation = ServiceLoginResponse.class)))}), @ApiResponse(responseCode = "400", description = "Bad Request", content = @Content), @ApiResponse(responseCode = "401", description = "Authorization denied", content = @Content), @ApiResponse(responseCode = "500", description = "Unexpected system exception", content = @Content) }) @PostMapping(value = "/token", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getToken(@Valid @RequestBody ServiceTokenRequest serviceTokenRequest) { log.debug("Inside getToken for scope list - {}", serviceTokenRequest); return ResponseHandler.generateResponse(List.of(MessageConstant.TOKEN_FETCH_SUCCESS), MessageConstant.STATUS_SUCCESS, HttpStatus.OK, AppConstant.PARENT_NODE_SERVICE_AUTH, s2SAuthService.getAccessToken(serviceTokenRequest)); } @Operation(summary = "verify access token") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Successfully verified token", content = {@Content(array = @ArraySchema( schema = @Schema(implementation = ServiceTokenVerificationRequest.class)))}), @ApiResponse(responseCode = "400", description = "Bad Request", content = @Content), @ApiResponse(responseCode = "401", description = "Authorization denied", content = @Content), @ApiResponse(responseCode = "500", description = "Unexpected system exception", content = @Content) }) @PostMapping(value = "/token/verify", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity verifyToken(@Valid @RequestBody ServiceTokenVerificationRequest serviceTokenVerificationRequest) { log.debug("Inside verifyToken"); return ResponseHandler.generateResponse(List.of(MessageConstant.TOKEN_FOUND_VALID), MessageConstant.STATUS_SUCCESS, HttpStatus.OK, AppConstant.PARENT_NODE_SERVICE_AUTH, s2SAuthService.verifyToken(serviceTokenVerificationRequest)); } }