Skip to content

Commit

Permalink
chore: reseting account during oauth init
Browse files Browse the repository at this point in the history
  • Loading branch information
sbansla committed Sep 27, 2024
1 parent 2bc465f commit f8af734
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
27 changes: 18 additions & 9 deletions src/main/java/com/twilio/Twilio.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -123,6 +121,7 @@ public static synchronized void setUsername(final String username) {
if (!username.equals(Twilio.username)) {
Twilio.invalidate();
}
Twilio.invalidateOAuthCreds();

Twilio.username = username;
}
Expand All @@ -141,6 +140,7 @@ public static synchronized void setPassword(final String password) {
if (!password.equals(Twilio.password)) {
Twilio.invalidate();
}
Twilio.invalidateOAuthCreds();

Twilio.password = password;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/com/twilio/credential/ClientCredentialProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
36 changes: 25 additions & 11 deletions src/main/java/com/twilio/http/bearertoken/ApiTokenManager.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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();
}

Expand All @@ -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);
}
}

0 comments on commit f8af734

Please sign in to comment.