From 4c9a29f27b5a2ce72677abc921d0c716ab6289ad Mon Sep 17 00:00:00 2001 From: john Date: Mon, 24 Apr 2017 17:20:30 -0400 Subject: [PATCH] PB-1004. Support for authenticate signing --- .../com/silanis/esl/api/model/Signer.java | 45 ++++++++++++++++--- .../silanis/esl/sdk/AuthenticationMethod.java | 4 ++ .../main/java/com/silanis/esl/sdk/Signer.java | 14 ++++++ .../esl/sdk/builder/SignerBuilder.java | 24 ++++++++++ .../examples/AuthenticatedSigningExample.java | 41 +++++++++++++++++ .../esl/sdk/examples/UpdateSignerExample.java | 8 ++-- .../internal/converter/SignerConverter.java | 28 ++++++++++-- .../AuthenticationMethodConverterTest.java | 20 +++++++++ .../converter/SignerConverterTest.java | 9 ++++ .../AuthenticatedSigningExampleTest.java | 21 +++++++++ 10 files changed, 202 insertions(+), 12 deletions(-) create mode 100644 sdk/src/main/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExample.java create mode 100644 tester/src/test/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExampleTest.java diff --git a/sdk/src/main/java/com/silanis/esl/api/model/Signer.java b/sdk/src/main/java/com/silanis/esl/api/model/Signer.java index 7992a8699..388013772 100644 --- a/sdk/src/main/java/com/silanis/esl/api/model/Signer.java +++ b/sdk/src/main/java/com/silanis/esl/api/model/Signer.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.silanis.esl.api.util.JsonDateDeserializer; import com.silanis.esl.api.util.SchemaSanitizer; + +import java.util.ArrayList; import java.util.List; import java.util.Map; @JsonIgnoreProperties(ignoreUnknown=true) @@ -26,6 +28,8 @@ public class Signer extends User @JsonIgnore public static final String FIELD_DELIVERY = "delivery"; @JsonIgnore + public static final String FIELD_AUTHS = "auths"; + @JsonIgnore public static final String FIELD_EMAIL = "email"; @JsonIgnore public static final String FIELD_EXTERNAL = "external"; @@ -63,6 +67,7 @@ public Signer ( ) {} // Fields protected Auth _auth; + protected List _auths = new ArrayList(); protected Delivery _delivery; protected Group _group = null; protected KnowledgeBasedAuthentication _knowledgeBasedAuthentication = null; @@ -81,9 +86,9 @@ public Signer safeSetAddress( Address value ){ if ( value != null ) { this.setAddress( value ); } return this; } - - - + + + public Signer setAuth( Auth value ){ SchemaSanitizer.throwOnNull(FIELD_AUTH,value); // TODO With proper compare @@ -101,7 +106,7 @@ public Signer safeSetAuth( Auth value ){ public Auth getAuth(){ return _auth; } - + @Override @@ -164,8 +169,36 @@ public Signer safeSetDelivery( Delivery value ){ public Delivery getDelivery(){ return _delivery; } - - + + + public Signer setAuths(List value) { + SchemaSanitizer.throwOnNull(FIELD_AUTHS, value); + // TODO With proper compare + // if ( this._customFields == value ) return this; + this._auths = value; + setDirty(FIELD_AUTHS); + return this; + } + + // Used internally by aws. Invokes a the corresponding setter if the value is not null + @JsonIgnore + public Signer safeSetAuths( List value ){ + if ( value != null ) { this.setAuths(value); } + return this; + } + + // List adder + public Signer addAuth(Auth value) { + if (value == null) { throw new IllegalArgumentException("Argument cannot be null"); } + this._auths.add(value); + setDirty(FIELD_AUTHS); + return this; + } + + public List getAuths() { + return _auths; + } + @Override public Signer setEmail( String value ){ diff --git a/sdk/src/main/java/com/silanis/esl/sdk/AuthenticationMethod.java b/sdk/src/main/java/com/silanis/esl/sdk/AuthenticationMethod.java index cfa815df8..ae78c83af 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/AuthenticationMethod.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/AuthenticationMethod.java @@ -11,6 +11,8 @@ public class AuthenticationMethod extends EslEnumeration { public static final AuthenticationMethod CHALLENGE = new AuthenticationMethod("CHALLENGE", "CHALLENGE", 1); public static final AuthenticationMethod SMS = new AuthenticationMethod("SMS", "SMS", 2); public static final AuthenticationMethod KBA = new AuthenticationMethod("KBA", "KBA", 3); + public static final AuthenticationMethod CERTIFICATE = new AuthenticationMethod("CERTIFICATE", "CERTIFICATE", 4); + public static final AuthenticationMethod EXTERNAL = new AuthenticationMethod("EXTERNAL", "EXTERNAL", 5); /** * DO NOT USE! This is an internal implementation concern. It is there to avoid crashes in existing code when new values are added to the enumerations @@ -30,6 +32,8 @@ public static final AuthenticationMethod UNRECOGNIZED(String unknownValue){ sdkValues.put(CHALLENGE.name(), CHALLENGE); sdkValues.put(SMS.name(), SMS); sdkValues.put(KBA.name(), KBA); + sdkValues.put(CERTIFICATE.name(), CERTIFICATE); + sdkValues.put(EXTERNAL.name(), EXTERNAL); } private AuthenticationMethod(String apiValue, String sdkValue, int index) { diff --git a/sdk/src/main/java/com/silanis/esl/sdk/Signer.java b/sdk/src/main/java/com/silanis/esl/sdk/Signer.java index 65dcad9ca..8e2a84dee 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/Signer.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/Signer.java @@ -1,6 +1,7 @@ package com.silanis.esl.sdk; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; /** @@ -26,6 +27,7 @@ public class Signer implements Serializable { private boolean locked; private List attachments; private KnowledgeBasedAuthentication knowledgeBasedAuthentication; + private List authentications = new ArrayList(); /** *

