Skip to content

Commit

Permalink
feat: Align application credentials to current unified credentials: t…
Browse files Browse the repository at this point in the history
…hey are store at SinchClient level and a SinchClient instance can handle a single one application (like for single one project id)
  • Loading branch information
JPPortier committed Dec 12, 2023
1 parent 4a44ffb commit a7f7325
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 110 deletions.
3 changes: 2 additions & 1 deletion client/src/main/com/sinch/sdk/SinchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class SinchClient {

private static final String DEFAULT_PROPERTIES_FILE_NAME = "/config-default.properties";
private static final String VERSION_PROPERTIES_FILE_NAME = "/version.properties";
private static final String VERSION_PROPERTIES_FILE_NAME = "/version.properties";

private static final String OAUTH_URL_KEY = "oauth-url";
private static final String NUMBERS_SERVER_KEY = "numbers-server";
Expand Down Expand Up @@ -224,4 +224,5 @@ private String formatAuxiliaryFlag(String auxiliaryFlag) {
}
return String.join(",", values);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Collection<Pair<String, String>> getAuthorizationHeaders(

return Arrays.asList(
new Pair<>("Authorization", AUTH_KEYWORD + " " + key + ":" + encoded),
new Pair<>(XTIMESTAMP_HEADER, timestamp.toString()));
new Pair<>(XTIMESTAMP_HEADER, timestamp));
}

