-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for token based authentication and addition of helpers …
…for organisation apis (#797) * Handling null pointers when meta info is null in a response * Add fetcher class under noauth package * Making token fetch thread safe * Token re fetch logic implementation * adding token class files
- Loading branch information
Showing
18 changed files
with
627 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class BearerTokenAuthenticationExamples { | ||
public static void main { | ||
//Getting access token - Method #1 | ||
TwilioOrgsTokenAuth.init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET); | ||
|
||
//Getting access token - Method #2 | ||
//To provide custom token manager implementation | ||
//Need not call init method if customer token manager is passed | ||
//TwilioOrgsTokenAuth.setTokenManager(new OrgsTokenManager(grantType, clientId, clientSecret)); | ||
|
||
fetchAccountDetails(); | ||
} | ||
|
||
private static void fetchAccountDetails() { | ||
ResourceSet<Account> accountSet = Account.reader(ORGANISATION_ID).read(); | ||
String accountSid = accountSet.iterator().next().getAccountSid(); | ||
System.out.println(accountSid); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.twilio; | ||
|
||
import com.twilio.annotations.Preview; | ||
import com.twilio.exception.AuthenticationException; | ||
import com.twilio.http.bearertoken.BearerTokenTwilioRestClient; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import com.twilio.http.bearertoken.TokenManager; | ||
import com.twilio.http.bearertoken.OrgsTokenManager; | ||
|
||
@Preview | ||
public class TwilioOrgsTokenAuth { | ||
private static String accessToken; | ||
@Getter | ||
private static List<String> userAgentExtensions; | ||
private static String region = System.getenv("TWILIO_REGION"); | ||
private static String edge = System.getenv("TWILIO_EDGE"); | ||
private static volatile BearerTokenTwilioRestClient restClient; | ||
@Getter @Setter | ||
private static TokenManager tokenManager; | ||
|
||
private static volatile ExecutorService executorService; | ||
|
||
private TwilioOrgsTokenAuth() { | ||
} | ||
|
||
public static synchronized void init(String grantType, String clientId, String clientSecret) { | ||
validateAuthCredentials(grantType, clientId, clientSecret); | ||
tokenManager = new OrgsTokenManager(grantType, clientId, clientSecret); | ||
} | ||
public static synchronized void init(String grantType, String clientId, String clientSecret, String code, String redirectUri, String audience, String refreshToken, String scope) { | ||
validateAuthCredentials(grantType, clientId, clientSecret); | ||
tokenManager = new OrgsTokenManager(grantType, clientId, clientSecret, code, redirectUri, audience, refreshToken, scope); | ||
} | ||
|
||
private static void validateAuthCredentials(String grantType, String clientId, String clientSecret){ | ||
if (grantType == null) { | ||
throw new AuthenticationException("Grant Type cannot be null"); | ||
} | ||
if (clientId == null) { | ||
throw new AuthenticationException("Client Id cannot be null"); | ||
} | ||
if (clientSecret == null) { | ||
throw new AuthenticationException("Client Secret cannot be null"); | ||
} | ||
return; | ||
} | ||
|
||
public static BearerTokenTwilioRestClient getRestClient() { | ||
if (TwilioOrgsTokenAuth.restClient == null) { | ||
synchronized (TwilioOrgsTokenAuth.class) { | ||
if (TwilioOrgsTokenAuth.restClient == null) { | ||
TwilioOrgsTokenAuth.restClient = buildOAuthRestClient(); | ||
} | ||
} | ||
} | ||
return TwilioOrgsTokenAuth.restClient; | ||
} | ||
/** | ||
* Returns the Twilio executor service. | ||
* | ||
* @return the Twilio executor service | ||
*/ | ||
public static ExecutorService getExecutorService() { | ||
if (TwilioOrgsTokenAuth.executorService == null) { | ||
synchronized (TwilioOrgsTokenAuth.class) { | ||
if (TwilioOrgsTokenAuth.executorService == null) { | ||
TwilioOrgsTokenAuth.executorService = Executors.newCachedThreadPool(); | ||
} | ||
} | ||
} | ||
return TwilioOrgsTokenAuth.executorService; | ||
} | ||
|
||
private static BearerTokenTwilioRestClient buildOAuthRestClient() { | ||
|
||
BearerTokenTwilioRestClient.Builder builder = new BearerTokenTwilioRestClient.Builder(); | ||
|
||
if (userAgentExtensions != null) { | ||
builder.userAgentExtensions(TwilioOrgsTokenAuth.userAgentExtensions); | ||
} | ||
|
||
builder.region(TwilioOrgsTokenAuth.region); | ||
builder.edge(TwilioOrgsTokenAuth.edge); | ||
if(TwilioOrgsTokenAuth.tokenManager == null){ | ||
throw new AuthenticationException("Either initialize the authentications class or pass a custom token manager"); | ||
} | ||
builder.tokenManager(TwilioOrgsTokenAuth.tokenManager); | ||
|
||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Invalidates the volatile state held in the Twilio singleton. | ||
*/ | ||
private static void invalidate() { | ||
TwilioOrgsTokenAuth.restClient = null; | ||
TwilioOrgsTokenAuth.tokenManager = null; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.