Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import static com.yoti.api.client.spi.remote.call.YotiConstants.DEFAULT_YOTI_HOST;
import static com.yoti.api.client.spi.remote.call.YotiConstants.PROPERTY_YOTI_DOCS_URL;
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
import static com.yoti.api.client.spi.remote.util.Validation.notNullOrEmpty;
import static com.yoti.validation.Validation.notNullOrEmpty;

import java.io.IOException;
import java.net.URISyntaxException;
Expand All @@ -19,6 +18,8 @@
import com.yoti.api.client.spi.remote.call.ResourceException;
import com.yoti.api.client.spi.remote.call.YotiHttpRequest;
import com.yoti.api.client.spi.remote.call.YotiHttpRequestBuilderFactory;
import com.yoti.api.client.spi.remote.call.factory.AuthStrategy;
import com.yoti.api.client.spi.remote.call.factory.AuthTokenStrategy;
import com.yoti.api.client.spi.remote.call.factory.DocsSignedRequestStrategy;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -34,10 +35,10 @@ public class DocScanSandboxClient {
private final ObjectMapper mapper;
private final YotiHttpRequestBuilderFactory yotiHttpRequestBuilderFactory;
private final String sdkId;
private final DocsSignedRequestStrategy authStrategy;
private final AuthStrategy authStrategy;

private DocScanSandboxClient(String sdkId,
DocsSignedRequestStrategy authStrategy,
AuthStrategy authStrategy,
ObjectMapper mapper,
YotiHttpRequestBuilderFactory yotiHttpRequestBuilderFactory) {
this.sdkId = sdkId;
Expand Down Expand Up @@ -107,10 +108,15 @@ public static class Builder {

private static final Logger LOGGER = LoggerFactory.getLogger(Builder.class);

private String authToken;
private String sdkId;
private KeyPair keyPair;

private Builder() {
private Builder() {}

public Builder withAuthenticationToken(String authenticationToken) {
this.authToken = authenticationToken;
return this;
}

public Builder withSdkId(String sdkId) {
Expand All @@ -130,10 +136,28 @@ public Builder withKeyPair(KeyPairSource keyPairSource) {

public DocScanSandboxClient build() {
notNullOrEmpty(sdkId, "sdkId");
notNull(keyPair, "keyPair");

return new DocScanSandboxClient(sdkId, new DocsSignedRequestStrategy(keyPair, sdkId), new ObjectMapper(), new YotiHttpRequestBuilderFactory());
if (authToken == null) {
validateForSignedRequest();
return new DocScanSandboxClient(sdkId, new DocsSignedRequestStrategy(keyPair, sdkId), new ObjectMapper(), new YotiHttpRequestBuilderFactory());
} else {
validateAuthToken();
return new DocScanSandboxClient(sdkId, new AuthTokenStrategy(authToken), new ObjectMapper(), new YotiHttpRequestBuilderFactory());
}
}

private void validateForSignedRequest() {
if (sdkId == null || sdkId.isEmpty() || keyPair == null) {
throw new IllegalStateException("An sdkId and KeyPairSource must be provided when not using an authentication token");
}
}

private void validateAuthToken() {
if (keyPair != null) {
throw new IllegalStateException("Must not supply KeyPairSource when using an authentication token");
}
}

}

}