Skip to content

Commit

Permalink
Token re fetch logic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Jun 14, 2024
1 parent a91fb70 commit 93f857b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
15 changes: 15 additions & 0 deletions examples/BearerTokenAuthentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class BearerTokenAuthenticationExamples {
public static void main {
//Getting access token
Token token = Token.creator(GRANT_TYPE, CLIENT_ID).setClientSecret(CLIENT_SECRET).create();

TwilioBearerTokenAuth.init(token.getAccessToken());
fetchAccountDetails();
}

private static void fetchAccountDetails() {
ResourceSet<Account> accountSet = Account.reader(ORGANISATION_ID).read();
String accountSid = accountSet.iterator().next().getAccountSid();
System.out.println(accountSid);
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/twilio/TwilioBearerTokenAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ public class TwilioBearerTokenAuth {
private TwilioBearerTokenAuth() {
}

public static synchronized void init(final String accessToken, final TokenManager tokenManager) {
if (accessToken == null || accessToken.isEmpty()) {
throw new AuthenticationException("Access Token can not be null or Empty");
public static synchronized void init(final TokenManager tokenManager) {
if (tokenManager == null) {
throw new AuthenticationException("Token Manager cannot be null");
}
if (!accessToken.equals(TwilioBearerTokenAuth.accessToken)) {
TwilioBearerTokenAuth.invalidate();
}
TwilioBearerTokenAuth.accessToken = accessToken;
TwilioBearerTokenAuth.tokenManager = tokenManager;
TwilioBearerTokenAuth.accessToken = tokenManager.fetchAccessToken();
}

public static BearerTokenTwilioRestClient getRestClient() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/twilio/base/noauth/Creator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.twilio.Twilio;
import com.twilio.TwilioNoAuth;
import com.twilio.base.Resource;
import com.twilio.base.noauth.Resource;
import com.twilio.http.noauth.NoAuthTwilioRestClient;

import java.util.concurrent.CompletableFuture;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/twilio/base/noauth/Fetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.twilio.Twilio;

import com.twilio.TwilioNoAuth;
import com.twilio.base.Resource;
import com.twilio.base.noauth.Resource;
import com.twilio.http.noauth.NoAuthTwilioRestClient;

import java.util.concurrent.CompletableFuture;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/twilio/base/noauth/Resource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.twilio.base.noauth;

import java.io.Serializable;

public abstract class Resource implements Serializable {

private static final long serialVersionUID = -5898012691404059592L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface TokenManager {
public String refreshAccessToken(String refreshToken);
public synchronized String fetchAccessToken();
public String fetchAccessToken();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String refreshAccessToken(String refreshToken){
return token.getAccessToken();
}

public synchronized String fetchAccessToken(){
public String fetchAccessToken(){
Token token = Token.creator(grantType, clientId).setClientSecret(clientSecret).create();
return token.getAccessToken();
}
Expand Down

0 comments on commit 93f857b

Please sign in to comment.