Skip to content

Commit

Permalink
Merge branch 'Hemuu:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemuu authored May 9, 2023
2 parents 935324a + 140f826 commit a7cd037
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
17 changes: 17 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,22 @@
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1680091733281</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
1 change: 1 addition & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>laverca-rest</groupId>
<artifactId>laverca-rest</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<name>Laverca REST MSS client</name>

<build>
Expand Down Expand Up @@ -177,4 +177,4 @@

</dependencies>

</project>
</project>
4 changes: 4 additions & 0 deletions src/main/java/fi/methics/laverca/rest/DocxSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public ByteArrayOutputStream signDocument(final String msisdn,

MssCertificate cert = this.client.getCertificate(msisdn, signatureProfile);

if (cert.getCertificate() == null) {
throw new MssRestException(MssRestException.UNKNOWN_USER, "Failed to get user certificate");
}

SignatureConfig signatureConfig = new SignatureConfig();
signatureConfig.setSigningCertificateChain(cert.getCertificateChain());
signatureConfig.setIncludeEntireCertificateChain(true);
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/fi/methics/laverca/rest/MssClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ public MSS_SignatureResp sign(final MSS_SignatureReq req) throws MssRestExceptio
*/
public static class Builder {

private String resturl;
private String restUrl;
private String secondaryUrl;
private String apid;
private String apname;
private String password;
Expand All @@ -336,18 +337,29 @@ public MssClient build() {
client.client.setApName(this.apname);
client.client.setPassword(this.password);
}
client.client.setRestUrl(this.resturl);
client.client.setRestUrl(this.restUrl);
client.client.setSecondaryUrl(this.secondaryUrl);
client.appwd = this.appwd;
return client;
}

/**
* Set RESTAPI service URL
* Set primary RESTAPI service URL
* @param resturl RESTAPI service URL
* @return this builder
*/
public Builder withRestUrl(String resturl) {
this.resturl = resturl;
this.restUrl = resturl;
return this;
}

/**
* Set alternative RESTAPI service URL
* @param alternateUrl RESTAPI service URL
* @return this builder
*/
public Builder withSecondaryUrl(String alternateUrl) {
this.secondaryUrl = alternateUrl;
return this;
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/fi/methics/laverca/rest/PdfSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import fi.methics.laverca.rest.util.DocumentSigner;
import fi.methics.laverca.rest.util.LavercaPAdESService;
import fi.methics.laverca.rest.util.MssCertificate;
import fi.methics.laverca.rest.util.MssRestException;
import fi.methics.laverca.rest.util.SignatureProfile;

/**
Expand Down Expand Up @@ -205,6 +206,11 @@ private void addSignatureField(final String msisdn, final PDAcroForm acroForm, f
*/
private PAdESSignatureParameters createParams(String msisdn, SignatureProfile sigprof) {
MssCertificate cert = this.client.getCertificate(msisdn, sigprof);

if (cert.getCertificate() == null) {
throw new MssRestException(MssRestException.UNKNOWN_USER, "Failed to get user certificate");
}

PAdESSignatureParameters parameters = new PAdESSignatureParameters();
parameters.setSignatureLevel(SignatureLevel.PAdES_BASELINE_B);
parameters.setSignaturePackaging(SignaturePackaging.ENVELOPED);
Expand Down
66 changes: 52 additions & 14 deletions src/main/java/fi/methics/laverca/rest/util/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;

Expand Down Expand Up @@ -37,6 +38,7 @@ public class RestClient {
private HttpClient httpClient;

private String resturl;
private String secondaryUrl;

private String apid;
private String apikey;
Expand Down Expand Up @@ -71,6 +73,10 @@ public void setRestUrl(final String resturl) {
this.resturl = resturl;
}

public void setSecondaryUrl(final String resturl) {
this.resturl = resturl;
}

public void setAuthnMode(final AuthnMode mode) {
this.mode = mode;
}
Expand Down Expand Up @@ -135,6 +141,15 @@ private String sendHmacReq(final String req) throws MssRestException {
return client.send(req, this.resturl);
} catch (IOException e) {
log.error("Connection to " + this.resturl + " failed: " + e.getMessage());
try {
if (this.secondaryUrl != null) {
HmacHttpClient client = new HmacHttpClient(this.httpClient, userid, apikey);
return client.send(req, this.secondaryUrl);
}
} catch (IOException e2) {
log.error("Connection to " + this.secondaryUrl + " failed: " + e.getMessage());
throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e2.getMessage());
}
throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage());
}
}
Expand All @@ -151,22 +166,19 @@ private String sendHmacReq(final String req) throws MssRestException {
private String sendBasicReq(final String req) throws MssRestException {

try {
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.apname, this.password));
HttpClientContext ctx = HttpClientContext.create();

URL url = new URL(this.resturl);
AuthCache authCache = new BasicAuthCache();
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
authCache.put(targetHost, new BasicScheme());

ctx.setAuthCache(authCache);
ctx.setCredentialsProvider(provider);

HttpPost post = this.createPost(req, this.resturl);
return this.getResponseBody(this.httpClient.execute(post, ctx));
return this.getResponseBody(this.httpClient.execute(post, this.createContext(this.resturl)));
} catch (IOException e) {
log.error("Connection to " + this.resturl + " failed (TestUtil): " + e.getMessage());
log.error("Connection to " + this.resturl + " failed: " + e.getMessage());
if (this.secondaryUrl != null) {
try {
HttpPost post = this.createPost(req, this.secondaryUrl);
return this.getResponseBody(this.httpClient.execute(post, this.createContext(this.secondaryUrl)));
} catch (IOException e2) {
log.error("Connection to " + this.secondaryUrl + " failed: " + e.getMessage());
throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage());
}
}
throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage());
}
}
Expand All @@ -185,6 +197,32 @@ private String getUserId(final String apid) {
}
}

/**
* Create a HTTP client context for given URL
* @param _url URL
* @return Context
* @throws MssRestException
*/
private HttpClientContext createContext(final String _url) throws MssRestException {
HttpClientContext ctx = HttpClientContext.create();
try {
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.apname, this.password));

URL url = new URL(_url);
AuthCache authCache = new BasicAuthCache();
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
authCache.put(targetHost, new BasicScheme());

ctx.setAuthCache(authCache);
ctx.setCredentialsProvider(provider);
} catch (MalformedURLException e) {
log.error("Connection to " + _url + " failed: " + e.getMessage());
throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage());
}
return ctx;
}

/**
* Create a HTTP Post
* @param req Request as String
Expand Down

0 comments on commit a7cd037

Please sign in to comment.