From 2d4c921f1f61abf9fdc28f6f9b842856474216e9 Mon Sep 17 00:00:00 2001 From: kaifk468 <74772315+kaifk468@users.noreply.github.com> Date: Thu, 5 Oct 2023 14:06:34 +0530 Subject: [PATCH] added create-vci-exchange api (#1100) --- .../controller/AuthRequestController.java | 75 ++++++++++++++++--- .../src/main/resources/application.properties | 3 +- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/authentication-demo-service/src/main/java/io/mosip/testrig/authentication/demo/service/controller/AuthRequestController.java b/authentication-demo-service/src/main/java/io/mosip/testrig/authentication/demo/service/controller/AuthRequestController.java index c5c37461221..a45c3a29f27 100644 --- a/authentication-demo-service/src/main/java/io/mosip/testrig/authentication/demo/service/controller/AuthRequestController.java +++ b/authentication-demo-service/src/main/java/io/mosip/testrig/authentication/demo/service/controller/AuthRequestController.java @@ -47,6 +47,7 @@ import javax.crypto.NoSuchPaddingException; import javax.xml.bind.DatatypeConverter; +import io.mosip.testrig.authentication.demo.service.dto.*; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.bouncycastle.operator.OperatorCreationException; @@ -62,12 +63,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; -import org.springframework.web.bind.annotation.DeleteMapping; -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.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -90,9 +86,6 @@ import io.mosip.kernel.core.util.DateUtils; import io.mosip.kernel.core.util.HMACUtils2; import io.mosip.testrig.authentication.demo.service.controller.Encrypt.SplittedEncryptedData; -import io.mosip.testrig.authentication.demo.service.dto.CertificateChainResponseDto; -import io.mosip.testrig.authentication.demo.service.dto.EncryptionRequestDto; -import io.mosip.testrig.authentication.demo.service.dto.EncryptionResponseDto; import io.mosip.testrig.authentication.demo.service.helper.CertificateTypes; import io.mosip.testrig.authentication.demo.service.helper.KeyMgrUtil; import io.mosip.testrig.authentication.demo.service.helper.PartnerTypes; @@ -177,6 +170,8 @@ public class AuthRequestController { private static final String IDA_KYC_EXCHANGE_REQUEST_TEMPLATE = "ida.kycExchangeRequest.template"; + private static final String IDA_VCI_EXCHANGE_REQUEST_TEMPLATE = "ida.vciExchangeRequest.template"; + private static final String ID = "id"; private static final String CLASSPATH = "classpath"; @@ -574,6 +569,68 @@ public ResponseEntity createKycExchangeRequest(@RequestParam(name = ID, String.format(IdAuthenticationErrorConstants.MISSING_INPUT_PARAMETER.getErrorMessage(), IDENTITY)); } } + + @PostMapping(path = "/create-vci-exchange-request", consumes = MediaType.APPLICATION_JSON_VALUE, produces = { + MediaType.APPLICATION_JSON_VALUE }) + public ResponseEntity createVciExchangeRequest(@RequestParam(name = ID, required = true) @Nullable String id, + @RequestParam(name = ID_TYPE, required = false) @Nullable String idType, + @RequestParam(name = "Authtype", required = false) @Nullable String reqAuth, + @RequestParam(name = TRANSACTION_ID, required = false) @Nullable String transactionId, + @RequestParam(name = "requestTime", required = false) @Nullable String requestTime, + @RequestParam(name = "vcFormat", required = false) @Nullable String vcFormat, + @RequestParam(name = "credSubjectId", required = false)@Nullable String credSubjectId, + @RequestParam(name = "vcAuthToken", required = false)@Nullable String vcAuthToken, + @RequestParam(name = "keyFileNameByPartnerName", required = false)boolean keyFileNameByPartnerName, + @RequestParam(name = "partnerName", required = false)@Nullable String partnerName, + @RequestBody Map request, + @RequestParam(name = "certsDir", required = false) String certsDir, + @RequestParam(name = "moduleName", required = false) String moduleName) throws Exception { + String authRequestTemplate = environment.getProperty(IDA_VCI_EXCHANGE_REQUEST_TEMPLATE); + Map reqValues = new HashMap<>(); + + if (requestTime == null) { + requestTime = DateUtils.getUTCCurrentDateTimeString(environment.getProperty("datetime.pattern")); + } + + reqValues.put(ID, id); + reqValues.put("individualIdType", idType == null || idType.trim().length() == 0 ? IdType.UIN.toString() : idType); + reqValues.put(AUTH_TYPE, reqAuth); + reqValues.put(TIMESTAMP, requestTime); + reqValues.put(TXN, transactionId == null ? "1234567890" : transactionId); + reqValues.put(VER, environment.getProperty(IDA_API_VERSION)); + reqValues.put("vcFormat", vcFormat); + reqValues.put("credSubjectId", credSubjectId); + reqValues.put("vcAuthToken", vcAuthToken); + + StringWriter writer = new StringWriter(); + InputStream templateValue; + if (request != null && request.size() > 0) { + templateValue = templateManager + .merge(new ByteArrayInputStream(authRequestTemplate.getBytes(StandardCharsets.UTF_8)), reqValues); + + if (templateValue != null) { + IOUtils.copy(templateValue, writer, StandardCharsets.UTF_8); + String res = writer.toString(); + ObjectNode response = mapper.readValue(res.getBytes(), ObjectNode.class); + + HttpHeaders httpHeaders = new HttpHeaders(); + String responseStr = response.toString(); + + String rpSignature = signRequest(PartnerTypes.MISP, partnerName, keyFileNameByPartnerName, responseStr, certsDir, moduleName); + httpHeaders.add("signature", rpSignature); + return new ResponseEntity<>(responseStr, httpHeaders, HttpStatus.OK); + } else { + throw new IdAuthenticationBusinessException( + IdAuthenticationErrorConstants.MISSING_INPUT_PARAMETER.getErrorCode(), String.format( + IdAuthenticationErrorConstants.MISSING_INPUT_PARAMETER.getErrorMessage(), TEMPLATE)); + } + } else { + throw new IdAuthenticationBusinessException( + IdAuthenticationErrorConstants.MISSING_INPUT_PARAMETER.getErrorCode(), + String.format(IdAuthenticationErrorConstants.MISSING_INPUT_PARAMETER.getErrorMessage(), IDENTITY)); + } + } + /** * this method is used to create the auth request. diff --git a/authentication-demo-service/src/main/resources/application.properties b/authentication-demo-service/src/main/resources/application.properties index cf22ff0f99e..0ca94feea60 100644 --- a/authentication-demo-service/src/main/resources/application.properties +++ b/authentication-demo-service/src/main/resources/application.properties @@ -20,6 +20,7 @@ ida-demo.api.version=v1 server.port = 8082 ida.authRequest.template={"consentObtained":true,"id":"mosip.identity.$authType","individualId":"$id","keyIndex":"string","request":"$encRequest","requestHMAC":"$encHmac","requestSessionKey":"$encSessionKey","requestTime":"$timestamp","transactionID":"$txn","version":"$ver","domainUri":"$domainUri","env":"$env","specVersion":"1.0","thumbprint":"$thumbprint"} ida.kycExchangeRequest.template={"consentObtained":["true"],"locales":["eng"],"id":"mosip.identity.$authType","individualId":"$id","individualIdType":"$individualIdType","kycToken":"$kycToken","respType":"$respType","metadata":"$request","requestTime":"$timestamp","transactionID":"$txn","version":"$ver"} +ida.vciExchangeRequest.template={"consentObtained":["true"],"locales":["eng"],"id":"mosip.identity.$authType","individualId":"$id","individualIdType":"$individualIdType","kycToken":"$kycToken","respType":"$respType","metadata":"$request","requestTime":"$timestamp","transactionID":"$txn","version":"$ver","vcAuthToken":"$vcAuthToken","credSubjectId":"$credSubjectId","vcAuthToken":"$vcAuthToken","vcFormat":"$vcFormat"} otpRequestTemplate={\ "id": "$reqId",\ "individualId": "$id",\ @@ -64,7 +65,7 @@ ida.api.version=1.0 javax.persistence.jdbc.driver=org.postgresql.Driver javax.persistence.jdbc.url=jdbc:postgresql://localhost:5432/ida_db javax.persistence.jdbc.user=postgres -javax.persistence.jdbc.password=admin +javax.persistence.jdbc.password=postgres # ********* Hibernate Properties ***********