Skip to content

Commit

Permalink
making token fetch thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Jun 21, 2024
1 parent a4ab362 commit c046ba6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/twilio/TwilioOrgsTokenAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TwilioOrgsTokenAuth {
private static String region = System.getenv("TWILIO_REGION");
private static String edge = System.getenv("TWILIO_EDGE");
private static volatile BearerTokenTwilioRestClient restClient;
@Setter
@Getter @Setter
private static TokenManager tokenManager;

private static volatile ExecutorService executorService;
Expand Down Expand Up @@ -87,7 +87,7 @@ private static BearerTokenTwilioRestClient buildOAuthRestClient() {
builder.region(TwilioOrgsTokenAuth.region);
builder.edge(TwilioOrgsTokenAuth.edge);
if(TwilioOrgsTokenAuth.tokenManager == null){
tokenManager = new OrgsTokenManager(grantType, clientId, clientSecret, code, redirectUri, audience, refreshToken, scope);
throw new AuthenticationException("Either initialize the authentications class or pass a custom token manager");
}
builder.tokenManager(TwilioOrgsTokenAuth.tokenManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public BearerTokenTwilioRestClient build() {
public Response request(BearerTokenRequest request) {

if (accessToken == null || accessToken.isEmpty() || isTokenExpired(this.accessToken)) {
this.accessToken = tokenManager.fetchAccessToken();
synchronized (BearerTokenTwilioRestClient.class){
this.accessToken = tokenManager.fetchAccessToken();
}
}

request.setAuth(accessToken);
Expand Down

0 comments on commit c046ba6

Please sign in to comment.