forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix 457 Support for Bearer Auth (OpenAPITools#1930)
* fix OpenAPITools#457 by introducing an HttpBearerAuth object See OpenAPITools#457 Also OpenAPITools#1446 for typescript, OpenAPITools#1577 for python Specs defined as follows currently generate BasicAuth and send an "Authorization: Basic [base64Encode(username + ":" + password)]" header components: securitySchemes: bearer: type: http scheme: bearer This change will generate code which uses a new HttpBearerAuth class, which will send a "Authorization: [scheme] [accessToken]" header. This change is slightly larger and more impactful than simply using OAuth for bearerBearer, but it allows for scheme values other than bearer. This fix was enabled by the recent commit of OpenAPITools@80ca67c This PR is an alternative to OpenAPITools#1972 * update petstore samples * Update HttpBearerAuth mustache templates and samples * correct the expected number of generated java client files * update the retrofit2 HttpBearerAuth template and samples * Add resttemplate-specific HttpBearerAuth mustache and samples * add vertx-specific HttpBearerAuth template and samples * add java webclient-specific HttpBearerAuth template and samples
- Loading branch information
1 parent
9113b0f
commit 4fd02b9
Showing
32 changed files
with
1,198 additions
and
5 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
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
45 changes: 45 additions & 0 deletions
45
modules/openapi-generator/src/main/resources/Java/auth/HttpBearerAuth.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{{>licenseInfo}} | ||
|
||
package {{invokerPackage}}.auth; | ||
|
||
import {{invokerPackage}}.Pair; | ||
|
||
import java.util.Map; | ||
import java.util.List; | ||
|
||
{{>generatedAnnotation}} | ||
public class HttpBearerAuth implements Authentication { | ||
private final String scheme; | ||
private String bearerToken; | ||
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. | ||
*/ | ||
public String getBearerToken() { | ||
return bearerToken; | ||
} | ||
|
||
/** | ||
* Sets the token, which together with the scheme, will be sent as the value of the Authorization header. | ||
*/ | ||
public void setBearerToken(String bearerToken) { | ||
this.bearerToken = bearerToken; | ||
} | ||
|
||
@Override | ||
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { | ||
if(bearerToken == null) { | ||
return; | ||
} | ||
|
||
headerParams.put("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); | ||
} | ||
|
||
private static String upperCaseBearer(String scheme) { | ||
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...es/openapi-generator/src/main/resources/Java/libraries/feign/auth/HttpBearerAuth.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package {{invokerPackage}}.auth; | ||
|
||
import feign.RequestInterceptor; | ||
import feign.RequestTemplate; | ||
|
||
/** | ||
* 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; | ||
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. | ||
*/ | ||
public String getBearerToken() { | ||
return bearerToken; | ||
} | ||
|
||
/** | ||
* Sets the token, which together with the scheme, will be sent as the value of the Authorization header. | ||
*/ | ||
public void setBearerToken(String bearerToken) { | ||
this.bearerToken = bearerToken; | ||
} | ||
|
||
@Override | ||
public void apply(RequestTemplate template) { | ||
if(bearerToken == null) { | ||
return; | ||
} | ||
|
||
template.header("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); | ||
} | ||
|
||
private static String upperCaseBearer(String scheme) { | ||
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...api-generator/src/main/resources/Java/libraries/resttemplate/auth/HttpBearerAuth.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package {{invokerPackage}}.auth; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.util.Base64Utils; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
{{>generatedAnnotation}} | ||
public class HttpBearerAuth implements Authentication { | ||
private final String scheme; | ||
private String bearerToken; | ||
public HttpBearerAuth(String scheme) { | ||
this.scheme = scheme; | ||
} | ||
|
||
public String getBearerToken() { | ||
return bearerToken; | ||
} | ||
|
||
public void setBearerToken(String bearerToken) { | ||
this.bearerToken = bearerToken; | ||
} | ||
|
||
@Override | ||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams) { | ||
if (bearerToken == null) { | ||
return; | ||
} | ||
headerParams.add(HttpHeaders.AUTHORIZATION, (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); | ||
} | ||
|
||
private static String upperCaseBearer(String scheme) { | ||
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...openapi-generator/src/main/resources/Java/libraries/retrofit/auth/HttpBearerAuth.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package {{invokerPackage}}.auth; | ||
|
||
import java.io.IOException; | ||
|
||
import com.squareup.okhttp.Interceptor; | ||
import com.squareup.okhttp.Request; | ||
import com.squareup.okhttp.Response; | ||
|
||
public class HttpBearerAuth implements Interceptor { | ||
private final String scheme; | ||
private String bearerToken; | ||
public HttpBearerAuth(String scheme) { | ||
this.scheme = scheme; | ||
} | ||
|
||
public String getBearerToken() { | ||
return bearerToken; | ||
} | ||
|
||
public void setBearerToken(String bearerToken) { | ||
this.bearerToken = bearerToken; | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
// If the request already have an authorization (eg. Basic auth), do nothing | ||
if (request.header("Authorization") == null && bearerToken != null) { | ||
request = request.newBuilder() | ||
.addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) | ||
.build(); | ||
} | ||
return chain.proceed(request); | ||
} | ||
|
||
private static String upperCaseBearer(String scheme) { | ||
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...penapi-generator/src/main/resources/Java/libraries/retrofit2/auth/HttpBearerAuth.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package {{invokerPackage}}.auth; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class HttpBearerAuth implements Interceptor { | ||
private final String scheme; | ||
private String bearerToken; | ||
public HttpBearerAuth(String scheme) { | ||
this.scheme = scheme; | ||
} | ||
|
||
public String getBearerToken() { | ||
return bearerToken; | ||
} | ||
|
||
public void setBearerToken(String bearerToken) { | ||
this.bearerToken = bearerToken; | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
// If the request already have an authorization (eg. Basic auth), do nothing | ||
if (request.header("Authorization") == null && bearerToken != null) { | ||
request = request.newBuilder() | ||
.addHeader("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken) | ||
.build(); | ||
} | ||
return chain.proceed(request); | ||
} | ||
|
||
private static String upperCaseBearer(String scheme) { | ||
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...es/openapi-generator/src/main/resources/Java/libraries/vertx/auth/HttpBearerAuth.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{{>licenseInfo}} | ||
|
||
package {{invokerPackage}}.auth; | ||
|
||
import {{invokerPackage}}.Pair; | ||
import io.vertx.core.MultiMap; | ||
import java.util.Base64; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
{{>generatedAnnotation}} | ||
public class HttpBearerAuth implements Authentication { | ||
private final String scheme; | ||
private String bearerToken; | ||
public HttpBearerAuth(String scheme) { | ||
this.scheme = scheme; | ||
} | ||
|
||
public String getBearerToken() { | ||
return bearerToken; | ||
} | ||
|
||
public void setBearerToken(String bearerToken) { | ||
this.bearerToken = bearerToken; | ||
} | ||
|
||
@Override | ||
public void applyToParams(List<Pair> queryParams, MultiMap headerParams) { | ||
if (bearerToken == null) { | ||
return; | ||
} | ||
headerParams.add("Authorization", (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); | ||
} | ||
|
||
private static String upperCaseBearer(String scheme) { | ||
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...penapi-generator/src/main/resources/Java/libraries/webclient/auth/HttpBearerAuth.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package {{invokerPackage}}.auth; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.util.Base64Utils; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
{{>generatedAnnotation}} | ||
public class HttpBearerAuth implements Authentication { | ||
private final String scheme; | ||
private String bearerToken; | ||
public HttpBearerAuth(String scheme) { | ||
this.scheme = scheme; | ||
} | ||
|
||
public String getBearerToken() { | ||
return bearerToken; | ||
} | ||
|
||
public void setBearerToken(String bearerToken) { | ||
this.bearerToken = bearerToken; | ||
} | ||
|
||
@Override | ||
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams) { | ||
if (bearerToken == null) { | ||
return; | ||
} | ||
headerParams.add(HttpHeaders.AUTHORIZATION, (scheme != null ? upperCaseBearer(scheme) + " " : "") + bearerToken); | ||
} | ||
|
||
private static String upperCaseBearer(String scheme) { | ||
return ("bearer".equalsIgnoreCase(scheme)) ? "Bearer" : scheme; | ||
} | ||
|
||
} |
Oops, something went wrong.