Skip to content

Commit

Permalink
added create-vci-exchange api (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaifk468 authored Oct 5, 2023
1 parent f14d1b8 commit 2d4c921
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -574,6 +569,68 @@ public ResponseEntity<String> 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<String> 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<String, Object> 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<String, Object> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",\
Expand Down Expand Up @@ -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 ***********
Expand Down

0 comments on commit 2d4c921

Please sign in to comment.