Skip to content

Commit 320c6fa

Browse files
committed
SDK-2771: Allow the DocScanSandboxClient to use authentication token in requests, instead of signed requests
1 parent d986e81 commit 320c6fa

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

yoti-sdk-sandbox/src/main/java/com/yoti/api/client/sandbox/docs/DocScanSandboxClient.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import static com.yoti.api.client.spi.remote.call.YotiConstants.DEFAULT_YOTI_HOST;
44
import static com.yoti.api.client.spi.remote.call.YotiConstants.PROPERTY_YOTI_DOCS_URL;
5-
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
6-
import static com.yoti.api.client.spi.remote.util.Validation.notNullOrEmpty;
5+
import static com.yoti.validation.Validation.notNullOrEmpty;
76

87
import java.io.IOException;
98
import java.net.URISyntaxException;
@@ -19,6 +18,8 @@
1918
import com.yoti.api.client.spi.remote.call.ResourceException;
2019
import com.yoti.api.client.spi.remote.call.YotiHttpRequest;
2120
import com.yoti.api.client.spi.remote.call.YotiHttpRequestBuilderFactory;
21+
import com.yoti.api.client.spi.remote.call.factory.AuthStrategy;
22+
import com.yoti.api.client.spi.remote.call.factory.AuthTokenStrategy;
2223
import com.yoti.api.client.spi.remote.call.factory.DocsSignedRequestStrategy;
2324

2425
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -34,10 +35,10 @@ public class DocScanSandboxClient {
3435
private final ObjectMapper mapper;
3536
private final YotiHttpRequestBuilderFactory yotiHttpRequestBuilderFactory;
3637
private final String sdkId;
37-
private final DocsSignedRequestStrategy authStrategy;
38+
private final AuthStrategy authStrategy;
3839

3940
private DocScanSandboxClient(String sdkId,
40-
DocsSignedRequestStrategy authStrategy,
41+
AuthStrategy authStrategy,
4142
ObjectMapper mapper,
4243
YotiHttpRequestBuilderFactory yotiHttpRequestBuilderFactory) {
4344
this.sdkId = sdkId;
@@ -107,10 +108,15 @@ public static class Builder {
107108

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

111+
private String authenticationToken;
110112
private String sdkId;
111113
private KeyPair keyPair;
112114

113-
private Builder() {
115+
private Builder() {}
116+
117+
public Builder withAuthenticationToken(String authenticationToken) {
118+
this.authenticationToken = authenticationToken;
119+
return this;
114120
}
115121

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

131137
public DocScanSandboxClient build() {
132138
notNullOrEmpty(sdkId, "sdkId");
133-
notNull(keyPair, "keyPair");
134139

135-
return new DocScanSandboxClient(sdkId, new DocsSignedRequestStrategy(keyPair, sdkId), new ObjectMapper(), new YotiHttpRequestBuilderFactory());
140+
if (authenticationToken == null) {
141+
validateForSignedRequest();
142+
return new DocScanSandboxClient(sdkId, new DocsSignedRequestStrategy(keyPair, sdkId), new ObjectMapper(), new YotiHttpRequestBuilderFactory());
143+
} else {
144+
validateAuthToken();
145+
return new DocScanSandboxClient(sdkId, new AuthTokenStrategy(authenticationToken), new ObjectMapper(), new YotiHttpRequestBuilderFactory());
146+
}
147+
}
148+
149+
private void validateForSignedRequest() {
150+
if (sdkId == null || sdkId.isEmpty() || keyPair == null) {
151+
throw new IllegalStateException("An sdkId and KeyPairSource must be provided when not using an authentication token");
152+
}
136153
}
154+
155+
private void validateAuthToken() {
156+
if (keyPair != null) {
157+
throw new IllegalStateException("Must not supply KeyPairSource when using an authentication token");
158+
}
159+
}
160+
137161
}
138162

139163
}

0 commit comments

Comments
 (0)