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

[Java] [Feign] Fix duplicated Authorization headers when renewing a token on a retry #11513

Merged
Show file tree
Hide file tree
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 @@ -10,14 +10,17 @@ import java.util.Collection;
{{>generatedAnnotation}}
public abstract class OAuth implements RequestInterceptor {

//https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
static final int LEEWAY_SENCONDS = 10;

static final int MILLIS_PER_SECOND = 1000;

public interface AccessTokenListener {
void notify(OAuth2AccessToken token);
}

private volatile String accessToken;
private Long expirationTimeMillis;
private Long expirationTimeSeconds;
private AccessTokenListener accessTokenListener;

protected OAuth20Service service;
Expand All @@ -39,6 +42,7 @@ public abstract class OAuth implements RequestInterceptor {
}
String accessToken = getAccessToken();
if (accessToken != null) {
template.removeHeader("Authorization");
template.header("Authorization", "Bearer " + accessToken);
}
}
Expand Down Expand Up @@ -73,7 +77,7 @@ public abstract class OAuth implements RequestInterceptor {

public synchronized String getAccessToken() {
// If first time, get the token
if (expirationTimeMillis == null || System.currentTimeMillis() >= expirationTimeMillis) {
if (expirationTimeSeconds == null || System.currentTimeMillis() >= expirationTimeSeconds) {
updateAccessToken();
}
return accessToken;
Expand All @@ -86,7 +90,7 @@ public abstract class OAuth implements RequestInterceptor {
*/
public synchronized void setAccessToken(String accessToken, Integer expiresIn) {
this.accessToken = accessToken;
this.expirationTimeMillis = expiresIn == null ? null : System.currentTimeMillis() + expiresIn * MILLIS_PER_SECOND;
this.expirationTimeSeconds = expiresIn == null ? null : System.currentTimeMillis() / MILLIS_PER_SECOND + expiresIn - LEEWAY_SENCONDS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public abstract class OAuth implements RequestInterceptor {

//https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
static final int LEEWAY_SENCONDS = 10;

static final int MILLIS_PER_SECOND = 1000;

public interface AccessTokenListener {
void notify(OAuth2AccessToken token);
}

private volatile String accessToken;
private Long expirationTimeMillis;
private Long expirationTimeSeconds;
private AccessTokenListener accessTokenListener;

protected OAuth20Service service;
Expand All @@ -39,6 +42,7 @@ public void apply(RequestTemplate template) {
}
String accessToken = getAccessToken();
if (accessToken != null) {
template.removeHeader("Authorization");
template.header("Authorization", "Bearer " + accessToken);
}
}
Expand Down Expand Up @@ -73,7 +77,7 @@ public synchronized void registerAccessTokenListener(AccessTokenListener accessT

public synchronized String getAccessToken() {
// If first time, get the token
if (expirationTimeMillis == null || System.currentTimeMillis() >= expirationTimeMillis) {
if (expirationTimeSeconds == null || System.currentTimeMillis() >= expirationTimeSeconds) {
updateAccessToken();
}
return accessToken;
Expand All @@ -86,7 +90,7 @@ public synchronized String getAccessToken() {
*/
public synchronized void setAccessToken(String accessToken, Integer expiresIn) {
this.accessToken = accessToken;
this.expirationTimeMillis = expiresIn == null ? null : System.currentTimeMillis() + expiresIn * MILLIS_PER_SECOND;
this.expirationTimeSeconds = expiresIn == null ? null : System.currentTimeMillis() / MILLIS_PER_SECOND + expiresIn - LEEWAY_SENCONDS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public abstract class OAuth implements RequestInterceptor {

//https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
static final int LEEWAY_SENCONDS = 10;

static final int MILLIS_PER_SECOND = 1000;

public interface AccessTokenListener {
void notify(OAuth2AccessToken token);
}

private volatile String accessToken;
private Long expirationTimeMillis;
private Long expirationTimeSeconds;
private AccessTokenListener accessTokenListener;

protected OAuth20Service service;
Expand All @@ -39,6 +42,7 @@ public void apply(RequestTemplate template) {
}
String accessToken = getAccessToken();
if (accessToken != null) {
template.removeHeader("Authorization");
template.header("Authorization", "Bearer " + accessToken);
}
}
Expand Down Expand Up @@ -73,7 +77,7 @@ public synchronized void registerAccessTokenListener(AccessTokenListener accessT

public synchronized String getAccessToken() {
// If first time, get the token
if (expirationTimeMillis == null || System.currentTimeMillis() >= expirationTimeMillis) {
if (expirationTimeSeconds == null || System.currentTimeMillis() >= expirationTimeSeconds) {
updateAccessToken();
}
return accessToken;
Expand All @@ -86,7 +90,7 @@ public synchronized String getAccessToken() {
*/
public synchronized void setAccessToken(String accessToken, Integer expiresIn) {
this.accessToken = accessToken;
this.expirationTimeMillis = expiresIn == null ? null : System.currentTimeMillis() + expiresIn * MILLIS_PER_SECOND;
this.expirationTimeSeconds = expiresIn == null ? null : System.currentTimeMillis() / MILLIS_PER_SECOND + expiresIn - LEEWAY_SENCONDS;
}

}