mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-19 12:47:07 +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:
@@ -55,6 +55,7 @@ import java.util.List;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.TimeZone;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -305,6 +306,20 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
throw new RuntimeException("No Bearer authentication configured!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to set the supplier of access tokens for Bearer authentication.
|
||||
*
|
||||
* @param tokenSupplier the token supplier function
|
||||
*/
|
||||
public void setBearerToken(Supplier<String> tokenSupplier) {
|
||||
for (Authentication auth : authentications.values()) {
|
||||
if (auth instanceof HttpBearerAuth) {
|
||||
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("No Bearer authentication configured!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to set username for the first HTTP basic authentication.
|
||||
|
||||
@@ -15,13 +15,15 @@ package org.openapitools.client.auth;
|
||||
|
||||
import org.openapitools.client.Pair;
|
||||
|
||||
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;
|
||||
@@ -33,7 +35,7 @@ public class HttpBearerAuth implements Authentication {
|
||||
* @return The bearer token
|
||||
*/
|
||||
public String getBearerToken() {
|
||||
return bearerToken;
|
||||
return tokenSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,12 +44,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) {
|
||||
if(bearerToken == null) {
|
||||
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
|
||||
if (bearerToken == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.util.Map;
|
||||
/**
|
||||
* API tests for AuthApi
|
||||
*/
|
||||
@Ignore
|
||||
public class AuthApiTest {
|
||||
|
||||
private final AuthApi api = new AuthApi();
|
||||
@@ -47,4 +46,23 @@ public class AuthApiTest {
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
/**
|
||||
* To test HTTP bearer authentication
|
||||
*
|
||||
* To test HTTP bearer authentication
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
public void testAuthHttpBearerTest() throws ApiException {
|
||||
String response;
|
||||
api.getApiClient().setBearerToken("fixed token");
|
||||
response = api.testAuthHttpBearer();
|
||||
Assert.assertTrue(response.contains("Authorization: Bearer fixed token"));
|
||||
|
||||
api.getApiClient().setBearerToken(() -> "dynamic token");
|
||||
response = api.testAuthHttpBearer();
|
||||
Assert.assertTrue(response.contains("Authorization: Bearer dynamic token"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user