Skip to content
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

Handling of expired access tokens when CF CC rate limiting exhausts #965

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,33 @@ public Flux<String> getRefreshTokens(ConnectionContext connectionContext) {

@Override
public final Mono<String> getToken(ConnectionContext connectionContext) {
return this.accessTokens.computeIfAbsent(connectionContext, this::token);
Mono<String> accessToken = this.accessTokens.get(connectionContext);
if(accessToken != null) {
try {
String token = this.accessTokens.get(connectionContext).map(s -> s.split(" ")[1]).block();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making a .block() call is a significant code smell in a reactive project (note that, outside of a few specific tests, it's not used anywhere else in this project). This needs to be refactored into a reactive style.

exctractClaimsFromToken(token)
.ifPresent(claims -> {
Date expirationTime = claims.getExpiration();
int i = expirationTime.compareTo(new Date());
long milliSeconds = expirationTime.getTime() - new Date().getTime();
// invalidate the token if it is going to be expired in one minute.
boolean isTokenInvalid = ((i <= 0) || (i == 1 && milliSeconds <= 60000));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inspection tells me that i == 1 (rather than i > 0 is bad practice).

if(isTokenInvalid) {
LOGGER.debug("Invalidating Access Token");
invalidate(connectionContext);
LOGGER.debug("Invalidated Access Token");
}
});

}catch (Exception e) {
LOGGER.debug("Invalidating Expired Access Token");
invalidate(connectionContext);
LOGGER.debug("Invalidated Expired Access Token");
}
return this.accessTokens.get(connectionContext);
}else {
return this.accessTokens.computeIfAbsent(connectionContext, this::token);
}
}

@Override
Expand Down Expand Up @@ -279,5 +305,12 @@ private static final class RefreshToken {
private FluxSink<String> sink = this.processor.sink();

}

private static Optional<Claims> exctractClaimsFromToken(String token) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo


String jws = token.substring(0, token.lastIndexOf('.') + 1);
return Optional.of(Jwts.parser().parseClaimsJwt(jws).getBody());

}

}