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][resttemplate] Enable access token refresh #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -62,6 +62,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
{{#jsr310}}
import java.time.OffsetDateTime;
{{/jsr310}}
Expand Down Expand Up @@ -192,9 +193,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
setBearerToken(() -> bearerToken);
}

/**
* Helper method to set the token supplier for HTTP 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(bearerToken);
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package {{invokerPackage}}.auth;

import java.util.Optional;
import java.util.function.Supplier;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

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

public HttpBearerAuth(String scheme) {
this.scheme = scheme;
}

public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
this.tokenSupplier = () -> bearerToken;
}

public void setBearerToken(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;
}

@Override
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
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 @@ -51,6 +51,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.openapitools.client.auth;

import java.util.Optional;
import java.util.function.Supplier;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

@jakarta.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;
}

public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
this.tokenSupplier = () -> bearerToken;
}

public void setBearerToken(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;
}

@Override
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
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 @@ -51,6 +51,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.openapitools.client.auth;

import java.util.Optional;
import java.util.function.Supplier;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

@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;
}

public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
this.tokenSupplier = () -> bearerToken;
}

public void setBearerToken(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;
}

@Override
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
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 @@ -51,6 +51,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.openapitools.client.auth;

import java.util.Optional;
import java.util.function.Supplier;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

@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;
}

public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
this.tokenSupplier = () -> bearerToken;
}

public void setBearerToken(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;
}

@Override
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
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 @@ -56,6 +56,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down Expand Up @@ -170,9 +171,18 @@ public Authentication getAuthentication(String authName) {
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
setBearerToken(() -> bearerToken);
}

/**
* Helper method to set the token supplier for HTTP 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(bearerToken);
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.openapitools.client.auth;

import java.util.Optional;
import java.util.function.Supplier;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

@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;
}

public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
this.tokenSupplier = () -> bearerToken;
}

public void setBearerToken(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;
}

@Override
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
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 @@ -51,6 +51,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.function.Supplier;
import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down Expand Up @@ -165,9 +166,18 @@ public Authentication getAuthentication(String authName) {
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
setBearerToken(() -> bearerToken);
}

/**
* Helper method to set the token supplier for HTTP 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(bearerToken);
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.openapitools.client.auth;

import java.util.Optional;
import java.util.function.Supplier;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

@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;
}

public String getBearerToken() {
return bearerToken;
return tokenSupplier.get();
}

public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
this.tokenSupplier = () -> bearerToken;
}

public void setBearerToken(Supplier<String> tokenSupplier) {
this.tokenSupplier = tokenSupplier;
}

@Override
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
String bearerToken = Optional.ofNullable(tokenSupplier).map(Supplier::get).orElse(null);
if (bearerToken == null) {
return;
}
Expand Down