diff --git a/src/main/java/com/twilio/Twilio.java b/src/main/java/com/twilio/Twilio.java index 72ccf4525..012655c1c 100644 --- a/src/main/java/com/twilio/Twilio.java +++ b/src/main/java/com/twilio/Twilio.java @@ -2,8 +2,6 @@ import com.twilio.annotations.Beta; import com.twilio.auth_strategy.AuthStrategy; -import com.twilio.constant.EnumConstants; -import com.twilio.credential.ClientCredentialProvider; import com.twilio.exception.ApiException; import com.twilio.exception.AuthenticationException; import com.twilio.exception.CertificateValidationException; @@ -13,8 +11,6 @@ import com.twilio.http.Request; import com.twilio.http.Response; import com.twilio.http.TwilioRestClient; -import com.twilio.http.bearertoken.ApiTokenManager; -import com.twilio.http.bearertoken.TokenManager; import lombok.Getter; import java.util.ArrayList; @@ -76,6 +72,7 @@ public static synchronized void init(final String username, final String passwor @Beta public static synchronized void init(final CredentialProvider credentialProvider) { Twilio.setCredentialProvider(credentialProvider); + Twilio.setAccountSid(null); } @Beta @@ -88,11 +85,12 @@ private static void setCredentialProvider(final CredentialProvider credentialPro if (credentialProvider == null) { throw new AuthenticationException("Credential Provider can not be null"); } - - if (!credentialProvider.equals(Twilio.credentialProvider)) { // TODO: Write equals method in credential provider implementation class. + + if (!credentialProvider.equals(Twilio.credentialProvider)) { Twilio.invalidate(); } - // TODO: In case of Basic Auth, How to set account_sid ? + // Invalidate Basic Creds as they might be initialized via environment variables. + invalidateBasicCreds(); Twilio.credentialProvider = credentialProvider; } @@ -123,6 +121,7 @@ public static synchronized void setUsername(final String username) { if (!username.equals(Twilio.username)) { Twilio.invalidate(); } + Twilio.invalidateOAuthCreds(); Twilio.username = username; } @@ -141,6 +140,7 @@ public static synchronized void setPassword(final String password) { if (!password.equals(Twilio.password)) { Twilio.invalidate(); } + Twilio.invalidateOAuthCreds(); Twilio.password = password; } @@ -215,13 +215,13 @@ private static TwilioRestClient buildRestClient() { if (Twilio.username == null || Twilio.password == null) { if (credentialProvider == null) { throw new AuthenticationException( - "TwilioRestClient was used before AccountSid and AuthToken were set, please call Twilio.init()" + "Credentials have not been initialized or changed, please call Twilio.init()" ); } } TwilioRestClient.Builder builder; if (credentialProvider != null) { - AuthStrategy authStrategy = credentialProvider.toAuthStrategy(); // Does Code need to be thread safe ? + AuthStrategy authStrategy = credentialProvider.toAuthStrategy(); builder = new TwilioRestClient.Builder(authStrategy); } else { builder = new TwilioRestClient.Builder(Twilio.username, Twilio.password); @@ -312,6 +312,15 @@ private static void invalidate() { Twilio.restClient = null; } + private static void invalidateOAuthCreds() { + Twilio.credentialProvider = null; + } + + private static void invalidateBasicCreds() { + Twilio.username = null; + Twilio.password = null; + } + /** * Attempts to gracefully shutdown the ExecutorService if it is present. */ diff --git a/src/main/java/com/twilio/auth_strategy/TokenAuthStrategy.java b/src/main/java/com/twilio/auth_strategy/TokenAuthStrategy.java index 47ad82c8e..e1f20f261 100644 --- a/src/main/java/com/twilio/auth_strategy/TokenAuthStrategy.java +++ b/src/main/java/com/twilio/auth_strategy/TokenAuthStrategy.java @@ -32,7 +32,8 @@ public void fetchToken() { if (this.token == null || this.token.isEmpty() || isTokenExpired(this.token)) { synchronized (TokenAuthStrategy.class){ if (this.token == null || this.token.isEmpty() || isTokenExpired(this.token)) { - this.token = tokenManager.fetchAccessToken(); // TODO: Exceptional handling + + this.token = tokenManager.fetchAccessToken(); } } } diff --git a/src/main/java/com/twilio/credential/ClientCredentialProvider.java b/src/main/java/com/twilio/credential/ClientCredentialProvider.java index 128d196b7..81b4a03ad 100644 --- a/src/main/java/com/twilio/credential/ClientCredentialProvider.java +++ b/src/main/java/com/twilio/credential/ClientCredentialProvider.java @@ -6,20 +6,17 @@ import com.twilio.auth_strategy.TokenAuthStrategy; import com.twilio.constant.EnumConstants; import com.twilio.exception.AuthenticationException; -import com.twilio.http.Request; + import com.twilio.http.bearertoken.ApiTokenManager; import com.twilio.http.bearertoken.TokenManager; -import com.twilio.rest.oauth.v1.Token; -import lombok.Getter; -import lombok.Setter; + +import java.util.Objects; @Beta public class ClientCredentialProvider extends CredentialProvider { private String grantType; private String clientId; private String clientSecret; - @Setter - @Getter private TokenManager tokenManager; public ClientCredentialProvider(String clientId, String clientSecret) { @@ -54,4 +51,20 @@ public AuthStrategy toAuthStrategy() { } return new TokenAuthStrategy(tokenManager); } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + ClientCredentialProvider other = (ClientCredentialProvider) o; + return Objects.equals(clientId, other.clientId) && + Objects.equals(clientSecret, other.clientSecret) && + Objects.equals(tokenManager, other.tokenManager); + } } diff --git a/src/main/java/com/twilio/http/bearertoken/ApiTokenManager.java b/src/main/java/com/twilio/http/bearertoken/ApiTokenManager.java index 56ce633e0..38ca3129b 100644 --- a/src/main/java/com/twilio/http/bearertoken/ApiTokenManager.java +++ b/src/main/java/com/twilio/http/bearertoken/ApiTokenManager.java @@ -1,10 +1,11 @@ package com.twilio.http.bearertoken; import com.twilio.annotations.Beta; -import com.twilio.exception.ApiException; import com.twilio.rest.previewiam.v1.Token; import com.twilio.rest.previewiam.v1.TokenCreator; +import java.util.Objects; + @Beta public class ApiTokenManager implements TokenManager { @@ -19,21 +20,13 @@ public class ApiTokenManager implements TokenManager { @Override public String fetchAccessToken() { - TokenCreator tokenCreator = Token.creator(grantType, clientId).setClientSecret(clientSecret); // TODO: Change this + TokenCreator tokenCreator = Token.creator(grantType, clientId).setClientSecret(clientSecret); if (this.code != null) tokenCreator.setCode(code); if (this.redirectUri != null) tokenCreator.setRedirectUri(redirectUri); if (this.audience != null) tokenCreator.setAudience(audience); if (this.refreshToken != null) tokenCreator.setRefreshToken(refreshToken); if (this.scope != null) tokenCreator.setScope(scope); - Token token; - try { - token = tokenCreator.create(); - if(token == null || token.getAccessToken() == null){ - throw new ApiException("Token creation failed"); - } - } catch(Exception e){ - throw new ApiException("Token creation failed"); - } + Token token = tokenCreator.create(); return token.getAccessToken(); } @@ -53,4 +46,25 @@ public ApiTokenManager(String grantType, String clientId, String clientSecret, S this.refreshToken = refreshToken; this.scope = scope; } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + ApiTokenManager other = (ApiTokenManager) o; + return Objects.equals(grantType, other.grantType) && + Objects.equals(clientId, other.clientId) && + Objects.equals(clientSecret, other.clientSecret) && + Objects.equals(code, other.code) && + Objects.equals(redirectUri, other.redirectUri) && + Objects.equals(audience, other.audience) && + Objects.equals(refreshToken, other.refreshToken) && + Objects.equals(scope, other.scope); + } }