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][apache-httpclient][feign][okhttp-gson] Enable access token refresh #17086

Merged
merged 3 commits into from
Nov 18, 2023
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 @@ -4,13 +4,15 @@ package {{invokerPackage}}.auth;

import {{invokerPackage}}.Pair;

import java.util.Map;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

{{>generatedAnnotation}}
public class HttpBearerAuth implements Authentication {
private final String scheme;
private String bearerToken;
private Supplier<String> tokenSupplier;

public HttpBearerAuth(String scheme) {
this.scheme = scheme;
Expand All @@ -22,7 +24,7 @@ public class HttpBearerAuth implements Authentication {
* @return The bearer token
*/
public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

/**
Expand All @@ -31,12 +33,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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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;
Expand Down Expand Up @@ -335,6 +336,20 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
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!");
}
{{/hasHttpBearerMethods}}

{{#hasHttpBasicMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package {{invokerPackage}};

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -265,6 +266,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,51 @@ package {{invokerPackage}}.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;
}

/**
* 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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;

Expand Down Expand Up @@ -528,14 +529,23 @@ public class ApiClient {
}

{{#hasHttpBearerMethods}}
/**
* 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {{invokerPackage}}.ApiException;
import {{invokerPackage}}.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;

{{>generatedAnnotation}}
public class HttpBearerAuth implements Authentication {
private final String scheme;
private String bearerToken;
private Supplier<String> tokenSupplier;

public HttpBearerAuth(String scheme) {
this.scheme = scheme;
Expand All @@ -24,7 +26,7 @@ public class HttpBearerAuth implements Authentication {
* @return The bearer token
*/
public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

/**
Expand All @@ -33,12 +35,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

{{#hasHttpBearerMethods}}
/**
* Helper method to set token for HTTP bearer authentication.
* Helper method to set access token for the first Bearer authentication.
*
* @param bearerToken the token
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
setBearerToken(() -> bearerToken);
}

/**
* Helper method to set the token supplier for HTTP bearer authentication.
* Helper method to set the supplier of access tokens for Bearer authentication.
*
* @param tokenSupplier the token supplier function
* @param tokenSupplier The supplier of bearer tokens
*/
public void setBearerToken(Supplier<String> tokenSupplier) {
for (Authentication auth : authentications.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ public class HttpBearerAuth implements Authentication {
this.scheme = scheme;
}

/**
* 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 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.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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
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;
Expand Down Expand Up @@ -305,6 +306,20 @@ public ApiClient setBearerToken(String bearerToken) {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

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;
Expand All @@ -33,7 +35,7 @@ public HttpBearerAuth(String scheme) {
* @return The bearer token
*/
public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

/**
Expand All @@ -42,12 +44,22 @@ public String getBearerToken() {
* @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;
}

Expand Down
Loading
Loading