Skip to content

Commit

Permalink
Added a test and fixed various issues with BatchSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemuu committed Feb 26, 2024
1 parent 4e7443f commit 269cafd
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 28 deletions.
24 changes: 16 additions & 8 deletions src/main/java/fi/methics/laverca/rest/json/BatchSignatureReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@
*/
public class BatchSignatureReq extends AdditionalService {

@SerializedName("DataToBeSigned")
public Data DataToBeSigned;

@SerializedName("DataToBeDisplayed")
public Data DataToBeDisplayed;
@SerializedName("BatchSignatureRequest")
public BatchSignatureRequest BatchSignatureRequest;

public BatchSignatureReq(DTBS dtbs, String dtbd) {
this.Description = BATCH_AS;
this.DataToBeSigned = new Data(dtbs);
this.DataToBeDisplayed = new Data();
this.DataToBeDisplayed.Data = dtbd;
this.BatchSignatureRequest = new BatchSignatureRequest();
this.BatchSignatureRequest.DataToBeSigned = new Data(dtbs);
this.BatchSignatureRequest.DataToBeDisplayed = new Data();
this.BatchSignatureRequest.DataToBeDisplayed.Data = dtbd;
}

public class BatchSignatureRequest {

@SerializedName("DataToBeSigned")
public Data DataToBeSigned;

@SerializedName("DataToBeDisplayed")
public Data DataToBeDisplayed;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,4 @@
*/
public class MSS_BatchSignatureResp extends MSS_SignatureResp {

/**
* Get all batch signatures if available.
* @return Map which contains both the DTBD and Signature. Map will be empty if no batch signature was done.
*/
public Map<String, byte[]> getBatchSignatures() {
Map<String, byte[]> result = new HashMap<>();
for (ServiceResponse resp : this.getServiceResponses()) {
if (AdditionalService.BATCH_AS.equals(resp.Description)) {
if (resp.BatchSignatureResponses != null) {
for (BatchSignatureResponse bResp : resp.BatchSignatureResponses) {
if (bResp == null) continue;
String signature = bResp.MSS_Signature != null ? bResp.MSS_Signature.Base64Signature : null;
result.put(bResp.DTBD, Base64.getDecoder().decode(signature));
}
}
}
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public Data(DTBS dtbs) {
this.Data = Base64.getEncoder().encodeToString(dtbs.toBytes());
this.Encoding = dtbs.getEncoding();
this.MimeType = dtbs.getMimetype();
if (this.MimeType == null) {
this.MimeType = "text/plain";
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/fi/methics/laverca/rest/json/MSS_SignatureResp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bouncycastle.asn1.pkcs.SignedData;
import org.bouncycastle.asn1.x500.X500Name;

import com.google.gson.annotations.SerializedName;

import fi.methics.laverca.rest.json.ServiceResponse.BatchSignatureResponse;
import fi.methics.laverca.rest.util.LavercaRDNStyle;
import fi.methics.laverca.rest.util.X509Util;

Expand Down Expand Up @@ -154,5 +157,26 @@ public SignedData getCmsSignedData() {
public List<ServiceResponse> getServiceResponses() {
return this.ServiceResponses;
}


/**
* Get all batch signatures if available.
* @return Map which contains both the DTBD and Signature. Map will be empty if no batch signature was done.
*/
public Map<String, byte[]> getBatchSignatures() {
Map<String, byte[]> result = new HashMap<>();
for (ServiceResponse resp : this.getServiceResponses()) {
if (AdditionalService.BATCH_AS.equals(resp.Description)) {
if (resp.BatchSignatureResponses != null) {
for (BatchSignatureResponse bResp : resp.BatchSignatureResponses) {
if (bResp == null) continue;
String signature = bResp.MSS_Signature != null ? bResp.MSS_Signature.Base64Signature : null;
result.put(bResp.DTBD, Base64.getDecoder().decode(signature));
}
}
}
}
return result;
}

}
71 changes: 71 additions & 0 deletions src/test/java/fi/methics/laverca/rest/TestBatchSignature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// (c) Copyright 2003-2023 Methics Oy. All rights reserved.
//
package fi.methics.laverca.rest;

import java.util.Collection;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import fi.methics.laverca.rest.json.MSS_SignatureReq;
import fi.methics.laverca.rest.json.MSS_SignatureResp;
import fi.methics.laverca.rest.util.DTBS;
import fi.methics.laverca.rest.util.MSS_SignatureReqBuilder;
import fi.methics.laverca.rest.util.SignatureProfile;

public class TestBatchSignature {


public static final String BASE_URL = "https://demo.methics.fi/restapi/";
public static final String APNAME = "TestAP";
public static final String PASSWORD = "9TMzfH7EKXETOB8FT5gz";
public static final String MSISDN = "35847001001";
public static final SignatureProfile SIGPROF = SignatureProfile.of("http://alauda.mobi/digitalSignature");


@Test
public void testTwoSignatures() {
MssClient client = new MssClient.Builder().withRestUrl("https://demo.methics.fi/restapi/")
.withPassword("TestAP", "9TMzfH7EKXETOB8FT5gz")
.build();

MSS_SignatureReq req = new MSS_SignatureReqBuilder()
.withMsisdn(MSISDN)
.withDtbs(new DTBS("test1")).withDtbd("test1")
.withBatchSinature("test2", new DTBS("test2"))
.build();

MSS_SignatureResp resp = client.sign(req);
Assertions.assertTrue(resp.isSuccessful(), "Batch signature succeeded");
}


@Test
public void testFourSignatures() {
MssClient client = new MssClient.Builder().withRestUrl("https://demo.methics.fi/restapi/")
.withPassword("TestAP", "9TMzfH7EKXETOB8FT5gz")
.build();

MSS_SignatureReq req = new MSS_SignatureReqBuilder()
.withMsisdn(MSISDN)
.withDtbs(new DTBS("test1")).withDtbd("test1")
.withBatchSinature("test2", new DTBS("test2"))
.withBatchSinature("test3", new DTBS("test3"))
.withBatchSinature("test4", new DTBS("test4"))
.build();

MSS_SignatureResp resp = client.sign(req);
Assertions.assertTrue(resp.isSuccessful(), "Batch signature succeeded");

Collection<String> dtbds = resp.getBatchSignatures().keySet();
for (String dtbd : dtbds) {
Assertions.assertNotNull(resp.getBatchSignatures().get(dtbd), "Signature for DTBD " + dtbd);
}
Assertions.assertTrue(dtbds.contains("test1"), "Got signature for test1");
Assertions.assertTrue(dtbds.contains("test2"), "Got signature for test2");
Assertions.assertTrue(dtbds.contains("test3"), "Got signature for test3");
Assertions.assertTrue(dtbds.contains("test4"), "Got signature for test4");
}

}

0 comments on commit 269cafd

Please sign in to comment.