[Java][apache-httpclient][feign][okhttp-gson] Enable access token refresh (#17086)

* add setter of bearer token supplier

* run generate-samples.sh

* add test of bearer auth
This commit is contained in:
Tomohiko Ozawa
2023-11-18 13:46:18 +09:00
committed by GitHub
parent dc4c72c85c
commit 4bedeef643
57 changed files with 723 additions and 129 deletions

View File

@@ -47,6 +47,7 @@ import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -386,14 +387,23 @@ public class ApiClient {
return authentications.get(authName);
}
/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
setBearerToken(() -> bearerToken);
}
/**
* Helper method to set the supplier of access tokens for Bearer authentication.
*
* @param tokenSupplier The supplier of bearer tokens
*/
public void setBearerToken(Supplier<String> tokenSupplier) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
return;
}
}

View File

@@ -17,13 +17,15 @@ import org.openapitools.client.ApiException;
import org.openapitools.client.Pair;
import java.net.URI;
import java.util.Map;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class HttpBearerAuth implements Authentication {
private final String scheme;
private String bearerToken;
private Supplier<String> tokenSupplier;
public HttpBearerAuth(String scheme) {
this.scheme = scheme;
@@ -35,7 +37,7 @@ public class HttpBearerAuth implements Authentication {
* @return The bearer token
*/
public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}
/**
@@ -44,12 +46,22 @@ public class HttpBearerAuth implements Authentication {
* @param bearerToken The bearer token to send in the Authorization header
*/
public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
this.tokenSupplier = () -> bearerToken;
}
/**
* Sets the supplier of tokens, which together with the scheme, will be sent as the value of the Authorization header.
*
* @param tokenSupplier The supplier of bearer tokens to send in the Authorization header
*/
public void setBearerToken(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;
}
@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
if (bearerToken == null) {
return;
}