Skip to content

Commit

Permalink
MOSIP:32732 - API To fetch list of available partner certificates bas…
Browse files Browse the repository at this point in the history
…ed on partner type.

Signed-off-by: sudeep <[email protected]>
  • Loading branch information
Sudeep7353 committed May 9, 2024
1 parent a7ddd8a commit ff005fd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public enum ErrorCode {
PARTNER_HAVING_NO_CLAIMS("PMS_ESI_005","Partner has no user claims"),
PARTNER_HAVING_NO_ACRVALUES("PMS_ESI_006","Partner has no Authentication Context Refrences"),
FAILED_TO_PROCESS_JWK("PMS_ESI_007","Failed to process the Public Key"),
PARTNER_CERTIFICATES_FETCH_ERROR("PMS_CERTIFICATE_ERROR_001","Error while fetching partner certificates.");
PARTNER_CERTIFICATES_FETCH_ERROR("PMS_CERTIFICATE_ERROR_001","Error while fetching partner certificates."),
USER_ID_NOT_EXISTS("PMS_CERTIFICATE_ERROR_002","User Id does not exists."),
PARTNER_ID_NOT_EXISTS("PMS_CERTIFICATE_ERROR_003","Partner Id is null or empty.");

/**
* The error code.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,16 @@ public ResponseEntity<ResponseWrapper<APIKeyGenerateResponseDto>> generateAPIKey
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping(value = "/getPartnerCertificates")
@Operation(summary = "Get partner certificate", description = "fetch partner certificates")
@GetMapping(value = "/getAllCertificateDetails")
@Operation(summary = "Get partner certificates", description = "fetch partner certificates")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(schema = @Schema(hidden = true)))})
public ResponseWrapper<List<CertificateDto>> getPartnerCertificates() {
public ResponseWrapper<List<CertificateDto>> getAllCertificateDetails() {
ResponseWrapper<List<CertificateDto>> responseWrapper = new ResponseWrapper<>();
responseWrapper.setResponse(partnerService.getPartnerCertificates());
responseWrapper.setResponse(partnerService.getAllCertificateDetails());
return responseWrapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@Getter
@Setter
public class CertificateDto {
private String validFrom;
private String validTo;
private Date certificateUploadDate;
private Date certificateExpiryDate;
private String partnerType;
private String partnerId;
private Boolean isCertificateAvailable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,6 @@ public interface PartnerService {
/**
* @return
*/
public List<CertificateDto> getPartnerCertificates();
public List<CertificateDto> getAllCertificateDetails();

}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public class PartnerServiceImpl implements PartnerService {

private final static String LINE_SEPARATOR = "\n";

public static final String BLANK_STRING="";

@Autowired
PartnerServiceRepository partnerRepository;

Expand Down Expand Up @@ -1629,7 +1631,7 @@ public PartnerResponse updatePartnerDetail(PartnerUpdateRequest partnerUpdateReq
}

@Override
public List<CertificateDto> getPartnerCertificates() {
public List<CertificateDto> getAllCertificateDetails() {
List<CertificateDto> certificateDtoList = new ArrayList<>();
try {
String userId = getUserId();
Expand All @@ -1638,12 +1640,17 @@ public List<CertificateDto> getPartnerCertificates() {
for (Partner partner : partnerList) {
CertificateDto certificateDto = new CertificateDto();
try {
if (Objects.nonNull(partner.getId()) && !partner.getId().equals(BLANK_STRING)) {
LOGGER.info("Partner Id is null or empty for user id : " + userId);
throw new PartnerServiceException(ErrorCode.PARTNER_ID_NOT_EXISTS.getErrorCode(),
ErrorCode.PARTNER_ID_NOT_EXISTS.getErrorMessage());
}
PartnerCertDownloadRequestDto requestDto = new PartnerCertDownloadRequestDto();
requestDto.setPartnerId(partner.getId());
PartnerCertDownloadResponeDto partnerCertDownloadResponeDto = getPartnerCertificate(requestDto);
String certificateData = partnerCertDownloadResponeDto.getCertificateData();
certificateData = certificateData.replaceAll("-----BEGIN CERTIFICATE-----", "")
.replaceAll("-----END CERTIFICATE-----", "")
certificateData = certificateData.replaceAll(BEGIN_CERTIFICATE, "")
.replaceAll(END_CERTIFICATE, "")
.replaceAll("\n", "");

byte[] decodedCertificate = Base64.getDecoder().decode(certificateData);
Expand All @@ -1652,11 +1659,11 @@ public List<CertificateDto> getPartnerCertificates() {
X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(decodedCertificate));

certificateDto.setIsCertificateAvailable(true);
certificateDto.setValidFrom(formatDate(cert.getNotBefore()));
certificateDto.setValidTo(formatDate(cert.getNotAfter()));
certificateDto.setCertificateUploadDate(cert.getNotBefore());
certificateDto.setCertificateExpiryDate(cert.getNotAfter());
certificateDto.setPartnerId(partner.getId());
certificateDto.setPartnerType(partner.getPartnerTypeCode());
} catch (Exception ex) {
} catch (PartnerServiceException ex) {
LOGGER.info("Could not fetch partner certificate :" + ex.getMessage());
certificateDto.setIsCertificateAvailable(false);
certificateDto.setPartnerId(partner.getId());
Expand All @@ -1666,11 +1673,15 @@ public List<CertificateDto> getPartnerCertificates() {
}
} else {
LOGGER.info("sessionId", "idType", "id", "User id does not exists.");
throw new PartnerServiceException(ErrorCode.USER_ID_NOT_EXISTS.getErrorCode(),
ErrorCode.USER_ID_NOT_EXISTS.getErrorMessage());
}
} catch (PartnerServiceException ex) {
throw ex;
} catch (Exception ex) {
LOGGER.debug("sessionId", "idType", "id", ex.getStackTrace());
LOGGER.error("sessionId", "idType", "id",
"In getPartnerCertificates method of PartnerServiceImpl - " + ex.getMessage());
"In getAllCertificateDetails method of PartnerServiceImpl - " + ex.getMessage());
throw new PartnerServiceException(ErrorCode.PARTNER_CERTIFICATES_FETCH_ERROR.getErrorCode(),
ErrorCode.PARTNER_CERTIFICATES_FETCH_ERROR.getErrorMessage());
}
Expand All @@ -1685,9 +1696,4 @@ private String getUserId() {
String userId = authUserDetails().getUserId();
return userId;
}

public static String formatDate(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
}

0 comments on commit ff005fd

Please sign in to comment.