Sample code for using IDA APIs in relying party application in Java

Sample code for using IDA APIs in relying party application in Java

MOSIP has built an application for demonstrations that uses MOSIP’s ID authentication APIs. Any relying party who wants to use MOSIP’s authentication APIs in their application can follow the steps used in the authentication-demo-ui application.

Code Base

Go to the IdaController.java > onSendAuthRequest () method in the authentication-demo-UI code.

Steps

  1. Create the AuthRequestDTO.

    AuthTypeDTO authTypeDTO = new AuthTypeDTO();
  2. Set the authentication types in the DTO.

    authTypeDTO.setBio(isBioAuthType()); authTypeDTO.setOtp(isOtpAuthType()); authTypeDTO.setDemo(isDemoAuthType()); authRequestDTO.setRequestedAuth(authTypeDTO);
  3. Set the individual ID, ID type, environment, and domain URI.

    authRequestDTO.setIndividualId(idValue.getText()); authRequestDTO.setIndividualIdType(idTypebox.getValue()); authRequestDTO.setEnv(env.getProperty("ida.request.captureFinger.env")); authRequestDTO.setDomainUri(env.getProperty("ida.request.captureFinger.domainUri"));
  4. Create a request DTO and set the encrypted request section.

    // Create Request DTO RequestDTO requestDTO = new RequestDTO(); // Set data in request block requestDTO.setTimestamp(getUTCCurrentDateTimeISOString()); if (isOtpAuthType()) { requestDTO.setOtp(otpValue.getText()); } Map<String, Object> identityBlock = mapper.convertValue(requestDTO, Map.class); if (isBioAuthType()) { identityBlock.put("biometrics", mapper.readValue(capture, Map.class).get("biometrics")); } if (isDemoAuthType()) { String input = StringUtils.isBlank(demoInputData.getText()) ? "{}" : demoInputData.getText(); identityBlock.put("demographics", mapper.readValue(input, Map.class)); } responsetextField.setText("Encrypting Auth Request..."); // Encrypt the request block EncryptionRequestDto encryptionRequestDto = new EncryptionRequestDto(); encryptionRequestDto.setIdentityRequest(identityBlock); EncryptionResponseDto kernelEncrypt = null; try { kernelEncrypt = kernelEncrypt(encryptionRequestDto, false); } catch (Exception e) { e.printStackTrace(); responsetextField.setText("Encryption of Auth Request Failed"); return; } responsetextField.setText("Authenticating..."); // Set request block authRequestDTO.setRequest(requestDTO);
  5. Add other request body parameters, such as unique transaction ID per transaction, current time in request time, consent, auth request ID (mosip.identity.kyc for KYC authentication and mosip.identity.auth for authentication), version (as 1.0), and thumbprint of the request.

    authRequestDTO.setTransactionID(getTransactionID()); authRequestDTO.setRequestTime(getUTCCurrentDateTimeISOString()); authRequestDTO.setConsentObtained(true); authRequestDTO.setId(getAuthRequestId()); authRequestDTO.setVersion("1.0"); authRequestDTO.setThumbprint(kernelEncrypt.getThumbprint());
  6. Create an authentication request map and add the request block, requestSessionKey, requestHMAC, and other parameters to the authentication request.

    Map<String, Object> authRequestMap = mapper.convertValue(authRequestDTO, Map.class); authRequestMap.replace("request", kernelEncrypt.getEncryptedIdentity()); authRequestMap.replace("requestSessionKey", kernelEncrypt.getEncryptedSessionKey()); authRequestMap.replace("requestHMAC", kernelEncrypt.getRequestHMAC()); RestTemplate restTemplate = createTemplate();
  7. Set the HTTP headers with the signature and content type.

    String reqJson = mapper.writeValueAsString(authRequestMap); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("signature", getSignature(reqJson)); httpHeaders.add("Content-type", MediaType.APPLICATION_JSON_VALUE); HttpEntity<String> httpEntity = new HttpEntity<>(reqJson, httpHeaders);

For the signature of the authentication request, follow the getSignature() and sign() methods in IdaContoller.java. These methods call the sign() method in SignatureUtil.java.



Reference