Skip to content

Commit fdf2bf2

Browse files
committed
Introduced impl. for new dtbd format #2, type rename, UT update for Sig Request Dtbs
1 parent f07f1e0 commit fdf2bf2

File tree

6 files changed

+164
-48
lines changed

6 files changed

+164
-48
lines changed

mid-java-client-core/src/main/java/ch/swisscom/mid/client/impl/SignatureValidatorImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import ch.swisscom.mid.client.SignatureValidator;
44
import ch.swisscom.mid.client.config.ConfigurationException;
55
import ch.swisscom.mid.client.config.SignatureValidationConfiguration;
6-
import ch.swisscom.mid.client.model.DataToBeSignedTXN;
6+
import ch.swisscom.mid.client.model.DataToBeSignedTXNResponseType;
77
import ch.swisscom.mid.client.model.SignatureValidationFailureReason;
88
import ch.swisscom.mid.client.model.SignatureValidationResult;
99
import ch.swisscom.mid.client.model.Traceable;
@@ -184,7 +184,7 @@ public SignatureValidationResult validateSignature(String base64SignatureContent
184184
.replace("\"[", "[")
185185
.replace("]\"", "]"));
186186

187-
DataToBeSignedTXN resDtbs = jacksonMapper.readValue(escResultDtbs, DataToBeSignedTXN.class);
187+
DataToBeSignedTXNResponseType resDtbs = jacksonMapper.readValue(escResultDtbs, DataToBeSignedTXNResponseType.class);
188188
String finalResDtbs = jacksonMapper.writeValueAsString(resDtbs.getDtbd());
189189
result.setDtbsMatching(reqDtbsValueStr.equals(finalResDtbs));
190190
} catch (JsonProcessingException e) {

mid-java-client-core/src/main/java/ch/swisscom/mid/client/model/DataToBeSigned.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package ch.swisscom.mid.client.model;
1717

18+
import static ch.swisscom.mid.client.utils.Utils.dataIsTXNApprovalRequestType;
1819
import static ch.swisscom.mid.client.utils.Utils.dataNotEmpty;
1920

2021
public class DataToBeSigned {
@@ -61,14 +62,17 @@ public void validateYourself() {
6162
dataNotEmpty(data, "The data in the DataToBeSigned cannot be null or empty");
6263
dataNotEmpty(encoding, "The encoding in the DataToBeSigned cannot be null or empty (set it to \"UTF-8\", for example)");
6364
dataNotEmpty(mimeType, "The mime type in the DataToBeSigned cannot be null or empty (set it to \"text/plain\", for example)");
65+
if (mimeType.equalsIgnoreCase("application/vnd.mobileid.txn-approval")) {
66+
dataIsTXNApprovalRequestType(data, "The DataToBeSigned format is not valid for mime type 'application/vnd.mobileid.txn-approval'");
67+
}
6468
}
6569

6670
@Override
6771
public String toString() {
6872
return "DataToBeSigned{" +
69-
"data='" + data + '\'' +
70-
", encoding='" + encoding + '\'' +
71-
", mimeType='" + mimeType + '\'' +
72-
'}';
73+
"data='" + data + '\'' +
74+
", encoding='" + encoding + '\'' +
75+
", mimeType='" + mimeType + '\'' +
76+
'}';
7377
}
7478
}

mid-java-client-core/src/main/java/ch/swisscom/mid/client/model/DataToBeSignedTXN.java renamed to mid-java-client-core/src/main/java/ch/swisscom/mid/client/model/DataToBeSignedTXNResponseType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
import java.util.Map;
88

99

10-
public class DataToBeSignedTXN {
10+
public class DataToBeSignedTXNResponseType {
1111
@JsonProperty("format_version")
1212
private String formatVersion;
1313

1414
@JsonProperty("content_string")
1515
private List<Map<String, String>> dtbd = new ArrayList<>();
1616

17-
public DataToBeSignedTXN() {
17+
public DataToBeSignedTXNResponseType() {
1818
}
1919

20-
public DataToBeSignedTXN(String formatVersion, List<Map<String, String>> dtbd) {
20+
public DataToBeSignedTXNResponseType(String formatVersion, List<Map<String, String>> dtbd) {
2121
this.formatVersion = formatVersion;
2222
this.dtbd = dtbd;
2323
}
@@ -40,7 +40,7 @@ public void setDtbd(List<Map<String, String>> dtbd) {
4040

4141
@Override
4242
public String toString() {
43-
return "DataToBeSignedTXN{" +
43+
return "DataToBeSignedTXNResponseType{" +
4444
"formatVersion='" + formatVersion + '\'' +
4545
", content_string=" + dtbd +
4646
'}';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ch.swisscom.mid.client.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class TXNApprovalReqType {
10+
11+
@JsonProperty("type")
12+
private String type;
13+
14+
@JsonProperty("dtbd")
15+
private List<Map<String, String>> dtbd = new ArrayList<>();
16+
17+
public TXNApprovalReqType() {
18+
}
19+
20+
public TXNApprovalReqType(String type, List<Map<String, String>> dtbd) {
21+
this.type = type;
22+
this.dtbd = dtbd;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "TXNApprovalReqType{" +
28+
"type='" + type + '\'' +
29+
", dtbd=" + dtbd +
30+
'}';
31+
}
32+
33+
public void setType(String type) {
34+
this.type = type;
35+
}
36+
37+
public void setDtbd(List<Map<String, String>> dtbd) {
38+
this.dtbd = dtbd;
39+
}
40+
41+
public String getType() {
42+
return type;
43+
}
44+
45+
public List<Map<String, String>> getDtbd() {
46+
return dtbd;
47+
}
48+
}

mid-java-client-core/src/main/java/ch/swisscom/mid/client/utils/Utils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
import ch.swisscom.mid.client.config.ConfigurationException;
2222
import ch.swisscom.mid.client.model.DataAssemblyException;
23+
import ch.swisscom.mid.client.model.TXNApprovalReqType;
2324
import ch.swisscom.mid.client.model.Traceable;
25+
import com.fasterxml.jackson.core.JsonProcessingException;
26+
import com.fasterxml.jackson.databind.DeserializationFeature;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
2428

2529
import javax.xml.datatype.DatatypeConfigurationException;
2630
import javax.xml.datatype.DatatypeFactory;
@@ -51,6 +55,21 @@ public static void dataNotNull(Object target, String errorMessage) throws DataAs
5155
}
5256
}
5357

58+
public static void dataIsTXNApprovalRequestType(String data, String errorMessage) throws DataAssemblyException {
59+
final ObjectMapper jacksonMapper = new ObjectMapper();
60+
jacksonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
61+
TXNApprovalReqType parsedDtbs = null;
62+
try {
63+
parsedDtbs = jacksonMapper.readValue(data, TXNApprovalReqType.class);
64+
} catch (JsonProcessingException e) {
65+
throw new DataAssemblyException(errorMessage);
66+
}
67+
if (parsedDtbs == null || parsedDtbs.getDtbd().isEmpty()) {
68+
throw new DataAssemblyException(errorMessage);
69+
}
70+
71+
}
72+
5473
public static <T> void dataNotEmpty(List<T> list, String errorMessage) throws DataAssemblyException {
5574
if (list == null || list.size() == 0) {
5675
throw new DataAssemblyException(errorMessage);

mid-java-client-rest/src/test/java/ch/swisscom/mid/client/rest/SyncSignatureTest.java

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,26 @@
1515
*/
1616
package ch.swisscom.mid.client.rest;
1717

18-
import ch.swisscom.mid.client.config.TlsConfiguration;
19-
import com.github.tomakehurst.wiremock.WireMockServer;
20-
import com.github.tomakehurst.wiremock.http.MimeType;
21-
22-
import org.junit.jupiter.api.AfterAll;
23-
import org.junit.jupiter.api.BeforeAll;
24-
import org.junit.jupiter.api.Test;
25-
2618
import ch.swisscom.mid.client.MIDClient;
2719
import ch.swisscom.mid.client.MIDFlowException;
2820
import ch.swisscom.mid.client.config.DefaultConfiguration;
2921
import ch.swisscom.mid.client.impl.MIDClientImpl;
3022
import ch.swisscom.mid.client.model.*;
23+
import com.github.tomakehurst.wiremock.WireMockServer;
24+
import com.github.tomakehurst.wiremock.http.MimeType;
25+
import org.junit.jupiter.api.AfterAll;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
3128

3229
import static ch.swisscom.mid.client.rest.TestData.CUSTOM_AP_ID;
3330
import static ch.swisscom.mid.client.rest.TestData.CUSTOM_AP_PASSWORD;
3431
import static ch.swisscom.mid.client.rest.TestSupport.*;
35-
import static ch.swisscom.mid.client.rest.TestSupport.fileToBytes;
36-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
37-
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
38-
import static com.github.tomakehurst.wiremock.client.WireMock.post;
39-
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
32+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
4033
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
4134
import static org.hamcrest.MatcherAssert.assertThat;
4235
import static org.hamcrest.Matchers.is;
4336
import static org.hamcrest.Matchers.notNullValue;
44-
import static org.junit.jupiter.api.Assertions.fail;
37+
import static org.junit.jupiter.api.Assertions.*;
4538

4639
public class SyncSignatureTest {
4740

@@ -68,11 +61,11 @@ public static void tearDownThisClass() {
6861
@Test
6962
public void testSignature_success() {
7063
server.stubFor(
71-
post(urlEqualTo(DefaultConfiguration.REST_ENDPOINT_SUB_URL))
72-
.willReturn(
73-
aResponse()
74-
.withHeader("Content-Type", MimeType.JSON.toString())
75-
.withBody(fileToString("/samples/rest-response-signature.json"))));
64+
post(urlEqualTo(DefaultConfiguration.REST_ENDPOINT_SUB_URL))
65+
.willReturn(
66+
aResponse()
67+
.withHeader("Content-Type", MimeType.JSON.toString())
68+
.withBody(fileToString("/samples/rest-response-signature.json"))));
7669

7770
SignatureRequest signatureRequest = buildSignatureRequest();
7871
SignatureResponse response = client.requestSyncSignature(signatureRequest);
@@ -87,13 +80,13 @@ public void testSignature_success() {
8780
@Test
8881
public void testSignature_success_overrideApIdAndApPassword() {
8982
server.stubFor(
90-
post(urlEqualTo(DefaultConfiguration.REST_ENDPOINT_SUB_URL))
91-
.withRequestBody(containing("\"" + CUSTOM_AP_ID + "\""))
92-
.withRequestBody(containing("\"" + CUSTOM_AP_PASSWORD + "\""))
93-
.willReturn(
94-
aResponse()
95-
.withHeader("Content-Type", MimeType.JSON.toString())
96-
.withBody(fileToString("/samples/rest-response-signature.json"))));
83+
post(urlEqualTo(DefaultConfiguration.REST_ENDPOINT_SUB_URL))
84+
.withRequestBody(containing("\"" + CUSTOM_AP_ID + "\""))
85+
.withRequestBody(containing("\"" + CUSTOM_AP_PASSWORD + "\""))
86+
.willReturn(
87+
aResponse()
88+
.withHeader("Content-Type", MimeType.JSON.toString())
89+
.withBody(fileToString("/samples/rest-response-signature.json"))));
9790

9891
SignatureRequest signatureRequest = buildSignatureRequest();
9992
signatureRequest.setOverrideApId(CUSTOM_AP_ID);
@@ -113,12 +106,12 @@ public void testSignature_success_overrideApIdAndApPassword() {
113106
@Test
114107
public void testSignature_userCancel() {
115108
server.stubFor(
116-
post(urlEqualTo("/rest/service"))
117-
.willReturn(
118-
aResponse()
119-
.withStatus(500)
120-
.withHeader("Content-Type", MimeType.JSON.toString())
121-
.withBody(fileToString("/samples/rest-response-fault-user-cancel.json"))));
109+
post(urlEqualTo("/rest/service"))
110+
.willReturn(
111+
aResponse()
112+
.withStatus(500)
113+
.withHeader("Content-Type", MimeType.JSON.toString())
114+
.withBody(fileToString("/samples/rest-response-fault-user-cancel.json"))));
122115

123116
SignatureRequest signatureRequest = buildSignatureRequest();
124117
try {
@@ -135,12 +128,12 @@ public void testSignature_userCancel() {
135128
@Test
136129
public void testSignature_conFailure_responseTimeout() {
137130
server.stubFor(
138-
post(urlEqualTo("/rest/service"))
139-
.willReturn(
140-
aResponse()
141-
.withStatus(200)
142-
.withBody("")
143-
.withFixedDelay(5000)));
131+
post(urlEqualTo("/rest/service"))
132+
.willReturn(
133+
aResponse()
134+
.withStatus(200)
135+
.withBody("")
136+
.withFixedDelay(5000)));
144137

145138
SignatureRequest signatureRequest = buildSignatureRequest();
146139
try {
@@ -154,6 +147,46 @@ public void testSignature_conFailure_responseTimeout() {
154147
}
155148
}
156149

150+
@Test
151+
public void testSignatureWithDtbsAsTXNApproval_success() {
152+
server.stubFor(
153+
post(urlEqualTo(DefaultConfiguration.REST_ENDPOINT_SUB_URL))
154+
.willReturn(
155+
aResponse()
156+
.withHeader("Content-Type", MimeType.JSON.toString())
157+
.withBody(fileToString("/samples/rest-response-signature.json"))));
158+
159+
SignatureRequest signatureRequest = buildSignatureReqWithDTBDTXNApproval(
160+
"{\"type\":\"Address Change\",\"dtbd\":[{\"key\":\"Client\",\"value\":\"#CLIENT#\"},{\"key\":\"FN\",\"value\":\"Test User\"},{\"key\":\"Country\",\"value\":\"Poland\"},{\"key\":\"Session\",\"value\":\"#SESSION#\"}]}"
161+
);
162+
SignatureResponse response = client.requestSyncSignature(signatureRequest);
163+
assertThat(response.getStatus().getStatusCode(), is(StatusCode.SIGNATURE));
164+
assertThat(response.getStatus().getStatusCodeString(), is("500"));
165+
assertThat(response.getStatus().getStatusMessage(), is("SIGNATURE"));
166+
assertThat(response.getSignatureProfile(), is(TestData.CUSTOM_SIGNATURE_PROFILE));
167+
assertThat(response.getBase64Signature(), is(notNullValue()));
168+
assertThat(response.getBase64Signature().length(), is(TestData.BASE64_SIGNATURE_LENGTH));
169+
}
170+
171+
@Test
172+
public void testSignatureWithDtbsAsTXNApproval_invalidDtbsContent() {
173+
server.stubFor(
174+
post(urlEqualTo(DefaultConfiguration.REST_ENDPOINT_SUB_URL))
175+
.willReturn(
176+
aResponse()
177+
.withHeader("Content-Type", MimeType.JSON.toString())
178+
.withBody(fileToString("/samples/rest-response-signature.json"))));
179+
180+
SignatureRequest signatureRequest = buildSignatureReqWithDTBDTXNApproval(
181+
"{\"type2\":\"Address Change\",\"dtbd\":[{}]"
182+
);
183+
184+
Exception exception = assertThrows(DataAssemblyException.class, () -> {
185+
SignatureResponse response = client.requestSyncSignature(signatureRequest);
186+
});
187+
assertEquals("The DataToBeSigned format is not valid for mime type 'application/vnd.mobileid.txn-approval'", exception.getMessage());
188+
}
189+
157190
// ----------------------------------------------------------------------------------------------------
158191

159192
private static SignatureRequest buildSignatureRequest() {
@@ -167,4 +200,16 @@ private static SignatureRequest buildSignatureRequest() {
167200
request.addAdditionalService(new GeofencingAdditionalService());
168201
return request;
169202
}
203+
204+
private static SignatureRequest buildSignatureReqWithDTBDTXNApproval(String dtbs) {
205+
SignatureRequest request = new SignatureRequest();
206+
request.setUserLanguage(UserLanguage.ENGLISH);
207+
request.getDataToBeSigned().setData(dtbs);
208+
request.getDataToBeSigned().setEncodingToUtf8();
209+
request.getDataToBeSigned().setMimeType("application/vnd.mobileid.txn-approval");
210+
request.getMobileUser().setMsisdn(TrialNumbers.ONE_THAT_GIVES_MISSING_PARAM);
211+
request.setSignatureProfile(SignatureProfiles.DEFAULT_PROFILE);
212+
request.addAdditionalService(new GeofencingAdditionalService());
213+
return request;
214+
}
170215
}

0 commit comments

Comments
 (0)