-
Notifications
You must be signed in to change notification settings - Fork 430
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
feat: Added OAuth Support for Public APIs with TokenManager Integration #813
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dd7006f
chore: added oauth functionality
sbansla aa293e0
fixed authstrategy code
sbansla 81d1758
added null check for authstrategy
sbansla 2bc465f
updated readme with OAuth feature for public APIs
sbansla f8af734
chore: reseting account during oauth init
sbansla 5176a49
chore: add logging when token is fetched
sbansla 8ee69fe
Merge branch 'main' into public-oauth-rest-client
sbansla 0850ea7
chore: added oauth api call example
sbansla cba6839
chore: added cluster test
sbansla 6ab9442
chore: corrected the exception msg
sbansla 58a022a
chore: added secret
sbansla 0f10880
chore: updated fetching token in getAuthString method
sbansla be5dcce
overriden equal method for authstrategy
sbansla db56638
update readme
sbansla 3bd1e60
updated readme
sbansla 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
``` | ||
import com.twilio.Twilio; | ||
import com.twilio.credential.ClientCredentialProvider; | ||
import com.twilio.rest.api.v2010.account.Message; | ||
|
||
public class FetchMessageUsingOAuth { | ||
public static void main(String[] args) { | ||
String clientId = "YOUR_CLIENT_ID"; | ||
String clientSecret = "YOUR_CLIENT_SECRET"; | ||
String accountSid = "YOUR_ACCOUNT_SID"; | ||
Twilio.init(new ClientCredentialProvider(clientId, clientSecret), accountSid); | ||
/* | ||
Or use the following if accountSid is not required as a path parameter for an API or when setting accountSid in the API. | ||
Twilio.init(new ClientCredentialProvider(clientId, clientSecret)); | ||
*/ | ||
String messageSid = "YOUR_MESSAGE_SID"; | ||
Message message = Message.fetcher(messageSid).fetch(); | ||
} | ||
} | ||
``` | ||
|
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 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,17 @@ | ||
package com.twilio.auth_strategy; | ||
|
||
import com.twilio.constant.EnumConstants; | ||
import lombok.Getter; | ||
|
||
public abstract class AuthStrategy { | ||
@Getter | ||
private EnumConstants.AuthType authType; | ||
|
||
public AuthStrategy(EnumConstants.AuthType authType) { | ||
this.authType = authType; | ||
} | ||
public abstract String getAuthString(); | ||
|
||
public abstract boolean requiresAuthentication(); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/twilio/auth_strategy/BasicAuthStrategy.java
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,44 @@ | ||
package com.twilio.auth_strategy; | ||
|
||
import com.twilio.constant.EnumConstants; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.Objects; | ||
|
||
public class BasicAuthStrategy extends AuthStrategy { | ||
private String username; | ||
private String password; | ||
|
||
public BasicAuthStrategy(String username, String password) { | ||
super(EnumConstants.AuthType.BASIC); | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
@Override | ||
public String getAuthString() { | ||
String credentials = username + ":" + password; | ||
String encoded = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.US_ASCII)); | ||
return "Basic " + encoded; | ||
} | ||
|
||
@Override | ||
public boolean requiresAuthentication() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
BasicAuthStrategy that = (BasicAuthStrategy) o; | ||
return Objects.equals(username, that.username) && | ||
Objects.equals(password, that.password); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(username, password); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/twilio/auth_strategy/NoAuthStrategy.java
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,20 @@ | ||
package com.twilio.auth_strategy; | ||
|
||
import com.twilio.constant.EnumConstants; | ||
|
||
public class NoAuthStrategy extends AuthStrategy { | ||
|
||
public NoAuthStrategy(String token) { | ||
super(EnumConstants.AuthType.NO_AUTH); | ||
} | ||
|
||
@Override | ||
public String getAuthString() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public boolean requiresAuthentication() { | ||
return false; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/twilio/auth_strategy/TokenAuthStrategy.java
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,66 @@ | ||
package com.twilio.auth_strategy; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import com.twilio.constant.EnumConstants; | ||
import com.twilio.http.bearertoken.TokenManager; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Date; | ||
import java.util.Objects; | ||
|
||
public class TokenAuthStrategy extends AuthStrategy { | ||
private String token; | ||
private TokenManager tokenManager; | ||
private static final Logger logger = LoggerFactory.getLogger(TokenAuthStrategy.class); | ||
public TokenAuthStrategy(TokenManager tokenManager) { | ||
super(EnumConstants.AuthType.TOKEN); | ||
this.tokenManager = tokenManager; | ||
} | ||
|
||
@Override | ||
public String getAuthString() { | ||
fetchToken(); | ||
return "Bearer " + token; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a null check here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call fetch token |
||
} | ||
|
||
@Override | ||
public boolean requiresAuthentication() { | ||
return true; | ||
} | ||
|
||
// Token-specific refresh logic | ||
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)) { | ||
logger.info("Fetching new token for Apis"); | ||
this.token = tokenManager.fetchAccessToken(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
TokenAuthStrategy that = (TokenAuthStrategy) o; | ||
return Objects.equals(token, that.token) && | ||
Objects.equals(tokenManager, that.tokenManager); | ||
} | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(token, tokenManager); | ||
} | ||
|
||
public boolean isTokenExpired(final String token) { | ||
DecodedJWT jwt = JWT.decode(token); | ||
Date expiresAt = jwt.getExpiresAt(); | ||
// Add a buffer of 30 seconds | ||
long bufferMilliseconds = 30 * 1000; | ||
Date bufferExpiresAt = new Date(expiresAt.getTime() - bufferMilliseconds); | ||
return bufferExpiresAt.before(new Date()); | ||
} | ||
} |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we mention the orgs auth is also in beta? since we changed back from preview?