private String getBodyMD5Hash(String body) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@
*/
public interface VerificationService {

/**
* Use application secret in place of unified configuration for authentication (see Sinch
* dashboard for details) These credentials are related to Verification Apps
*
* @param key see <a href="https://dashboard.sinch.com/verification/apps">dashboard</a>
* @param secret see <a href="https://dashboard.sinch.com/verification/apps">dashboard</a>
* @return service instance for project
* @since 1.0
*/
VerificationService setApplicationCredentials(String key, String secret);

/**
* Verifications Service instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,26 @@
import com.sinch.sdk.domains.verification.models.VerificationReport;
import com.sinch.sdk.models.Configuration;
import java.util.Map;
import java.util.function.Supplier;

public class StatusService implements com.sinch.sdk.domains.verification.StatusService {

private final Configuration configuration;
private final HttpClient httpClient;
private final Supplier<Map<String, AuthManager>> authManagerSupplier;
private final QueryVerificationsApi api;

public StatusService(
Configuration configuration,
HttpClient httpClient,
Supplier<Map<String, AuthManager>> authManagerSupplier) {
this.configuration = configuration;
this.httpClient = httpClient;
this.authManagerSupplier = authManagerSupplier;
}

protected QueryVerificationsApi getApi() {
return new QueryVerificationsApi(
Map<String, AuthManager> authManagers) {
this.api = new QueryVerificationsApi(
httpClient,
configuration.getVerificationServer(),
authManagerSupplier.get(),
authManagers,
new HttpMapper());
}

protected QueryVerificationsApi getApi() {
return this.api;
}

public VerificationReport get(Identity identity, VerificationMethodType method) {
if (!(identity instanceof NumberIdentity)) {
throw new ApiException("Unexpected entity: " + identity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.sinch.sdk.domains.verification.WebHooksService;
import com.sinch.sdk.models.Configuration;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Supplier;

public class VerificationService implements com.sinch.sdk.domains.verification.VerificationService {

Expand All @@ -29,19 +29,21 @@ public class VerificationService implements com.sinch.sdk.domains.verification.V
private Map<String, AuthManager> clientAuthManagers;
private Map<String, AuthManager> webhooksAuthManagers;

private final Supplier<Map<String, AuthManager>> clientAuthManagersSupplier =
() -> clientAuthManagers;
private final Supplier<Map<String, AuthManager>> webhooksAuthManagersSupplier =
() -> webhooksAuthManagers;

public VerificationService(Configuration configuration, HttpClient httpClient) {

// Currently, we are not supporting unified credentials: ensure application credentials are defined
Objects.requireNonNull(configuration.getApplicationKey(), "'applicationKey' cannot be null");
Objects.requireNonNull(configuration.getApplicationSecret(),
"'applicationSecret' cannot be null");

this.configuration = configuration;
this.httpClient = httpClient;
setApplicationCredentials(configuration.getApplicationKey(),configuration.getApplicationSecret() );
}

public VerificationService setApplicationCredentials(String key, String secret) {
private void setApplicationCredentials(String key, String secret) {

AuthManager basicAuthManager = new BasicAuthManager(configuration);
AuthManager basicAuthManager = new BasicAuthManager(key, secret);
AuthManager applicationAuthManager = new VerificationApplicationAuthManager(key, secret);

boolean useApplicationAuth = true;
Expand All @@ -61,15 +63,14 @@ public VerificationService setApplicationCredentials(String key, String secret)
webhooksAuthManagers.put(BASIC_SECURITY_SCHEME_KEYWORD_VERIFICATION, basicAuthManager);
webhooksAuthManagers.put(
APPLICATION_SECURITY_SCHEME_KEYWORD_VERIFICATION, applicationAuthManager);
return this;
}

public VerificationsService verifications() {
if (null == this.verifications) {
checkCredentials();
this.verifications =
new com.sinch.sdk.domains.verification.adapters.VerificationsService(
configuration, httpClient, clientAuthManagersSupplier);
configuration, httpClient, clientAuthManagers);
}
return this.verifications;
}
Expand All @@ -79,7 +80,7 @@ public StatusService status() {
checkCredentials();
this.status =
new com.sinch.sdk.domains.verification.adapters.StatusService(
configuration, httpClient, clientAuthManagersSupplier);
configuration, httpClient, clientAuthManagers);
}
return this.status;
}
Expand All @@ -89,7 +90,7 @@ public WebHooksService webhooks() {
if (null == this.webhooks) {
this.webhooks =
new com.sinch.sdk.domains.verification.adapters.WebHooksService(
webhooksAuthManagersSupplier);
webhooksAuthManagers);
}
return this.webhooks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,27 @@
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponse;
import com.sinch.sdk.models.Configuration;
import java.util.Map;
import java.util.function.Supplier;

public class VerificationsService
implements com.sinch.sdk.domains.verification.VerificationsService {

private final Configuration configuration;
private final HttpClient httpClient;
private final Supplier<Map<String, AuthManager>> authManagerSupplier;
private final SendingAndReportingVerificationsApi api;

public VerificationsService(
Configuration configuration,
HttpClient httpClient,
Supplier<Map<String, AuthManager>> authManagerSupplier) {
this.configuration = configuration;
this.httpClient = httpClient;
this.authManagerSupplier = authManagerSupplier;
}

protected SendingAndReportingVerificationsApi getApi() {
return new SendingAndReportingVerificationsApi(
Map<String, AuthManager> authManagers) {
this.api = new SendingAndReportingVerificationsApi(
httpClient,
configuration.getVerificationServer(),
authManagerSupplier.get(),
authManagers,
new HttpMapper());
}

protected SendingAndReportingVerificationsApi getApi() {
return this.api;
}

public StartVerificationResponse start(StartVerificationRequestParameters parameters) {
return VerificationsDtoConverter.convert(
getApi().startVerification(VerificationsDtoConverter.convert(parameters)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;
import java.util.logging.Logger;

public class WebHooksService implements com.sinch.sdk.domains.verification.WebHooksService {
private static final Logger LOGGER = Logger.getLogger(WebHooksService.class.getName());

private final Supplier<Map<String, AuthManager>> authManagerSupplier;
private final Map<String, AuthManager> authManagers;

public WebHooksService(Supplier<Map<String, AuthManager>> authManagerSupplier) {
this.authManagerSupplier = authManagerSupplier;
public WebHooksService(Map<String, AuthManager> authManagers) {
this.authManagers = authManagers;
}

public boolean checkAuthentication(
String method, String path, Map<String, String> headers, String jsonPayload) {

// convert header keys to use case-insensitive map keys
Map<String, String> ciHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
ciHeaders.putAll(headers);
Map<String, String> caseInsensitiveHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
caseInsensitiveHeaders.putAll(headers);

String authorizationHeader = ciHeaders.get("Authorization");
String authorizationHeader = caseInsensitiveHeaders.get("Authorization");

// no authorization required
if (null == authorizationHeader) {
Expand All @@ -40,14 +37,21 @@ public boolean checkAuthentication(
String authorizationKeyword = split.length > 0 ? split[0] : "";
String authorizationHash = split.length > 1 ? split[1] : "";

String computedHash = computeHash(caseInsensitiveHeaders, authorizationKeyword, method, path, jsonPayload);

return computedHash.equals(authorizationHash);
}

private String computeHash(Map<String, String> caseInsensitiveHeaders, String authorizationKeyword,
String method, String path, String jsonPayload) {
// getting content type header
String contentTypeHeader = ciHeaders.getOrDefault("content-type", "");
String contentTypeHeader = caseInsensitiveHeaders.getOrDefault("content-type", "");

// getting x-timestamp header
String xTimeStampHeader = ciHeaders.get("x-timestamp");
String xTimeStampHeader = caseInsensitiveHeaders.get("x-timestamp");

// getting manager related to Authorization header value
AuthManager authManager = authManagerSupplier.get().get(authorizationKeyword);
AuthManager authManager = authManagers.get(authorizationKeyword);

// compute locally according to inputs
Collection<Pair<String, String>> computedHeaders =
Expand All @@ -62,9 +66,7 @@ public boolean checkAuthentication(
.map(Pair::getRight)
.orElse("");
String[] newSplit = computedAuthorization.split(" ");
String computedHash = newSplit.length > 1 ? newSplit[1] : "";

return computedHash.equals(authorizationHash);
return newSplit.length > 1 ? newSplit[1] : "";
}

@Override
Expand Down
Loading

0 comments on commit a7f7325

Please sign in to comment.