mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-11 01:22:44 +00:00
Add support for a token supplier to OAuth based RestClient clients. (#19944)
Fixes OpenAPITools#19943
This commit is contained in:
@@ -13,25 +13,49 @@
|
||||
|
||||
package org.openapitools.client.auth;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization.
|
||||
*/
|
||||
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT")
|
||||
public class OAuth implements Authentication {
|
||||
private String accessToken;
|
||||
private Supplier<String> tokenSupplier;
|
||||
|
||||
/**
|
||||
* Returns the bearer token used for Authorization.
|
||||
*
|
||||
* @return The bearer token
|
||||
*/
|
||||
public String getAccessToken() {
|
||||
return accessToken;
|
||||
return tokenSupplier.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bearer access token used for Authorization.
|
||||
*
|
||||
* @param accessToken The bearer token to send in the Authorization header
|
||||
*/
|
||||
public void setAccessToken(String accessToken) {
|
||||
this.accessToken = accessToken;
|
||||
setAccessToken(() -> accessToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the supplier of bearer tokens used for Authorization.
|
||||
*
|
||||
* @param tokenSupplier The supplier of bearer tokens to send in the Authorization header
|
||||
*/
|
||||
public void setAccessToken(Supplier<String> tokenSupplier) {
|
||||
this.tokenSupplier = tokenSupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
|
||||
if (accessToken != null) {
|
||||
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
|
||||
}
|
||||
Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken ->
|
||||
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user