Skip to content

Commit

Permalink
overriden equal method for authstrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
sbansla committed Oct 3, 2024
1 parent 0f10880 commit be5dcce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/twilio/auth_strategy/BasicAuthStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Objects;

public class BasicAuthStrategy extends AuthStrategy {
private String username;
Expand All @@ -26,4 +27,18 @@ public String getAuthString() {
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);
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/twilio/auth_strategy/TokenAuthStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Objects;

public class TokenAuthStrategy extends AuthStrategy {
private String token;
Expand Down Expand Up @@ -35,13 +36,25 @@ public void fetchToken() {
synchronized (TokenAuthStrategy.class){
if (this.token == null || this.token.isEmpty() || isTokenExpired(this.token)) {
logger.info("Fetching new token for Apis");
System.out.println("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();
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/twilio/http/TwilioRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ public Response request(final Request request) {
if(response != null) {
int statusCode = response.getStatusCode();
if (statusCode == HTTP_STATUS_CODE_UNAUTHORIZED && authStrategy != null && EnumConstants.AuthType.TOKEN.equals(authStrategy.getAuthType())) {
((TokenAuthStrategy)authStrategy).fetchToken();
request.setAuth(authStrategy);
// Retry only once
response = httpClient.reliableRequest(request);
}
Expand Down

0 comments on commit be5dcce

Please sign in to comment.