-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestSearchClient.java
78 lines (59 loc) · 3.46 KB
/
TestSearchClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package test;
import gov.mda.Constants;
import gov.mda.saml.AssertionBuilder;
import gov.mda.saml.SAMLUtils;
import gov.mda.trustfabric.TrustFabric;
import gov.mda.util.RestServiceClient;
import java.io.FileInputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import org.opensaml.saml2.core.Assertion;
import org.opensaml.saml2.core.Attribute;
public class TestSearchClient {
/**
* @param args
*/
public static void main(String[] args) {
RestServiceClient m_client;
/* Strongly recommend that these be loaded from a configuration file dynamically in production code */
String miseCert = "C:\\Users\\user\\Documents\\ca\\ca.crt"; //public certificate for the MISE
String trustFabricUrl = "https://mise.mda.gov/miseresources/TrustFabric.xml"; //trust fabric URL on the MISE server
String trustFabricBackupPath = "C:\\Users\\user\\Documents\\TrustFabricBackup.xml"; //backup local file location for a cached version of the trust fabric
String serverScheme = "https";
String serverHost = "mise.mda.gov";
String serverPort = "9443";
String serverBasePath = "/services";
String keystorePath = "C:\\Users\\user\\Documents\\server.p12"; //keystore which contains the certificate and private key for this trusted system
String keystorePass = "password";
try {
FileInputStream isCert = new FileInputStream(FilenameUtils.separatorsToSystem(miseCert));
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(isCert);
TrustFabric.initializeFromURL(trustFabricUrl, trustFabricBackupPath, cert);
m_client = new RestServiceClient(serverScheme, serverHost, Integer.valueOf(serverPort), serverBasePath);
m_client.setClientCert(FilenameUtils.separatorsToSystem(keystorePath), keystorePass);
//Form the user assertion
String assertingPartyID = "test.client";
AssertionBuilder builder = new AssertionBuilder(assertingPartyID);
builder.addStandardConditions(Constants.MISE_AUDIENCE_RESTRICTION, 10*60); // valid for 10 minutes
builder.addAttribute("ElectronicIdentityId", "gfipm:2.0:user:ElectronicIdentityId", "[email protected]");
builder.addAttribute("FullName", "gfipm:2.0:user:FullName", "Test T. User");
Attribute attr = builder.addAttribute("CitizenshipCode", "mise:1.4:user:CitizenshipCode", "USA");
builder.addAttribute("LawEnforcementIndicator", "mise:1.4:user:LawEnforcementIndicator", "true");
builder.addAttribute("PrivacyProtectedIndicator", "mise:1.4:user:PrivacyProtectedIndicator", "true");
builder.signUsingPkcs12(assertingPartyID, FilenameUtils.separatorsToSystem(keystorePath), keystorePass);
Assertion assertion = builder.getAssertion();
//Important to not use SAMLUtils.asPrettyXMLString(object) as it will cause the signature validation to fail
HttpResponse response = m_client.post("/MDAUserSessionService/login", null, SAMLUtils.asXMLString(assertion), ContentType.APPLICATION_XML);
EntityUtils.consumeQuietly(response.getEntity());
response = m_client.get("/MDAService/search/pos?ulat=3.75&ulng=-2.0&llat=-2.75&llng=3.0&start=2012-06-10T12:10:00&end=2013-012-25T12:30:00", null, "");
//do something with the response
} catch(Exception e) {
e.printStackTrace();
}
}
}