mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-05 13:46:10 +00:00
[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:
@@ -2,6 +2,7 @@ package org.openapitools.client;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -206,6 +207,15 @@ public class ApiClient {
|
||||
apiAuthorization.setBearerToken(bearerToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the supplier of bearer tokens.
|
||||
* @param tokenSupplier the supplier of bearer tokens.
|
||||
*/
|
||||
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||
HttpBearerAuth apiAuthorization = getAuthorization(HttpBearerAuth.class);
|
||||
apiAuthorization.setBearerToken(tokenSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to configure the first api key found
|
||||
* @param apiKey API key
|
||||
|
||||
@@ -2,13 +2,15 @@ package org.openapitools.client.auth;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* An interceptor that adds the request header needed to use HTTP bearer authentication.
|
||||
*/
|
||||
public class HttpBearerAuth implements RequestInterceptor {
|
||||
private final String scheme;
|
||||
private String bearerToken;
|
||||
private Supplier<String> tokenSupplier;
|
||||
|
||||
public HttpBearerAuth(String scheme) {
|
||||
this.scheme = scheme;
|
||||
@@ -16,21 +18,35 @@ public class HttpBearerAuth implements RequestInterceptor {
|
||||
|
||||
/**
|
||||
* Gets the token, which together with the scheme, will be sent as the value of the Authorization header.
|
||||
*
|
||||
* @return The bearer token
|
||||
*/
|
||||
public String getBearerToken() {
|
||||
return bearerToken;
|
||||
return tokenSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the token, which together with the scheme, will be sent as the value of the Authorization header.
|
||||
*
|
||||
* @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 apply(RequestTemplate template) {
|
||||
if(bearerToken == null) {
|
||||
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||
if (bearerToken == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user