Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVEXP-201: store application credentials at SinchClient configuration level #27

Merged
Show file tree
Hide file tree
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
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,14 +10,13 @@
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;

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

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(
Expand Down Expand Up @@ -52,7 +51,7 @@ private String computeHash(Map<String, String> ciHeaders, String authorizationKe
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
Loading