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 4, 2023
1 parent 62920ed commit bc47453
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 99 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 @@ -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,16 +10,13 @@
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> authManagerSupplier) {
this.authManagers = authManagerSupplier;
}

public boolean checkAuthentication(
Expand Down Expand Up @@ -47,7 +44,7 @@ public boolean checkAuthentication(
String xTimeStampHeader = ciHeaders.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 Down
86 changes: 80 additions & 6 deletions client/src/main/com/sinch/sdk/models/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.sinch.sdk.core.models.ServerConfiguration;

/** Configuration used by Sinch Client */
/**
* Configuration used by Sinch Client
*/
public class Configuration {

private final String keyId;
Expand All @@ -13,6 +15,9 @@ public class Configuration {
private final SMSRegion smsRegion;
private final String smsUrl;
private final String verificationUrl;
private final String applicationKey;
private final String applicationSecret;


private Configuration(
String keyId,
Expand All @@ -22,7 +27,9 @@ private Configuration(
String numbersUrl,
SMSRegion smsRegion,
String smsUrl,
String verificationUrl) {
String verificationUrl,
String applicationKey,
String applicationSecret) {
this.keyId = keyId;
this.keySecret = keySecret;
this.projectId = projectId;
Expand All @@ -31,6 +38,8 @@ private Configuration(
this.smsRegion = null == smsRegion ? SMSRegion.US : smsRegion;
this.smsUrl = smsUrl;
this.verificationUrl = verificationUrl;
this.applicationKey = applicationKey;
this.applicationSecret = applicationSecret;
}

@Override
Expand Down Expand Up @@ -146,7 +155,7 @@ public ServerConfiguration getSmsServer() {
*
* @return SMS region
* @see <a
* href="https://developers.sinch.com/docs/sms/api-reference/#base-url/">https://developers.sinch.com/docs/sms/api-reference/#base-url/</a>
* href="https://developers.sinch.com/docs/sms/api-reference/#base-url/">https://developers.sinch.com/docs/sms/api-reference/#base-url/</a>
* @since 1.0
*/
public SMSRegion getSmsRegion() {
Expand All @@ -172,6 +181,7 @@ public String getSmsUrl() {
public ServerConfiguration getVerificationServer() {
return new ServerConfiguration(getVerificationUrl());
}

/**
* Verification URL
*
Expand All @@ -182,6 +192,38 @@ public String getVerificationUrl() {
return verificationUrl;
}

/**
* Application key to be used for Verification and Voice services
* <p>
* Use application secret in place of unified configuration for authentication (see Sinch
* dashboard for details) These credentials are related to Verification &amp; Voice Apps
*
* @return Application key
* @see <a
* href="https://developers.sinch.com/docs/verification/api-reference/authentication/">Sinch
* Documentation</a>
* @since 1.0
*/
public String getApplicationKey() {
return applicationKey;
}

/**
* Application secret to be used for Verification and Voice services
* <p>
* Use application secret in place of unified configuration for authentication (see Sinch
* dashboard for details) These credentials are related to Verification &amp; Voice Apps
*
* @return Application key
* @see <a
* href="https://developers.sinch.com/docs/verification/api-reference/authentication/">Sinch
* Documentation</a>
* @since 1.0
*/
public String getApplicationSecret() {
return applicationSecret;
}

public static Builder builder() {
return new Builder();
}
Expand All @@ -190,7 +232,9 @@ public static Builder builder(Configuration configuration) {
return new Builder(configuration);
}

/** Configuration builder */
/**
* Configuration builder
*/
public static class Builder {

private String keyId;
Expand All @@ -201,8 +245,11 @@ public static class Builder {
private SMSRegion smsRegion;
private String smsUrl;
private String verificationUrl;
private String applicationKey;
private String applicationSecret;

protected Builder() {}
protected Builder() {
}

/**
* Initialize a builder with existing configuration
Expand All @@ -219,6 +266,8 @@ protected Builder(Configuration configuration) {
this.smsRegion = configuration.getSmsRegion();
this.smsUrl = configuration.getSmsUrl();
this.verificationUrl = configuration.getVerificationUrl();
this.applicationKey = configuration.getApplicationKey();
this.applicationSecret = configuration.getApplicationSecret();
}

/**
Expand All @@ -229,7 +278,8 @@ protected Builder(Configuration configuration) {
*/
public Configuration build() {
return new Configuration(
keyId, keySecret, projectId, oauthUrl, numbersUrl, smsRegion, smsUrl, verificationUrl);
keyId, keySecret, projectId, oauthUrl, numbersUrl, smsRegion, smsUrl, verificationUrl,
applicationKey, applicationSecret);
}

/**
Expand Down Expand Up @@ -327,5 +377,29 @@ public Builder setVerificationUrl(String verificationUrl) {
this.verificationUrl = verificationUrl;
return this;
}

/**
* Set Application secret
*
* @param applicationKey Application key to be used
* @return Current builder
* @since 1.0
*/
public Builder setApplicationKey(String applicationKey) {
this.applicationKey = applicationKey;
return this;
}

/**
* Set Application secret
*
* @param applicationSecret Application secret to be used
* @return Current builder
* @since 1.0
*/
public Builder setApplicationSecret(String applicationSecret) {
this.applicationSecret = applicationSecret;
return this;
}
}
}
Loading

0 comments on commit bc47453

Please sign in to comment.