[java-jersey2] Conditionally include http signature mustache template (#6413)

This commit is contained in:
Sebastien Rosset 2020-05-23 14:32:06 -07:00 committed by GitHub
parent 284a90f7b1
commit e38168c2b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 5 additions and 529 deletions

View File

@ -380,7 +380,9 @@ public class JavaClientCodegen extends AbstractJavaCodegen
} else if (JERSEY2.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
supportingFiles.add(new SupportingFile("auth/HttpSignatureAuth.mustache", authFolder, "HttpSignatureAuth.java"));
if (ProcessUtils.hasHttpSignatureMethods(openAPI)) {
supportingFiles.add(new SupportingFile("auth/HttpSignatureAuth.mustache", authFolder, "HttpSignatureAuth.java"));
}
supportingFiles.add(new SupportingFile("AbstractOpenApiSchema.mustache", (sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar), "AbstractOpenApiSchema.java"));
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
} else if (NATIVE.equals(getLibrary())) {

View File

@ -59,7 +59,9 @@ import java.util.regex.Pattern;
import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.HttpBearerAuth;
{{#hasHttpSignatureMethods}}
import {{invokerPackage}}.auth.HttpSignatureAuth;
{{/hasHttpSignatureMethods}}
import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;

View File

@ -86,7 +86,6 @@ src/main/java/org/openapitools/client/auth/ApiKeyAuth.java
src/main/java/org/openapitools/client/auth/Authentication.java
src/main/java/org/openapitools/client/auth/HttpBasicAuth.java
src/main/java/org/openapitools/client/auth/HttpBearerAuth.java
src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java
src/main/java/org/openapitools/client/auth/OAuth.java
src/main/java/org/openapitools/client/auth/OAuthFlow.java
src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java

View File

@ -51,7 +51,6 @@ import java.util.regex.Pattern;
import org.openapitools.client.auth.Authentication;
import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.HttpSignatureAuth;
import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth;
import org.openapitools.client.model.AbstractOpenApiSchema;

View File

@ -1,262 +0,0 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.auth;
import org.openapitools.client.Pair;
import org.openapitools.client.ApiException;
import java.net.URI;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.Key;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.List;
import java.security.spec.AlgorithmParameterSpec;
import org.tomitribe.auth.signatures.Algorithm;
import org.tomitribe.auth.signatures.Signer;
import org.tomitribe.auth.signatures.Signature;
import org.tomitribe.auth.signatures.SigningAlgorithm;
/**
* A Configuration object for the HTTP message signature security scheme.
*/
public class HttpSignatureAuth implements Authentication {
private Signer signer;
// An opaque string that the server can use to look up the component they need to validate the signature.
private String keyId;
// The HTTP signature algorithm.
private SigningAlgorithm signingAlgorithm;
// The HTTP cryptographic algorithm.
private Algorithm algorithm;
// The cryptographic parameters.
private AlgorithmParameterSpec parameterSpec;
// The list of HTTP headers that should be included in the HTTP signature.
private List<String> headers;
// The digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
private String digestAlgorithm;
/**
* Construct a new HTTP signature auth configuration object.
*
* @param keyId An opaque string that the server can use to look up the component they need to validate the signature.
* @param signingAlgorithm The signature algorithm.
* @param algorithm The cryptographic algorithm.
* @param digestAlgorithm The digest algorithm.
* @param headers The list of HTTP headers that should be included in the HTTP signature.
*/
public HttpSignatureAuth(String keyId,
SigningAlgorithm signingAlgorithm,
Algorithm algorithm,
String digestAlgorithm,
AlgorithmParameterSpec parameterSpec,
List<String> headers) {
this.keyId = keyId;
this.signingAlgorithm = signingAlgorithm;
this.algorithm = algorithm;
this.parameterSpec = parameterSpec;
this.digestAlgorithm = digestAlgorithm;
this.headers = headers;
}
/**
* Returns the opaque string that the server can use to look up the component they need to validate the signature.
*
* @return The keyId.
*/
public String getKeyId() {
return keyId;
}
/**
* Set the HTTP signature key id.
*
* @param keyId An opaque string that the server can use to look up the component they need to validate the signature.
*/
public void setKeyId(String keyId) {
this.keyId = keyId;
}
/**
* Returns the HTTP signature algorithm which is used to sign HTTP requests.
*/
public SigningAlgorithm getSigningAlgorithm() {
return signingAlgorithm;
}
/**
* Sets the HTTP signature algorithm which is used to sign HTTP requests.
*
* @param signingAlgorithm The HTTP signature algorithm.
*/
public void setSigningAlgorithm(SigningAlgorithm signingAlgorithm) {
this.signingAlgorithm = signingAlgorithm;
}
/**
* Returns the HTTP cryptographic algorithm which is used to sign HTTP requests.
*/
public Algorithm getAlgorithm() {
return algorithm;
}
/**
* Sets the HTTP cryptographic algorithm which is used to sign HTTP requests.
*
* @param algorithm The HTTP signature algorithm.
*/
public void setAlgorithm(Algorithm algorithm) {
this.algorithm = algorithm;
}
/**
* Returns the cryptographic parameters which are used to sign HTTP requests.
*/
public AlgorithmParameterSpec getAlgorithmParameterSpec() {
return parameterSpec;
}
/**
* Sets the cryptographic parameters which are used to sign HTTP requests.
*
* @param parameterSpec The cryptographic parameters.
*/
public void setAlgorithmParameterSpec(AlgorithmParameterSpec parameterSpec) {
this.parameterSpec = parameterSpec;
}
/**
* Returns the digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
*
* @see java.security.MessageDigest
*/
public String getDigestAlgorithm() {
return digestAlgorithm;
}
/**
* Sets the digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
*
* The exact list of supported digest algorithms depends on the installed security providers.
* Every implementation of the Java platform is required to support "MD5", "SHA-1" and "SHA-256".
* Do not use "MD5" and "SHA-1", they are vulnerable to multiple known attacks.
* By default, "SHA-256" is used.
*
* @param digestAlgorithm The digest algorithm.
*
* @see java.security.MessageDigest
*/
public void setDigestAlgorithm(String digestAlgorithm) {
this.digestAlgorithm = digestAlgorithm;
}
/**
* Returns the list of HTTP headers that should be included in the HTTP signature.
*/
public List<String> getHeaders() {
return headers;
}
/**
* Sets the list of HTTP headers that should be included in the HTTP signature.
*
* @param headers The HTTP headers.
*/
public void setHeaders(List<String> headers) {
this.headers = headers;
}
/**
* Returns the signer instance used to sign HTTP messages.
*
* @returrn the signer instance.
*/
public Signer getSigner() {
return signer;
}
/**
* Sets the signer instance used to sign HTTP messages.
*
* @param signer The signer instance to set.
*/
public void setSigner(Signer signer) {
this.signer = signer;
}
/**
* Set the private key used to sign HTTP requests using the HTTP signature scheme.
*
* @param key The private key.
*/
public void setPrivateKey(Key key) throws ApiException {
if (key == null) {
throw new ApiException("Private key (java.security.Key) cannot be null");
}
signer = new Signer(key, new Signature(keyId, signingAlgorithm, algorithm, parameterSpec, null, headers));
}
@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
try {
if (headers.contains("host")) {
headerParams.put("host", uri.getHost());
}
if (headers.contains("date")) {
headerParams.put("date", new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).format(new Date()));
}
if (headers.contains("digest")) {
headerParams.put("digest",
this.digestAlgorithm + "=" +
new String(Base64.getEncoder().encode(MessageDigest.getInstance(this.digestAlgorithm).digest(payload.getBytes()))));
}
if (signer == null) {
throw new ApiException("Signer cannot be null. Please call the method `setPrivateKey` to set it up correctly");
}
// construct the path with the URL query string
String path = uri.getPath();
List<String> urlQueries = new ArrayList<String>();
for (Pair queryParam : queryParams) {
urlQueries.add(queryParam.getName() + "=" + URLEncoder.encode(queryParam.getValue(), "utf8").replaceAll("\\+", "%20"));
}
if (!urlQueries.isEmpty()) {
path = path + "?" + String.join("&", urlQueries);
}
headerParams.put("Authorization", signer.sign(method, path, headerParams).toString());
} catch (Exception ex) {
throw new ApiException("Failed to create signature in the HTTP request header: " + ex.toString());
}
}
}

View File

@ -86,7 +86,6 @@ src/main/java/org/openapitools/client/auth/ApiKeyAuth.java
src/main/java/org/openapitools/client/auth/Authentication.java
src/main/java/org/openapitools/client/auth/HttpBasicAuth.java
src/main/java/org/openapitools/client/auth/HttpBearerAuth.java
src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java
src/main/java/org/openapitools/client/auth/OAuth.java
src/main/java/org/openapitools/client/auth/OAuthFlow.java
src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java

View File

@ -51,7 +51,6 @@ import java.util.regex.Pattern;
import org.openapitools.client.auth.Authentication;
import org.openapitools.client.auth.HttpBasicAuth;
import org.openapitools.client.auth.HttpBearerAuth;
import org.openapitools.client.auth.HttpSignatureAuth;
import org.openapitools.client.auth.ApiKeyAuth;
import org.openapitools.client.auth.OAuth;
import org.openapitools.client.model.AbstractOpenApiSchema;

View File

@ -1,262 +0,0 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.auth;
import org.openapitools.client.Pair;
import org.openapitools.client.ApiException;
import java.net.URI;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.Key;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.List;
import java.security.spec.AlgorithmParameterSpec;
import org.tomitribe.auth.signatures.Algorithm;
import org.tomitribe.auth.signatures.Signer;
import org.tomitribe.auth.signatures.Signature;
import org.tomitribe.auth.signatures.SigningAlgorithm;
/**
* A Configuration object for the HTTP message signature security scheme.
*/
public class HttpSignatureAuth implements Authentication {
private Signer signer;
// An opaque string that the server can use to look up the component they need to validate the signature.
private String keyId;
// The HTTP signature algorithm.
private SigningAlgorithm signingAlgorithm;
// The HTTP cryptographic algorithm.
private Algorithm algorithm;
// The cryptographic parameters.
private AlgorithmParameterSpec parameterSpec;
// The list of HTTP headers that should be included in the HTTP signature.
private List<String> headers;
// The digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
private String digestAlgorithm;
/**
* Construct a new HTTP signature auth configuration object.
*
* @param keyId An opaque string that the server can use to look up the component they need to validate the signature.
* @param signingAlgorithm The signature algorithm.
* @param algorithm The cryptographic algorithm.
* @param digestAlgorithm The digest algorithm.
* @param headers The list of HTTP headers that should be included in the HTTP signature.
*/
public HttpSignatureAuth(String keyId,
SigningAlgorithm signingAlgorithm,
Algorithm algorithm,
String digestAlgorithm,
AlgorithmParameterSpec parameterSpec,
List<String> headers) {
this.keyId = keyId;
this.signingAlgorithm = signingAlgorithm;
this.algorithm = algorithm;
this.parameterSpec = parameterSpec;
this.digestAlgorithm = digestAlgorithm;
this.headers = headers;
}
/**
* Returns the opaque string that the server can use to look up the component they need to validate the signature.
*
* @return The keyId.
*/
public String getKeyId() {
return keyId;
}
/**
* Set the HTTP signature key id.
*
* @param keyId An opaque string that the server can use to look up the component they need to validate the signature.
*/
public void setKeyId(String keyId) {
this.keyId = keyId;
}
/**
* Returns the HTTP signature algorithm which is used to sign HTTP requests.
*/
public SigningAlgorithm getSigningAlgorithm() {
return signingAlgorithm;
}
/**
* Sets the HTTP signature algorithm which is used to sign HTTP requests.
*
* @param signingAlgorithm The HTTP signature algorithm.
*/
public void setSigningAlgorithm(SigningAlgorithm signingAlgorithm) {
this.signingAlgorithm = signingAlgorithm;
}
/**
* Returns the HTTP cryptographic algorithm which is used to sign HTTP requests.
*/
public Algorithm getAlgorithm() {
return algorithm;
}
/**
* Sets the HTTP cryptographic algorithm which is used to sign HTTP requests.
*
* @param algorithm The HTTP signature algorithm.
*/
public void setAlgorithm(Algorithm algorithm) {
this.algorithm = algorithm;
}
/**
* Returns the cryptographic parameters which are used to sign HTTP requests.
*/
public AlgorithmParameterSpec getAlgorithmParameterSpec() {
return parameterSpec;
}
/**
* Sets the cryptographic parameters which are used to sign HTTP requests.
*
* @param parameterSpec The cryptographic parameters.
*/
public void setAlgorithmParameterSpec(AlgorithmParameterSpec parameterSpec) {
this.parameterSpec = parameterSpec;
}
/**
* Returns the digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
*
* @see java.security.MessageDigest
*/
public String getDigestAlgorithm() {
return digestAlgorithm;
}
/**
* Sets the digest algorithm which is used to calculate a cryptographic digest of the HTTP request body.
*
* The exact list of supported digest algorithms depends on the installed security providers.
* Every implementation of the Java platform is required to support "MD5", "SHA-1" and "SHA-256".
* Do not use "MD5" and "SHA-1", they are vulnerable to multiple known attacks.
* By default, "SHA-256" is used.
*
* @param digestAlgorithm The digest algorithm.
*
* @see java.security.MessageDigest
*/
public void setDigestAlgorithm(String digestAlgorithm) {
this.digestAlgorithm = digestAlgorithm;
}
/**
* Returns the list of HTTP headers that should be included in the HTTP signature.
*/
public List<String> getHeaders() {
return headers;
}
/**
* Sets the list of HTTP headers that should be included in the HTTP signature.
*
* @param headers The HTTP headers.
*/
public void setHeaders(List<String> headers) {
this.headers = headers;
}
/**
* Returns the signer instance used to sign HTTP messages.
*
* @returrn the signer instance.
*/
public Signer getSigner() {
return signer;
}
/**
* Sets the signer instance used to sign HTTP messages.
*
* @param signer The signer instance to set.
*/
public void setSigner(Signer signer) {
this.signer = signer;
}
/**
* Set the private key used to sign HTTP requests using the HTTP signature scheme.
*
* @param key The private key.
*/
public void setPrivateKey(Key key) throws ApiException {
if (key == null) {
throw new ApiException("Private key (java.security.Key) cannot be null");
}
signer = new Signer(key, new Signature(keyId, signingAlgorithm, algorithm, parameterSpec, null, headers));
}
@Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams,
String payload, String method, URI uri) throws ApiException {
try {
if (headers.contains("host")) {
headerParams.put("host", uri.getHost());
}
if (headers.contains("date")) {
headerParams.put("date", new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).format(new Date()));
}
if (headers.contains("digest")) {
headerParams.put("digest",
this.digestAlgorithm + "=" +
new String(Base64.getEncoder().encode(MessageDigest.getInstance(this.digestAlgorithm).digest(payload.getBytes()))));
}
if (signer == null) {
throw new ApiException("Signer cannot be null. Please call the method `setPrivateKey` to set it up correctly");
}
// construct the path with the URL query string
String path = uri.getPath();
List<String> urlQueries = new ArrayList<String>();
for (Pair queryParam : queryParams) {
urlQueries.add(queryParam.getName() + "=" + URLEncoder.encode(queryParam.getValue(), "utf8").replaceAll("\\+", "%20"));
}
if (!urlQueries.isEmpty()) {
path = path + "?" + String.join("&", urlQueries);
}
headerParams.put("Authorization", signer.sign(method, path, headerParams).toString());
} catch (Exception ex) {
throw new ApiException("Failed to create signature in the HTTP request header: " + ex.toString());
}
}
}