diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index 0968056492a..b7d84d43466 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -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())) { diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 66fe2735584..362aee07d75 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -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; diff --git a/samples/client/petstore/java/jersey2-java7/.openapi-generator/FILES b/samples/client/petstore/java/jersey2-java7/.openapi-generator/FILES index 3cd9918bb90..7c98cddde48 100644 --- a/samples/client/petstore/java/jersey2-java7/.openapi-generator/FILES +++ b/samples/client/petstore/java/jersey2-java7/.openapi-generator/FILES @@ -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 diff --git a/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java index d2137b8d088..a6405acc784 100644 --- a/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/ApiClient.java @@ -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; diff --git a/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java b/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java deleted file mode 100644 index 628ba5c18df..00000000000 --- a/samples/client/petstore/java/jersey2-java7/src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java +++ /dev/null @@ -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 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 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 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 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 queryParams, Map headerParams, Map 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 urlQueries = new ArrayList(); - 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()); - } - } -} diff --git a/samples/client/petstore/java/jersey2-java8/.openapi-generator/FILES b/samples/client/petstore/java/jersey2-java8/.openapi-generator/FILES index 5f13bc879b4..ec3057e7863 100644 --- a/samples/client/petstore/java/jersey2-java8/.openapi-generator/FILES +++ b/samples/client/petstore/java/jersey2-java8/.openapi-generator/FILES @@ -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 diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index d2137b8d088..a6405acc784 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -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; diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java deleted file mode 100644 index 628ba5c18df..00000000000 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/auth/HttpSignatureAuth.java +++ /dev/null @@ -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 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 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 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 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 queryParams, Map headerParams, Map 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 urlQueries = new ArrayList(); - 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()); - } - } -}