-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added OAuth Support for Public APIs with TokenManager Integrati…
…on (#813) Added OAuth functionality for public APIs
- Loading branch information
Showing
21 changed files
with
501 additions
and
41 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
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; | ||
} | ||
|
||
@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.