forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for a bearer token supplier to OAuth based RestTemplate c…
…lients. Fixes OpenAPITools#19000
- Loading branch information
1 parent
1064cb6
commit f53e8c6
Showing
14 changed files
with
302 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 30 additions & 6 deletions
36
modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/auth/OAuth.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,48 @@ | ||
package {{invokerPackage}}.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. | ||
*/ | ||
{{>generatedAnnotation}} | ||
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 bearerToken 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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
modules/openapi-generator/src/test/resources/3_0/java/oauth.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
openapi: 3.0.3 | ||
|
||
info: | ||
title: Test OAuth code generation | ||
description: Tests OAuth code generation | ||
version: 1.0.0 | ||
|
||
servers: | ||
- url: http://localhost:8080 | ||
|
||
paths: | ||
/api/something: | ||
get: | ||
responses: | ||
204: | ||
description: Success! | ||
|
||
components: | ||
securitySchemes: | ||
oAuth: | ||
type: oauth2 | ||
flows: | ||
implicit: | ||
authorizationUrl: '' | ||
scopes: {} | ||
|
||
security: | ||
- oAuth: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 30 additions & 6 deletions
36
.../petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/auth/OAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,48 @@ | ||
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.7.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 bearerToken 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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 30 additions & 6 deletions
36
...petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/auth/OAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,48 @@ | ||
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. | ||
*/ | ||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.7.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 bearerToken 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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 30 additions & 6 deletions
36
...petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/auth/OAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,48 @@ | ||
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. | ||
*/ | ||
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.7.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 bearerToken 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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.