-
Notifications
You must be signed in to change notification settings - Fork 442
feat: Support for token based authentication and addition of helpers for organisation apis #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
af54ebd
Handling null pointers when meta info is null in a response
AsabuHere 12d20da
Add fetcher class under noauth package
AsabuHere 7c5cd6e
Implementation of token re fetch logic
AsabuHere f89ba9f
Implementation of token re fetch logic
AsabuHere ba5c794
Implementation of token re fetch logic
AsabuHere a91fb70
Making token fetch thread safe
AsabuHere 93f857b
Token re fetch logic implementation
AsabuHere 772f633
Token re fetch logic implementation
AsabuHere a88283b
Adding dummy file to avoid compilation errors. This will b e replaced…
AsabuHere fd9a2ba
Adding dummy file to avoid compilation errors. This will b e replaced…
AsabuHere f690533
adding token class files
AsabuHere 9acfcaa
adding token class files
AsabuHere 4171512
Adding null check for response
AsabuHere 8716eb1
uptaking review comments
AsabuHere a4ab362
Uptaking review comments
AsabuHere c046ba6
making token fetch thread safe
AsabuHere fd9731a
Making token fetch thread safe
AsabuHere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.