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

merge master #37

Merged
merged 7 commits into from
Feb 24, 2022
Merged
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 @@ -44,6 +44,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
protected String packageVersion = "1.0.0";
protected String declspec = "";
protected String defaultInclude = "";
protected String apiDirName = "api";
protected String modelDirName = "model";

private final Set<String> parentModels = new HashSet<>();
Expand Down Expand Up @@ -143,29 +144,6 @@ public CppRestSdkClientCodegen() {
VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_DESC,
Boolean.toString(this.variableNameFirstCharacterUppercase));

supportingFiles.add(new SupportingFile("modelbase-header.mustache", "", "ModelBase.h"));
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "", "ModelBase.cpp"));
supportingFiles.add(new SupportingFile("object-header.mustache", "", "Object.h"));
supportingFiles.add(new SupportingFile("object-source.mustache", "", "Object.cpp"));
supportingFiles.add(new SupportingFile("apiclient-header.mustache", "", "ApiClient.h"));
supportingFiles.add(new SupportingFile("apiclient-source.mustache", "", "ApiClient.cpp"));
supportingFiles.add(new SupportingFile("apiconfiguration-header.mustache", "", "ApiConfiguration.h"));
supportingFiles.add(new SupportingFile("apiconfiguration-source.mustache", "", "ApiConfiguration.cpp"));
supportingFiles.add(new SupportingFile("apiexception-header.mustache", "", "ApiException.h"));
supportingFiles.add(new SupportingFile("apiexception-source.mustache", "", "ApiException.cpp"));
supportingFiles.add(new SupportingFile("ihttpbody-header.mustache", "", "IHttpBody.h"));
supportingFiles.add(new SupportingFile("jsonbody-header.mustache", "", "JsonBody.h"));
supportingFiles.add(new SupportingFile("jsonbody-source.mustache", "", "JsonBody.cpp"));
supportingFiles.add(new SupportingFile("httpcontent-header.mustache", "", "HttpContent.h"));
supportingFiles.add(new SupportingFile("httpcontent-source.mustache", "", "HttpContent.cpp"));
supportingFiles.add(new SupportingFile("multipart-header.mustache", "", "MultipartFormData.h"));
supportingFiles.add(new SupportingFile("multipart-source.mustache", "", "MultipartFormData.cpp"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("cmake-lists.mustache", "", "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("cmake-config.mustache", "", "Config.cmake.in"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

languageSpecificPrimitives = new HashSet<>(
Arrays.asList("int", "char", "bool", "long", "float", "double", "int32_t", "int64_t"));

Expand Down Expand Up @@ -229,32 +207,62 @@ public void processOpts() {
additionalProperties.put("declspec", declspec);
additionalProperties.put("defaultInclude", defaultInclude);
additionalProperties.put(RESERVED_WORD_PREFIX_OPTION, reservedWordPrefix);

importMapping.put("HttpContent", "#include \"" + packageName + "/" + "HttpContent.h\"");
importMapping.put("Object", "#include \"" + packageName + "/" + "Object.h\"");

supportingFiles.add(new SupportingFile("modelbase-header.mustache", getHeaderFolder(), "ModelBase.h"));
supportingFiles.add(new SupportingFile("modelbase-source.mustache", getSourceFolder(), "ModelBase.cpp"));
supportingFiles.add(new SupportingFile("object-header.mustache", getHeaderFolder(), "Object.h"));
supportingFiles.add(new SupportingFile("object-source.mustache", getSourceFolder(), "Object.cpp"));
supportingFiles.add(new SupportingFile("apiclient-header.mustache", getHeaderFolder(), "ApiClient.h"));
supportingFiles.add(new SupportingFile("apiclient-source.mustache", getSourceFolder(), "ApiClient.cpp"));
supportingFiles.add(new SupportingFile("apiconfiguration-header.mustache", getHeaderFolder(), "ApiConfiguration.h"));
supportingFiles.add(new SupportingFile("apiconfiguration-source.mustache", getSourceFolder(), "ApiConfiguration.cpp"));
supportingFiles.add(new SupportingFile("apiexception-header.mustache", getHeaderFolder(), "ApiException.h"));
supportingFiles.add(new SupportingFile("apiexception-source.mustache", getSourceFolder(), "ApiException.cpp"));
supportingFiles.add(new SupportingFile("ihttpbody-header.mustache", getHeaderFolder(), "IHttpBody.h"));
supportingFiles.add(new SupportingFile("jsonbody-header.mustache", getHeaderFolder(), "JsonBody.h"));
supportingFiles.add(new SupportingFile("jsonbody-source.mustache", getSourceFolder(), "JsonBody.cpp"));
supportingFiles.add(new SupportingFile("httpcontent-header.mustache", getHeaderFolder(), "HttpContent.h"));
supportingFiles.add(new SupportingFile("httpcontent-source.mustache", getSourceFolder(), "HttpContent.cpp"));
supportingFiles.add(new SupportingFile("multipart-header.mustache", getHeaderFolder(), "MultipartFormData.h"));
supportingFiles.add(new SupportingFile("multipart-source.mustache", getSourceFolder(), "MultipartFormData.cpp"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("cmake-lists.mustache", "", "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("cmake-config.mustache", "", "Config.cmake.in"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
}

protected String getHeaderFolder() {
return "include/" + packageName;
}

protected String getSourceFolder() {
return "src";
}

/**
* Location to write model files. You can use the modelPackage() as defined
* when the class is instantiated
*/
@Override
public String modelFileFolder() {
return outputFolder + "/" + modelDirName;
public String apiFilename(String templateName, String tag) {
String suffix = apiTemplateFiles().get(templateName);
String targetOutDir = suffix.equals(".h") ? getHeaderFolder() : getSourceFolder();
return outputFolder + "/" + targetOutDir + "/" + apiDirName + "/" + toApiFilename(tag) + suffix;
}

/**
* Location to write api files. You can use the apiPackage() as defined when
* the class is instantiated
*/
@Override
public String apiFileFolder() {
return outputFolder + "/api";
public String modelFilename(String templateName, String modelName) {
String suffix = modelTemplateFiles().get(templateName);
String targetOutDir = suffix.equals(".h") ? getHeaderFolder() : getSourceFolder();
return outputFolder + "/" + targetOutDir + "/" + modelDirName + "/" + toModelFilename(modelName) + suffix;
}

@Override
public String toModelImport(String name) {
if (importMapping.containsKey(name)) {
return importMapping.get(name);
} else {
return "#include \"" + modelDirName + "/" + toModelFilename(name) + ".h\"";
return "#include \"" + packageName + "/" + modelDirName + "/" + toModelFilename(name) + ".h\"";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;
import java.text.DecimalFormat;
import java.util.GregorianCalendar;
import java.util.TimeZone;

Expand All @@ -20,6 +21,7 @@ public class RFC3339DateFormat extends DateFormat {

public RFC3339DateFormat() {
this.calendar = new GregorianCalendar();
this.numberFormat = new DecimalFormat();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,12 @@ public class {{classname}} {
{{#isDeepObject}}
if ({{paramName}} != null) {
{{#items.vars}}
{{#isArray}}
localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "{{baseName}}", {{paramName}}.{{getter}}()));
{{/isArray}}
{{^isArray}}
localVarQueryParams.addAll(ApiClient.parameterToPairs("{{baseName}}", {{paramName}}.{{getter}}()));
{{/isArray}}
{{/items.vars}}
}
{{/isDeepObject}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -32,18 +33,18 @@ import java.util.Optional;

@Singleton
public class SecurityAPIUtils {
private final String bearerPrefix = "Bearer ";
private static final String BEARER_PREFIX = "Bearer ";
private final ObjectMapper mapper;

private boolean useOnlineValidation = false;
private static final boolean USE_ONLINE_VALIDATION = false;

// Online validation
private HashMap<String, String> tokenIntrospectEndpoints = new HashMap<>();
private final HashMap<String, String> tokenIntrospectEndpoints = new HashMap<>();
private final String clientId;
private final String clientSecret;

// Offline validation
private HashMap<String, String> jwksEndpoints = new HashMap<>();
private final HashMap<String, String> jwksEndpoints = new HashMap<>();
private String tokenKeyId = "";
private JWTVerifier tokenVerifier; //Reusable verifier instance until tokenKeyId changes.

Expand All @@ -67,12 +68,13 @@ public class SecurityAPIUtils {
{{/hasOAuthMethods}}
}

//This function is not currently used because we hardcode USE_ONLINE_VALIDATION to false but might in the future versions
private boolean isRequestTokenValidByOnlineCheck(Http.Request request, String securityMethodName) {
try {
Optional<String> authToken = request.getHeaders().get(HttpHeaders.AUTHORIZATION);

if (authToken.isPresent()) {
String tokenWithoutBearerPrefix = authToken.get().substring(bearerPrefix.length());
String tokenWithoutBearerPrefix = authToken.get().substring(BEARER_PREFIX.length());

HttpClientBuilder builder = HttpClientBuilder.create();
HttpClient httpClient = builder.build();
Expand All @@ -86,9 +88,9 @@ public class SecurityAPIUtils {

HttpResponse response = httpClient.execute(httppost);
String responseJsonString = EntityUtils.toString(response.getEntity());
HashMap responseJsonObject = mapper.readValue(responseJsonString, HashMap.class);
JsonNode responseJsonObject = mapper.readTree(responseJsonString);

return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && (boolean) responseJsonObject.get("active");
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && responseJsonObject.get("active").asBoolean();
}
} catch (Exception exception) {
return false;
Expand All @@ -102,7 +104,7 @@ public class SecurityAPIUtils {
Optional<String> authHeader = request.getHeaders().get(HttpHeaders.AUTHORIZATION);

if (authHeader.isPresent()) {
String bearerToken = authHeader.get().substring(bearerPrefix.length());
String bearerToken = authHeader.get().substring(BEARER_PREFIX.length());
return isTokenValidByOfflineCheck(bearerToken, securityMethodName);
}
} catch (Exception exception) {
Expand All @@ -118,8 +120,9 @@ public class SecurityAPIUtils {
String issuer = jwt.getIssuer();
String keyId = jwt.getKeyId();
if (!tokenKeyId.equals(keyId)) {
if (securityMethodName == null) {
securityMethodName = jwksEndpoints.keySet().stream().findFirst().get();
Optional<String> optionalSecurityMethodName = jwksEndpoints.keySet().stream().findFirst();
if (securityMethodName == null && optionalSecurityMethodName.isPresent()) {
securityMethodName = optionalSecurityMethodName.get();
}

Jwk jwk = new UrlJwkProvider(new URL(this.jwksEndpoints.get(securityMethodName))).get(keyId);
Expand All @@ -136,7 +139,7 @@ public class SecurityAPIUtils {
tokenKeyId = keyId;
}

DecodedJWT verifiedJWT = tokenVerifier.verify(bearerToken);
tokenVerifier.verify(bearerToken);

return true;
} catch (Exception exception) {
Expand All @@ -148,7 +151,7 @@ public class SecurityAPIUtils {
try {
Optional<String> authHeader = requestWithPreviouslyVerifiedToken.getHeaders().get(HttpHeaders.AUTHORIZATION);
if (authHeader.isPresent()) {
String bearerToken = authHeader.get().substring(bearerPrefix.length());
String bearerToken = authHeader.get().substring(BEARER_PREFIX.length());
return getOAuthUserIdFromToken(bearerToken);
}
} catch (Exception exception) {
Expand All @@ -168,6 +171,6 @@ public class SecurityAPIUtils {
}

public boolean isRequestTokenValid(Http.Request request, String securityMethodName) {
return useOnlineValidation ? isRequestTokenValidByOnlineCheck(request, securityMethodName) : isRequestTokenValidByOfflineCheck(request, securityMethodName);
return USE_ONLINE_VALIDATION ? isRequestTokenValidByOnlineCheck(request, securityMethodName) : isRequestTokenValidByOfflineCheck(request, securityMethodName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

{{{defaultInclude}}}

#include "ApiClient.h"
{{^hasModelImport}}#include "ModelBase.h"{{/hasModelImport}}
#include "{{packageName}}/ApiClient.h"
{{^hasModelImport}}#include "{{packageName}}/ModelBase.h"{{/hasModelImport}}
{{#imports}}{{{import}}}
{{/imports}}
#include <boost/optional.hpp>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{{>licenseInfo}}
{{#operations}}

#include "{{classname}}.h"
#include "IHttpBody.h"
#include "JsonBody.h"
#include "MultipartFormData.h"

#include <unordered_set>
#include "{{packageName}}/api/{{classname}}.h"
#include "{{packageName}}/IHttpBody.h"
#include "{{packageName}}/JsonBody.h"
#include "{{packageName}}/MultipartFormData.h"

#include <boost/algorithm/string/replace.hpp>

#include <unordered_set>

{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
#define {{apiHeaderGuardPrefix}}_ApiClient_H_

{{{defaultInclude}}}
#include "ApiConfiguration.h"
#include "ApiException.h"
#include "IHttpBody.h"
#include "HttpContent.h"

#include <memory>
#include <vector>
#include <functional>
#include "{{packageName}}/ApiConfiguration.h"
#include "{{packageName}}/ApiException.h"
#include "{{packageName}}/IHttpBody.h"
#include "{{packageName}}/HttpContent.h"

#if defined (_WIN32) || defined (_WIN64)
#undef U
Expand All @@ -25,6 +21,10 @@
#include <cpprest/details/basic_types.h>
#include <cpprest/http_client.h>

#include <memory>
#include <vector>
#include <functional>

{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{>licenseInfo}}
#include "ApiClient.h"
#include "MultipartFormData.h"
#include "ModelBase.h"
#include "{{packageName}}/ApiClient.h"
#include "{{packageName}}/MultipartFormData.h"
#include "{{packageName}}/ModelBase.h"

#include <sstream>
#include <limits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

{{{defaultInclude}}}

#include <map>

#include <cpprest/details/basic_types.h>
#include <cpprest/http_client.h>

#include <map>

{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{>licenseInfo}}
#include "ApiConfiguration.h"
#include "{{packageName}}/ApiConfiguration.h"

{{#apiNamespaceDeclarations}}
namespace {{this}} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

{{{defaultInclude}}}

#include <memory>
#include <map>

#include <cpprest/details/basic_types.h>
#include <cpprest/http_msg.h>

#include <memory>
#include <map>

{{#apiNamespaceDeclarations}}
namespace {{this}} {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{>licenseInfo}}
#include "ApiException.h"
#include "{{packageName}}/ApiException.h"

{{#apiNamespaceDeclarations}}
namespace {{this}} {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@PACKAGE_INIT@

include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake)

check_required_components("@PROJECT_NAME@")
Loading