The constructor of the Signer class.

@@ -122,6 +124,18 @@ public Authentication getAuthentication() { return authentication; } + public List getAuthentications() { + return authentications; + } + + public void addAuthentication(Authentication authentication) { + this.authentications.add(authentication); + } + + public void setAuthentications(List authentications) { + this.authentications = authentications; + } + /** * Accessor method used to retrieve the authentication method used by the signer * diff --git a/sdk/src/main/java/com/silanis/esl/sdk/builder/SignerBuilder.java b/sdk/src/main/java/com/silanis/esl/sdk/builder/SignerBuilder.java index 627a3460e..98c023fc8 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/builder/SignerBuilder.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/builder/SignerBuilder.java @@ -29,6 +29,7 @@ final public class SignerBuilder { private String placeholderName = null; private List attachments = new ArrayList(); private KnowledgeBasedAuthentication knowledgeBasedAuthentication; + private List authentications = new ArrayList(); /** *

The constructor of the SignerBuilderClass.

@@ -163,6 +164,7 @@ private Signer buildGroupSigner(){ result.setMessage(message); result.setId(id); result.setAttachmentRequirements(attachments); + result.setAuthentications(authentications); return result; } @@ -176,6 +178,7 @@ private Signer buildPlaceholderSigner(){ result.setCanChangeSigner(canChangeSigner); result.setMessage(message); result.setAttachmentRequirements(attachments); + result.setAuthentications(authentications); return result; } @@ -192,6 +195,7 @@ private Signer buildRegularSigner(){ result.setTitle(title); result.setCompany(company); result.setDeliverSignedDocumentsByEmail(deliverSignedDocumentsByEmail); + result.setAuthentications(authentications); result.setSigningOrder(signingOrder); result.setCanChangeSigner(canChangeSigner); @@ -304,6 +308,26 @@ public SignerBuilder canChangeSigner() { return this; } + /** + *

Sets the certificateSigning.

+ * @return the signer builder object itself + */ + public SignerBuilder withCertificateSigning() { + Authentication authentication = new Authentication(AuthenticationMethod.CERTIFICATE); + authentications.add(authentication); + return this; + } + + /** + *

Sets the externalSigning.

+ * @return the signer builder object itself + */ + public SignerBuilder withExternalSigning() { + Authentication authentication = new Authentication(AuthenticationMethod.EXTERNAL); + authentications.add(authentication); + return this; + } + /** * Sets the signer's email message they will receive in the email invitation to start the signing ceremony. * diff --git a/sdk/src/main/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExample.java b/sdk/src/main/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExample.java new file mode 100644 index 000000000..20f886fa5 --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExample.java @@ -0,0 +1,41 @@ +package com.silanis.esl.sdk.examples; + +import com.silanis.esl.sdk.DocumentPackage; +import com.silanis.esl.sdk.DocumentType; + +import static com.silanis.esl.sdk.builder.DocumentBuilder.newDocumentWithName; +import static com.silanis.esl.sdk.builder.PackageBuilder.newPackageNamed; +import static com.silanis.esl.sdk.builder.SignatureBuilder.signatureFor; +import static com.silanis.esl.sdk.builder.SignerBuilder.newSignerWithEmail; + +/** + * Created by schoi on 19/04/17. + */ +public class AuthenticatedSigningExample extends SDKSample { + + public DocumentPackage sentPackage; + + public static void main( String... args ) { + new AuthenticatedSigningExample().run(); + } + + public void execute() { + DocumentPackage superDuperPackage = newPackageNamed(getPackageName()) + .describedAs("This is a package created using the eSignLive SDK") + .withSigner(newSignerWithEmail(email1) + .withFirstName("John1") + .withLastName("Smith1") + .withCertificateSigning() + .withExternalSigning()) + .withDocument(newDocumentWithName("First Document") + .fromStream(documentInputStream1, DocumentType.PDF) + .withSignature(signatureFor(email1) + .onPage(0) + .atPosition(100, 100))) + .build(); + + packageId = eslClient.createPackage( superDuperPackage ); + eslClient.sendPackage(packageId); + sentPackage = eslClient.getPackage(packageId); + } +} diff --git a/sdk/src/main/java/com/silanis/esl/sdk/examples/UpdateSignerExample.java b/sdk/src/main/java/com/silanis/esl/sdk/examples/UpdateSignerExample.java index 08e98cdf5..7a1f8603b 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/examples/UpdateSignerExample.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/examples/UpdateSignerExample.java @@ -52,10 +52,12 @@ public void execute() { Signer signer3 = newSignerWithEmail(email3) .withFirstName(SIGNER3_FIRST_NAME) .withLastName(SIGNER3_LAST_NAME) + .withCertificateSigning() + .withExternalSigning() .challengedWithQuestions(firstQuestion(SIGNER3_FIRST_QUESTION) - .answer(SIGNER3_FIRST_ANSWER) - .secondQuestion(SIGNER3_SECOND_QUESTION) - .answer(SIGNER3_SECOND_ANSWER)) + .answer(SIGNER3_FIRST_ANSWER) + .secondQuestion(SIGNER3_SECOND_QUESTION) + .answer(SIGNER3_SECOND_ANSWER)) .withCustomId(SIGNER1_CUSTOM_ID).build(); Signer signer4 = newSignerWithEmail(email2) diff --git a/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/SignerConverter.java b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/SignerConverter.java index 7d65721e5..ef3b8ba90 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/SignerConverter.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/SignerConverter.java @@ -1,8 +1,10 @@ package com.silanis.esl.sdk.internal.converter; +import com.silanis.esl.api.model.Auth; import com.silanis.esl.api.model.BaseMessage; import com.silanis.esl.api.model.Delivery; import com.silanis.esl.api.model.Role; +import com.silanis.esl.sdk.Authentication; import com.silanis.esl.sdk.GroupId; import com.silanis.esl.sdk.Placeholder; import com.silanis.esl.sdk.Signer; @@ -10,6 +12,8 @@ import com.silanis.esl.sdk.internal.Asserts; import org.apache.commons.lang3.StringUtils; +import java.util.List; + /** * User: jessica * Date: 03/12/13 @@ -56,15 +60,26 @@ public com.silanis.esl.api.model.Signer toAPISigner() { com.silanis.esl.api.model.Signer result = new com.silanis.esl.api.model.Signer(); if ( !sdkSigner.isGroupSigner() ) { - result.setEmail( sdkSigner.getEmail() ) + result.setEmail(sdkSigner.getEmail()) .setFirstName(sdkSigner.getFirstName()) .setLastName(sdkSigner.getLastName()) .setTitle(sdkSigner.getTitle()) .setCompany(sdkSigner.getCompany()) .setKnowledgeBasedAuthentication(new KnowledgeBasedAuthenticationConverter(sdkSigner.getKnowledgeBasedAuthentication()).toAPIKnowledgeBasedAuthentication()) - .setDelivery( new Delivery().setEmail( sdkSigner.isDeliverSignedDocumentsByEmail() ) ); + .setDelivery(new Delivery().setEmail(sdkSigner.isDeliverSignedDocumentsByEmail())); + + if(!sdkSigner.getAuthentications().isEmpty()) { + List auths = result.getAuths(); + for(Authentication authentication : sdkSigner.getAuthentications()) { + Auth auth = new Auth(); + auth.setScheme(authentication.getMethod().name()); + auths.add(auth); + } + result.setAuths(auths); + } + } else { - result.setGroup( new com.silanis.esl.api.model.Group().setId( sdkSigner.getGroupId().toString() ) ); + result.setGroup(new com.silanis.esl.api.model.Group().setId(sdkSigner.getGroupId().toString())); } if ( sdkSigner.getId() != null ) { @@ -115,6 +130,13 @@ private Signer newRegularSignerFromAPIRole(){ Signer signer = signerBuilder.build(); + List auths = apiSigner.getAuths(); + if(auths != null && !auths.isEmpty()) { + for(Auth auth : auths) { + signer.addAuthentication(new AuthenticationConverter(auth).toSDKAuthentication()); + } + } + if ( apiRole.evalLocked() ) { signer.setLocked(true); } diff --git a/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/AuthenticationMethodConverterTest.java b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/AuthenticationMethodConverterTest.java index 5e33f4a9e..4319ae461 100644 --- a/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/AuthenticationMethodConverterTest.java +++ b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/AuthenticationMethodConverterTest.java @@ -88,6 +88,16 @@ public void convertAPIToSDK() { sdkAuthenticationMethod = converter.toSDKAuthMethod(); assertThat("AuthenticationMethod was not set correctly", sdkAuthenticationMethod, is(AuthenticationMethod.KBA)); + apiAuthenticationMethod = "CERTIFICATE"; + converter = new AuthenticationMethodConverter(apiAuthenticationMethod); + sdkAuthenticationMethod = converter.toSDKAuthMethod(); + assertThat("AuthenticationMethod was not set correctly", sdkAuthenticationMethod, is(AuthenticationMethod.CERTIFICATE)); + + apiAuthenticationMethod = "EXTERNAL"; + converter = new AuthenticationMethodConverter(apiAuthenticationMethod); + sdkAuthenticationMethod = converter.toSDKAuthMethod(); + assertThat("AuthenticationMethod was not set correctly", sdkAuthenticationMethod, is(AuthenticationMethod.EXTERNAL)); + apiAuthenticationMethod = "UNKNOWN"; converter = new AuthenticationMethodConverter(apiAuthenticationMethod); sdkAuthenticationMethod = converter.toSDKAuthMethod(); @@ -116,6 +126,16 @@ public void convertSDKToAPI() { apiAuthenticationMethod = converter.toAPIAuthMethod(); assertThat("AuthenticationMethod was not set correctly", apiAuthenticationMethod, is("KBA")); + sdkAuthenticationMethod = com.silanis.esl.sdk.AuthenticationMethod.CERTIFICATE; + converter = new AuthenticationMethodConverter(sdkAuthenticationMethod); + apiAuthenticationMethod = converter.toAPIAuthMethod(); + assertThat("AuthenticationMethod was not set correctly", apiAuthenticationMethod, is("CERTIFICATE")); + + sdkAuthenticationMethod = com.silanis.esl.sdk.AuthenticationMethod.EXTERNAL; + converter = new AuthenticationMethodConverter(sdkAuthenticationMethod); + apiAuthenticationMethod = converter.toAPIAuthMethod(); + assertThat("AuthenticationMethod was not set correctly", apiAuthenticationMethod, is("EXTERNAL")); + sdkAuthenticationMethod = com.silanis.esl.sdk.AuthenticationMethod.UNRECOGNIZED("UNKNOWN"); converter = new AuthenticationMethodConverter(sdkAuthenticationMethod); apiAuthenticationMethod = converter.toAPIAuthMethod(); diff --git a/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/SignerConverterTest.java b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/SignerConverterTest.java index 6d9b533cb..c4235585e 100644 --- a/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/SignerConverterTest.java +++ b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/SignerConverterTest.java @@ -11,6 +11,7 @@ import java.util.UUID; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; @@ -98,6 +99,8 @@ public void convertAPIToSDK() { assertThat("Last name was not correctly set", apiSigner1.getLastName(), is(equalTo(sdkSigner1.getLastName()))); assertThat("Company was not correctly set", apiSigner1.getCompany(), is(equalTo(sdkSigner1.getCompany()))); assertThat("Title was not correctly set", apiSigner1.getTitle(), is(equalTo(sdkSigner1.getTitle()))); + assertThat("Authenticated signing flag was not correctly set", apiSigner1.getAuths(), hasSize(1)); + assertThat("Authenticated signing flag was not correctly set", apiSigner1.getAuths().get(0).getScheme(), is("CERTIFICATE")); assertThat("Signer ID was not correctly set", apiRole.getId(), is(equalTo(sdkSigner1.getId()))); assertThat("Signing order was not correctly set", apiRole.getIndex(), is(equalTo(sdkSigner1.getSigningOrder()))); assertThat("Can change signer flag was not correctly set", apiRole.getReassign(), is(equalTo(sdkSigner1.canChangeSigner()))); @@ -226,6 +229,12 @@ private com.silanis.esl.api.model.Role createTypicalAPIRole() { apiSigner.setCompany("ABC Inc."); apiSigner.setTitle("Doctor"); + List auths = new ArrayList(); + Auth auth = new Auth(); + auth.setScheme("CERTIFICATE"); + auths.add(auth); + apiSigner.setAuths(auths); + Delivery delivery = new Delivery(); delivery.setDownload(true); delivery.setEmail(true); diff --git a/tester/src/test/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExampleTest.java b/tester/src/test/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExampleTest.java new file mode 100644 index 000000000..34c622a1a --- /dev/null +++ b/tester/src/test/java/com/silanis/esl/sdk/examples/AuthenticatedSigningExampleTest.java @@ -0,0 +1,21 @@ +package com.silanis.esl.sdk.examples; + +import org.junit.Test; + +/** + * Created by schoi on 19/04/17. + */ +public class AuthenticatedSigningExampleTest { + + @Test + public void verifyResult() { + AuthenticatedSigningExample example = new AuthenticatedSigningExample(); + example.run(); + +/* + assertThat("Authenticated Signing is not set correctly.", example.sentPackage.getSigner(example.email1).getAuthentications(), hasSize(2)); + assertThat("Authenticated Signing is not set correctly.", example.sentPackage.getSigner(example.email1).getAuthentications().get(0).getMethod(), is(AuthenticationMethod.CERTIFICATE)); + assertThat("Authenticated Signing is not set correctly.", example.sentPackage.getSigner(example.email1).getAuthentications().get(1).getMethod(), is(AuthenticationMethod.EXTERNAL)); +*/ + } +}