Skip to content

Instantly share code, notes, and snippets.

@miguelcardo
miguelcardo / ini-service-delivery.java
Last active August 29, 2015 14:10
Initiate service delivery process
// Endpoint for the Service Delivery Request
put(new Route("/deliver") {
@Override
public Object handle(Request request, Response response) {
JSONObject jsonParameters = (JSONObject) JSONValue.parse(request.body());
String serviceId = (String) jsonParameters.get("serviceId");
// launch first operation for each of the two services or return 404 if serviceId unknown
if (serviceId.equals(Constants.invokeServiceId)){
// first operation is GET CARD
@miguelcardo
miguelcardo / send-op.java
Last active August 29, 2015 14:10
Sending an operation
// Read block 1 of sector 1 of the virtual MIFARE card
public int readBlock(String sessionId, HashMap<String, String> pendingOperations) {
String url = "https://api.fidesmo.com/mifare/read";
String callbackUrl = Constants.rootUrl + Constants.readBlockCallbackUrl;
// Encode the JSON structure with the trailer data for one sector
JSONObject payload = new JSONObject();
JSONArray blockData = new JSONArray();
blockData.add(Utils.encodeBlockToRead(1, 1));
payload.put("blocks", blockData);
@miguelcardo
miguelcardo / encodeBlockJason.java
Created November 27, 2014 15:44
Encode sector and block in JSON
// Returns JSON encoding of a block (inside a sector) to be read
public static JSONObject encodeBlockToRead(int numSector, int numBlock) {
JSONObject block = new JSONObject();
block.put ("sector", numSector);
block.put ("block", numBlock);
return block;
}
@miguelcardo
miguelcardo / check-operationId.java
Created November 27, 2014 15:47
Check operationId, store in map
BufferedReader inputReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = inputReader.readLine()) != null) {
response.append(inputLine);
}
inputReader.close();
JSONObject jsonResponse = (JSONObject) JSONValue.parse(response.toString());
String operationId = (String) jsonResponse.get("operationId");
@miguelcardo
miguelcardo / read-result.java
Created November 27, 2014 18:33
Check operation result
// handles response from the Read MIFARE Data message
post(new Route(Constants.readBlockCallbackUrl) {
@Override
public Object handle(Request request, Response response) {
JSONObject jsonParameters = (JSONObject) JSONValue.parse(request.body());
String operationId = (String) jsonParameters.get("operationId");
String sessionId = pendingOperations.get(operationId);
pendingOperations.remove(operationId);
int statusCode = ((Long)jsonParameters.get("statusCode")).intValue();
if (statusCode != HTTP_OK)
@miguelcardo
miguelcardo / we-are-done.java
Created November 27, 2014 18:36
Result for the entire process
// we are done - send the Service Delivery Completed message
cardClient.serviceDeliveryCompleted(true, sessionMap.get(sessionId).getCounter(), sessionId);
// Time to remove this session from the map - it is finished!
sessionMap.remove(sessionId);
@miguelcardo
miguelcardo / import_javacard.java
Created December 12, 2014 08:16
Writing a Java Card applet: start
import javacard.framework.*;
public class ExampleCardlet extends Applet
@miguelcardo
miguelcardo / install_cardlet.java
Created December 12, 2014 08:19
Install() method in a cardlet
public static void install(byte[] bArray, short bOffset, byte bLength){
new ExampleCardlet();
}
@miguelcardo
miguelcardo / process_apdu.java
Created December 12, 2014 08:23
Process an incoming APDU
public void process(APDU apdu){
byte buffer[] = apdu.getBuffer();
short length = (short) helloFidesmo.length;
Util.arrayCopyNonAtomic(helloFidesmo, (short)0, buffer, (short)0, (short)length);
apdu.setOutgoingAndSend((short)0, length);
}
import java.util.List;
import java.util.Arrays;
import java.lang.String;
import javax.smartcardio.*;
public class HelloFidesmoTest {
// don't forget to set the HELLO_FIDESMO_APPID environment variable with the AppID
// assigned by Fidesmo to your app in https://developer.fidesmo.com
public static String applicationId = System.getenv().get("HELLO_FIDESMO_APPID");
final private static String aidPrefix = "A00000061700";