Fix duplicated Authorization headers when renewing a token on a retry (#11513)

Add a leeway time to avoid a skew in the local clock
This commit is contained in:
ruijlpires 2022-02-05 06:28:05 +00:00 committed by GitHub
parent 6cf4e79f14
commit 92ccb629e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View File

@ -10,6 +10,9 @@ import java.util.Collection;
{{>generatedAnnotation}} {{>generatedAnnotation}}
public abstract class OAuth implements RequestInterceptor { 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; static final int MILLIS_PER_SECOND = 1000;
public interface AccessTokenListener { public interface AccessTokenListener {
@ -17,7 +20,7 @@ public abstract class OAuth implements RequestInterceptor {
} }
private volatile String accessToken; private volatile String accessToken;
private Long expirationTimeMillis; private Long expirationTimeSeconds;
private AccessTokenListener accessTokenListener; private AccessTokenListener accessTokenListener;
protected OAuth20Service service; protected OAuth20Service service;
@ -39,6 +42,7 @@ public abstract class OAuth implements RequestInterceptor {
} }
String accessToken = getAccessToken(); String accessToken = getAccessToken();
if (accessToken != null) { if (accessToken != null) {
template.removeHeader("Authorization");
template.header("Authorization", "Bearer " + accessToken); template.header("Authorization", "Bearer " + accessToken);
} }
} }
@ -73,7 +77,7 @@ public abstract class OAuth implements RequestInterceptor {
public synchronized String getAccessToken() { public synchronized String getAccessToken() {
// If first time, get the token // If first time, get the token
if (expirationTimeMillis == null || System.currentTimeMillis() >= expirationTimeMillis) { if (expirationTimeSeconds == null || System.currentTimeMillis() >= expirationTimeSeconds) {
updateAccessToken(); updateAccessToken();
} }
return accessToken; return accessToken;
@ -86,7 +90,7 @@ public abstract class OAuth implements RequestInterceptor {
*/ */
public synchronized void setAccessToken(String accessToken, Integer expiresIn) { public synchronized void setAccessToken(String accessToken, Integer expiresIn) {
this.accessToken = accessToken; 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;
} }
} }

View File

@ -10,6 +10,9 @@ import java.util.Collection;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen") @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public abstract class OAuth implements RequestInterceptor { 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; static final int MILLIS_PER_SECOND = 1000;
public interface AccessTokenListener { public interface AccessTokenListener {
@ -17,7 +20,7 @@ public abstract class OAuth implements RequestInterceptor {
} }
private volatile String accessToken; private volatile String accessToken;
private Long expirationTimeMillis; private Long expirationTimeSeconds;
private AccessTokenListener accessTokenListener; private AccessTokenListener accessTokenListener;
protected OAuth20Service service; protected OAuth20Service service;
@ -39,6 +42,7 @@ public abstract class OAuth implements RequestInterceptor {
} }
String accessToken = getAccessToken(); String accessToken = getAccessToken();
if (accessToken != null) { if (accessToken != null) {
template.removeHeader("Authorization");
template.header("Authorization", "Bearer " + accessToken); template.header("Authorization", "Bearer " + accessToken);
} }
} }
@ -73,7 +77,7 @@ public abstract class OAuth implements RequestInterceptor {
public synchronized String getAccessToken() { public synchronized String getAccessToken() {
// If first time, get the token // If first time, get the token
if (expirationTimeMillis == null || System.currentTimeMillis() >= expirationTimeMillis) { if (expirationTimeSeconds == null || System.currentTimeMillis() >= expirationTimeSeconds) {
updateAccessToken(); updateAccessToken();
} }
return accessToken; return accessToken;
@ -86,7 +90,7 @@ public abstract class OAuth implements RequestInterceptor {
*/ */
public synchronized void setAccessToken(String accessToken, Integer expiresIn) { public synchronized void setAccessToken(String accessToken, Integer expiresIn) {
this.accessToken = accessToken; 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;
} }
} }

View File

@ -10,6 +10,9 @@ import java.util.Collection;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen") @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public abstract class OAuth implements RequestInterceptor { 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; static final int MILLIS_PER_SECOND = 1000;
public interface AccessTokenListener { public interface AccessTokenListener {
@ -17,7 +20,7 @@ public abstract class OAuth implements RequestInterceptor {
} }
private volatile String accessToken; private volatile String accessToken;
private Long expirationTimeMillis; private Long expirationTimeSeconds;
private AccessTokenListener accessTokenListener; private AccessTokenListener accessTokenListener;
protected OAuth20Service service; protected OAuth20Service service;
@ -39,6 +42,7 @@ public abstract class OAuth implements RequestInterceptor {
} }
String accessToken = getAccessToken(); String accessToken = getAccessToken();
if (accessToken != null) { if (accessToken != null) {
template.removeHeader("Authorization");
template.header("Authorization", "Bearer " + accessToken); template.header("Authorization", "Bearer " + accessToken);
} }
} }
@ -73,7 +77,7 @@ public abstract class OAuth implements RequestInterceptor {
public synchronized String getAccessToken() { public synchronized String getAccessToken() {
// If first time, get the token // If first time, get the token
if (expirationTimeMillis == null || System.currentTimeMillis() >= expirationTimeMillis) { if (expirationTimeSeconds == null || System.currentTimeMillis() >= expirationTimeSeconds) {
updateAccessToken(); updateAccessToken();
} }
return accessToken; return accessToken;
@ -86,7 +90,7 @@ public abstract class OAuth implements RequestInterceptor {
*/ */
public synchronized void setAccessToken(String accessToken, Integer expiresIn) { public synchronized void setAccessToken(String accessToken, Integer expiresIn) {
this.accessToken = accessToken; 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;
} }
} }