From 582b2bfe64b14eb77e506fceb6cb4f3447fdc7a3 Mon Sep 17 00:00:00 2001 From: Marius Dege Date: Mon, 1 Feb 2021 17:22:52 +0100 Subject: [PATCH 1/3] added simple Oauth bearer token in header --- .../src/main/resources/cpp-qt5-client/api-body.mustache | 9 ++++++++- .../main/resources/cpp-qt5-client/api-header.mustache | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache index 08ae9aa8ddbb..7e2aa00e923e 100644 --- a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache @@ -99,6 +99,10 @@ void {{classname}}::setBearerToken(const QString &token){ _bearerToken = token; } +void {{classname}}::setOauthToken(const QString &token){ + _oAuthToken = token; +} + void {{classname}}::setUsername(const QString &username) { _username = username; } @@ -167,7 +171,10 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName QByteArray b64; b64.append(_username.toUtf8() + ":" + _password.toUtf8()); addHeaders("Authorization","Basic " + b64.toBase64()); - }{{/isBasicBasic}}{{/authMethods}} + }{{/isBasicBasic}}{{#isOAuth}} + if(!_oAuthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oAuthToken); + {{/isOAuth}}{{/authMethods}} {{#queryParams}}{{^collectionFormat}} if (fullPath.indexOf("?") > 0) fullPath.append("&"); diff --git a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache index f802c2015da8..71a2f7fccbfb 100644 --- a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache @@ -33,6 +33,7 @@ public: void setPort(int port); void setApiKey(const QString &apiKeyName, const QString &apiKey); void setBearerToken(const QString &token); + void setOauthToken(const QString &token); void setUsername(const QString &username); void setPassword(const QString &password); void setBasePath(const QString &basePath); @@ -54,6 +55,7 @@ private: QMap> _serverConfigs; QMap _apiKeys; QString _bearerToken; + QString _oAuthToken; QString _username; QString _password; int _timeOut; From 0199067966cc97fd18ed354578447bd653c76911 Mon Sep 17 00:00:00 2001 From: Marius Dege Date: Thu, 25 Feb 2021 09:24:37 +0100 Subject: [PATCH 2/3] Added oauth token functionality --- .../cpp-qt5-client/api-body.mustache | 6 ++--- .../cpp-qt5-client/api-header.mustache | 2 +- .../petstore/cpp-qt5/client/PFXPetApi.cpp | 25 +++++++++++++++++++ .../petstore/cpp-qt5/client/PFXPetApi.h | 2 ++ .../petstore/cpp-qt5/client/PFXStoreApi.cpp | 4 +++ .../petstore/cpp-qt5/client/PFXStoreApi.h | 2 ++ .../petstore/cpp-qt5/client/PFXUserApi.cpp | 4 +++ .../petstore/cpp-qt5/client/PFXUserApi.h | 2 ++ 8 files changed, 43 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache index 7e2aa00e923e..7fbc89a132a1 100644 --- a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache @@ -100,7 +100,7 @@ void {{classname}}::setBearerToken(const QString &token){ } void {{classname}}::setOauthToken(const QString &token){ - _oAuthToken = token; + _oauthToken = token; } void {{classname}}::setUsername(const QString &username) { @@ -172,8 +172,8 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName b64.append(_username.toUtf8() + ":" + _password.toUtf8()); addHeaders("Authorization","Basic " + b64.toBase64()); }{{/isBasicBasic}}{{#isOAuth}} - if(!_oAuthToken.isEmpty()) - addHeaders("Authorization", "Bearer " + _oAuthToken); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); {{/isOAuth}}{{/authMethods}} {{#queryParams}}{{^collectionFormat}} if (fullPath.indexOf("?") > 0) diff --git a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache index 71a2f7fccbfb..31be25ac42a5 100644 --- a/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache @@ -55,7 +55,7 @@ private: QMap> _serverConfigs; QMap _apiKeys; QString _bearerToken; - QString _oAuthToken; + QString _oauthToken; QString _username; QString _password; int _timeOut; diff --git a/samples/client/petstore/cpp-qt5/client/PFXPetApi.cpp b/samples/client/petstore/cpp-qt5/client/PFXPetApi.cpp index 14e5285627b7..a9d1700acace 100644 --- a/samples/client/petstore/cpp-qt5/client/PFXPetApi.cpp +++ b/samples/client/petstore/cpp-qt5/client/PFXPetApi.cpp @@ -105,6 +105,10 @@ void PFXPetApi::setBearerToken(const QString &token){ _bearerToken = token; } +void PFXPetApi::setOauthToken(const QString &token){ + _oauthToken = token; +} + void PFXPetApi::setUsername(const QString &username) { _username = username; } @@ -148,6 +152,9 @@ void PFXPetApi::abortRequests(){ void PFXPetApi::addPet(const PFXPet &body) { QString fullPath = QString(_serverConfigs["addPet"][_serverIndices.value("addPet")].URL()+"/pet"); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); + PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager); worker->setTimeOut(_timeOut); @@ -192,6 +199,9 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const QString &api_key) { pet_idPathParam.append("petId").append("}"); fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id))); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); + PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager); worker->setTimeOut(_timeOut); @@ -234,6 +244,9 @@ void PFXPetApi::deletePetCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::findPetsByStatus(const QList &status) { QString fullPath = QString(_serverConfigs["findPetsByStatus"][_serverIndices.value("findPetsByStatus")].URL()+"/pet/findByStatus"); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); + if (status.size() > 0) { if (QString("csv").indexOf("multi") == 0) { @@ -320,6 +333,9 @@ void PFXPetApi::findPetsByStatusCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::findPetsByTags(const QList &tags) { QString fullPath = QString(_serverConfigs["findPetsByTags"][_serverIndices.value("findPetsByTags")].URL()+"/pet/findByTags"); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); + if (tags.size() > 0) { if (QString("csv").indexOf("multi") == 0) { @@ -452,6 +468,9 @@ void PFXPetApi::getPetByIdCallback(PFXHttpRequestWorker *worker) { void PFXPetApi::updatePet(const PFXPet &body) { QString fullPath = QString(_serverConfigs["updatePet"][_serverIndices.value("updatePet")].URL()+"/pet"); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); + PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager); worker->setTimeOut(_timeOut); @@ -496,6 +515,9 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const QString &name, con pet_idPathParam.append("petId").append("}"); fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id))); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); + PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager); worker->setTimeOut(_timeOut); @@ -539,6 +561,9 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const QString &additional_metad pet_idPathParam.append("petId").append("}"); fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id))); + if(!_oauthToken.isEmpty()) + addHeaders("Authorization", "Bearer " + _oauthToken); + PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager); worker->setTimeOut(_timeOut); diff --git a/samples/client/petstore/cpp-qt5/client/PFXPetApi.h b/samples/client/petstore/cpp-qt5/client/PFXPetApi.h index 7c3e9b331f3e..ce5655e2101e 100644 --- a/samples/client/petstore/cpp-qt5/client/PFXPetApi.h +++ b/samples/client/petstore/cpp-qt5/client/PFXPetApi.h @@ -43,6 +43,7 @@ class PFXPetApi : public QObject { void setPort(int port); void setApiKey(const QString &apiKeyName, const QString &apiKey); void setBearerToken(const QString &token); + void setOauthToken(const QString &token); void setUsername(const QString &username); void setPassword(const QString &password); void setBasePath(const QString &basePath); @@ -71,6 +72,7 @@ class PFXPetApi : public QObject { QMap> _serverConfigs; QMap _apiKeys; QString _bearerToken; + QString _oauthToken; QString _username; QString _password; int _timeOut; diff --git a/samples/client/petstore/cpp-qt5/client/PFXStoreApi.cpp b/samples/client/petstore/cpp-qt5/client/PFXStoreApi.cpp index 211e318b6881..1219d959d478 100644 --- a/samples/client/petstore/cpp-qt5/client/PFXStoreApi.cpp +++ b/samples/client/petstore/cpp-qt5/client/PFXStoreApi.cpp @@ -93,6 +93,10 @@ void PFXStoreApi::setBearerToken(const QString &token){ _bearerToken = token; } +void PFXStoreApi::setOauthToken(const QString &token){ + _oauthToken = token; +} + void PFXStoreApi::setUsername(const QString &username) { _username = username; } diff --git a/samples/client/petstore/cpp-qt5/client/PFXStoreApi.h b/samples/client/petstore/cpp-qt5/client/PFXStoreApi.h index d065169e181d..9cfbee6231b8 100644 --- a/samples/client/petstore/cpp-qt5/client/PFXStoreApi.h +++ b/samples/client/petstore/cpp-qt5/client/PFXStoreApi.h @@ -42,6 +42,7 @@ class PFXStoreApi : public QObject { void setPort(int port); void setApiKey(const QString &apiKeyName, const QString &apiKey); void setBearerToken(const QString &token); + void setOauthToken(const QString &token); void setUsername(const QString &username); void setPassword(const QString &password); void setBasePath(const QString &basePath); @@ -66,6 +67,7 @@ class PFXStoreApi : public QObject { QMap> _serverConfigs; QMap _apiKeys; QString _bearerToken; + QString _oauthToken; QString _username; QString _password; int _timeOut; diff --git a/samples/client/petstore/cpp-qt5/client/PFXUserApi.cpp b/samples/client/petstore/cpp-qt5/client/PFXUserApi.cpp index 389e99f83d24..eddb80f10b24 100644 --- a/samples/client/petstore/cpp-qt5/client/PFXUserApi.cpp +++ b/samples/client/petstore/cpp-qt5/client/PFXUserApi.cpp @@ -105,6 +105,10 @@ void PFXUserApi::setBearerToken(const QString &token){ _bearerToken = token; } +void PFXUserApi::setOauthToken(const QString &token){ + _oauthToken = token; +} + void PFXUserApi::setUsername(const QString &username) { _username = username; } diff --git a/samples/client/petstore/cpp-qt5/client/PFXUserApi.h b/samples/client/petstore/cpp-qt5/client/PFXUserApi.h index aada5d8f3bf4..9dfa51680ac4 100644 --- a/samples/client/petstore/cpp-qt5/client/PFXUserApi.h +++ b/samples/client/petstore/cpp-qt5/client/PFXUserApi.h @@ -42,6 +42,7 @@ class PFXUserApi : public QObject { void setPort(int port); void setApiKey(const QString &apiKeyName, const QString &apiKey); void setBearerToken(const QString &token); + void setOauthToken(const QString &token); void setUsername(const QString &username); void setPassword(const QString &password); void setBasePath(const QString &basePath); @@ -70,6 +71,7 @@ class PFXUserApi : public QObject { QMap> _serverConfigs; QMap _apiKeys; QString _bearerToken; + QString _oauthToken; QString _username; QString _password; int _timeOut; From 62a8931d12a3b39bec1f77699dc48a23b85e9c9e Mon Sep 17 00:00:00 2001 From: Marius Dege Date: Thu, 25 Feb 2021 09:36:46 +0100 Subject: [PATCH 3/3] updated doc --- docs/generators/cpp-qt5-client.md | 2 +- .../org/openapitools/codegen/languages/CppQt5ClientCodegen.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/generators/cpp-qt5-client.md b/docs/generators/cpp-qt5-client.md index ada59e68cea1..ef2ae4244485 100644 --- a/docs/generators/cpp-qt5-client.md +++ b/docs/generators/cpp-qt5-client.md @@ -236,7 +236,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |ApiKey|✓|OAS2,OAS3 |OpenIDConnect|✗|OAS3 |BearerToken|✓|OAS3 -|OAuth2_Implicit|✗|OAS2,OAS3 +|OAuth2_Implicit|✓|OAS2,OAS3 |OAuth2_Password|✗|OAS2,OAS3 |OAuth2_ClientCredentials|✗|OAS2,OAS3 |OAuth2_AuthorizationCode|✗|OAS2,OAS3 diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java index 7a383a1e87eb..3ea7c8100f20 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java @@ -46,6 +46,7 @@ public CppQt5ClientCodegen() { .includeSecurityFeatures(SecurityFeature.BasicAuth) .includeSecurityFeatures(SecurityFeature.ApiKey) .includeSecurityFeatures(SecurityFeature.BearerToken) + .includeSecurityFeatures(SecurityFeature.OAuth2_Implicit) ); // set the output folder here