From 6f490dc7f52a1f907d43c530635d6f13f589cb55 Mon Sep 17 00:00:00 2001 From: diegomallada1 <111504477+diegomallada1@users.noreply.github.com> Date: Fri, 23 Aug 2024 10:42:42 +0200 Subject: [PATCH] Add empty select after checking card info (#98) * add empty select before starting delivery * rearrange terms * add comment * Add select in ws deliveries too --- .../java/com/fidesmo/fdsm/FidesmoCard.java | 9 +++++++++ .../fidesmo/fdsm/ServiceDeliverySession.java | 20 +++++++++++-------- tool/src/main/java/com/fidesmo/fdsm/Main.java | 2 ++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/com/fidesmo/fdsm/FidesmoCard.java b/library/src/main/java/com/fidesmo/fdsm/FidesmoCard.java index 008e99a..d8bf067 100644 --- a/library/src/main/java/com/fidesmo/fdsm/FidesmoCard.java +++ b/library/src/main/java/com/fidesmo/fdsm/FidesmoCard.java @@ -181,6 +181,7 @@ public static Optional detectOnline(BIBO channel, FidesmoApiClient static final HexBytes getDataCIN = HexBytes.b(new CommandAPDU(0x80, 0xCA, 0x00, 0x45, 0x00).getBytes()); static final HexBytes selectFidesmoPlatform = HexBytes.b(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, FIDESMO_PLATFORM_AID.getBytes()).getBytes()); static final HexBytes selectFidesmoBatch = HexBytes.b(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, FIDESMO_BATCH_AID.getBytes()).getBytes()); + static final HexBytes selectEmpty = HexBytes.b(new CommandAPDU(0x00, 0xA4, 0x00, 0x00, 0x00).getBytes()); public static Map probe(BIBO channel) { // preserve order, just for fun @@ -331,6 +332,14 @@ public static boolean deliverRecipes(BIBO bibo, FidesmoCard card, AuthenticatedF return true; } + public byte[] selectEmpty(APDUBIBO channel) { + CommandAPDU select = new CommandAPDU(selectEmpty.value()); + ResponseAPDU response; + + response = channel.transmit(select); + return response.getData(); + } + public byte[] getCIN() { return cin.clone(); } diff --git a/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java b/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java index 1d25247..4a19625 100644 --- a/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java +++ b/library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java @@ -21,6 +21,7 @@ */ package com.fidesmo.fdsm; +import apdu4j.core.APDUBIBO; import apdu4j.core.BIBO; import apdu4j.core.BIBOException; import apdu4j.core.HexUtils; @@ -103,6 +104,9 @@ public DeliveryResult call() throws FDSMException { } public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws IOException, UnsupportedCallbackException { + APDUBIBO apduBibo = new APDUBIBO(bibo); + //Reset after checking card info to avoid leaving FPA selected, which prevents some services to be run. + card.selectEmpty(apduBibo); // Address #4 JsonNode deviceInfo = client.rpc(client.getURI(FidesmoApiClient.DEVICES_URL, HexUtils.bin2hex(card.getCIN()), card.getBatchId())); byte[] iin = HexUtils.decodeHexString_imp(deviceInfo.get("iin").asText()); @@ -136,17 +140,17 @@ public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws } // Construct Delivery Request - ObjectNode deliveryrequest = JsonNodeFactory.instance.objectNode(); + ObjectNode deliveryRequest = JsonNodeFactory.instance.objectNode(); - deliveryrequest.put("appId", appId); - deliveryrequest.put("serviceId", serviceId); + deliveryRequest.put("appId", appId); + deliveryRequest.put("serviceId", serviceId); // cardId ObjectNode cardId = JsonNodeFactory.instance.objectNode(); cardId.put("iin", HexUtils.bin2hex(iin)); cardId.put("cin", HexUtils.bin2hex(card.getCIN())); cardId.put("platformVersion", platformVersion); - deliveryrequest.set("cardId", cardId); + deliveryRequest.set("cardId", cardId); // User input fields ArrayList fields = new ArrayList<>(fieldsFromNode(description.get("fieldsRequired"))); @@ -158,13 +162,13 @@ public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws Map userInput = formHandler.processForm(fields); if (description.has("emailRequired")) - deliveryrequest.put("email", userInput.remove("email").getValue()); + deliveryRequest.put("email", userInput.remove("email").getValue()); if (description.has("msisdnRequired")) - deliveryrequest.put("msisdn", userInput.remove("msisdn").getValue()); + deliveryRequest.put("msisdn", userInput.remove("msisdn").getValue()); - deliveryrequest.set("fields", mapToJsonNode(userInput)); + deliveryRequest.set("fields", mapToJsonNode(userInput)); - JsonNode delivery = client.rpc(client.getURI(FidesmoApiClient.SERVICE_DELIVER_URL), deliveryrequest); + JsonNode delivery = client.rpc(client.getURI(FidesmoApiClient.SERVICE_DELIVER_URL), deliveryRequest); String sessionId = delivery.get("sessionId").asText(); logger.info("Delivering: {}", FidesmoApiClient.lamei18n(description.get("title"))); diff --git a/tool/src/main/java/com/fidesmo/fdsm/Main.java b/tool/src/main/java/com/fidesmo/fdsm/Main.java index f560261..b85a08c 100644 --- a/tool/src/main/java/com/fidesmo/fdsm/Main.java +++ b/tool/src/main/java/com/fidesmo/fdsm/Main.java @@ -326,6 +326,8 @@ public static void main(String[] argv) { DeliveryUrl delivery = DeliveryUrl.parse(args.valueOf(OPT_RUN)); if (delivery.isWebSocket()) { + FidesmoCard fidesmoCard = fidesmoMetadata.get(); + fidesmoCard.selectEmpty(bibo); boolean success = WsClient.execute(new URI(delivery.getService()), bibo, auth).join().isSuccess(); if (!success) { fail("Fail to run a script");