[C++][Qt5] added Authentication Support (#8004)

* first commit of api Key feature.

* added multi key and URL query key support

* fixed error in mustache files, updated samples and tests

* added Basic Auth with Base64 encoding.

* updated Readme, added bearer token

* added check that authentication credentials are set. fixed typo in README
This commit is contained in:
basyskom-dege
2020-11-28 08:02:09 +01:00
committed by GitHub
parent 045d05fa72
commit cb1a620628
13 changed files with 517 additions and 1 deletions

View File

@@ -37,6 +37,7 @@ void PetApiTests::findPetsByStatusTest() {
void PetApiTests::createAndGetPetTest() {
PFXPetApi api;
api.setApiKey("api_key","special-key");
QEventLoop loop;
bool petCreated = false;

View File

@@ -6,6 +6,8 @@
void StoreApiTests::placeOrderTest() {
PFXStoreApi api;
// api.setUsername("TestName");
// api.setPassword("TestPassword");
QEventLoop loop;
bool orderPlaced = false;
@@ -36,6 +38,7 @@ void StoreApiTests::placeOrderTest() {
void StoreApiTests::getOrderByIdTest() {
PFXStoreApi api;
api.setApiKey("api_key_2","testKey");
QEventLoop loop;
bool orderFetched = false;
@@ -59,6 +62,7 @@ void StoreApiTests::getOrderByIdTest() {
void StoreApiTests::getInventoryTest() {
PFXStoreApi api;
api.setApiKey("api_key","special-key");
QEventLoop loop;
bool inventoryFetched = false;

View File

@@ -0,0 +1,174 @@
#
OpenAPI Petstore
- API version: 1.0.0
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*Automatically generated by the [OpenAPI Generator](https://openapi-generator.tech)*
## Licence
Apache-2.0
for more information visit [Apache-2.0](https://www.apache.org/licenses/LICENSE-2.0.html)
## Requirements
Building the API client library requires:
1. Java 1.7+
2. Maven/Gradle
## Installation
To install the API client library to your local Maven repository, simply execute:
```shell
mvn clean install
```
To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:
```shell
mvn clean deploy
```
Refer to the [OSSRH Guide](http://central.sonatype.org/pages/ossrh-guide.html) for more information.
### Generate manually
At first generate the JAR by executing:
```shell
mvn clean package
```
Use the Jar file to generate the Qt5 Client:
```shell
java -jar target/openapi-generator-cli.jar generate -i <yourOpenApiFile> -g cpp-qt5-client -o <outputDir>
```
## Getting Started
Please follow the [installation](#installation) instruction and execute the following Java code:
example.h:
```c++
#include <iostream>
#include "../client/PFXPetApi.h"
using namespace test_namespace;
class Example : public QObject {
Q_OBJECT
PFXPet create();
public slots:
void exampleFunction1();
};
```
example.cpp:
```c++
#include "../client/PFXPetApi.h"
#include "example.h"
#include <QTimer>
#include <QEventLoop>
PFXPet Example::create(){
PFXPet obj;
return obj;
}
void Example::exampleFunction1(){
PFXPetApi apiInstance;
//OAuth Authentication supported right now
QEventLoop loop;
connect(&apiInstance, &PFXPetApi::addPetSignal, [&]() {
loop.quit();
});
connect(&apiInstance, &PFXPetApi::addPetSignalE, [&](QNetworkReply::NetworkError, QString error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
loop.quit();
});
PFXPet body = create(); // PFXPet | Pet object that needs to be added to the store
apiInstance.addPet(body);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
}
```
## Documentation for API Endpoints
All URIs are relative to *http://petstore.swagger.io/v2*
Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*PFXPetApi* | [**addPet**](PFXPetApi.md#addPet) | **POST** /pet | Add a new pet to the store
*PFXPetApi* | [**deletePet**](PFXPetApi.md#deletePet) | **DELETE** /pet/{petId} | Deletes a pet
*PFXPetApi* | [**findPetsByStatus**](PFXPetApi.md#findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status
*PFXPetApi* | [**findPetsByTags**](PFXPetApi.md#findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
*PFXPetApi* | [**getPetById**](PFXPetApi.md#getPetById) | **GET** /pet/{petId} | Find pet by ID
*PFXPetApi* | [**updatePet**](PFXPetApi.md#updatePet) | **PUT** /pet | Update an existing pet
*PFXPetApi* | [**updatePetWithForm**](PFXPetApi.md#updatePetWithForm) | **POST** /pet/{petId} | Updates a pet in the store with form data
*PFXPetApi* | [**uploadFile**](PFXPetApi.md#uploadFile) | **POST** /pet/{petId}/uploadImage | uploads an image
*PFXStoreApi* | [**deleteOrder**](PFXStoreApi.md#deleteOrder) | **DELETE** /store/order/{orderId} | Delete purchase order by ID
*PFXStoreApi* | [**getInventory**](PFXStoreApi.md#getInventory) | **GET** /store/inventory | Returns pet inventories by status
*PFXStoreApi* | [**getOrderById**](PFXStoreApi.md#getOrderById) | **GET** /store/order/{orderId} | Find purchase order by ID
*PFXStoreApi* | [**placeOrder**](PFXStoreApi.md#placeOrder) | **POST** /store/order | Place an order for a pet
*PFXUserApi* | [**createUser**](PFXUserApi.md#createUser) | **POST** /user | Create user
*PFXUserApi* | [**createUsersWithArrayInput**](PFXUserApi.md#createUsersWithArrayInput) | **POST** /user/createWithArray | Creates list of users with given input array
*PFXUserApi* | [**createUsersWithListInput**](PFXUserApi.md#createUsersWithListInput) | **POST** /user/createWithList | Creates list of users with given input array
*PFXUserApi* | [**deleteUser**](PFXUserApi.md#deleteUser) | **DELETE** /user/{username} | Delete user
*PFXUserApi* | [**getUserByName**](PFXUserApi.md#getUserByName) | **GET** /user/{username} | Get user by user name
*PFXUserApi* | [**loginUser**](PFXUserApi.md#loginUser) | **GET** /user/login | Logs user into the system
*PFXUserApi* | [**logoutUser**](PFXUserApi.md#logoutUser) | **GET** /user/logout | Logs out current logged in user session
*PFXUserApi* | [**updateUser**](PFXUserApi.md#updateUser) | **PUT** /user/{username} | Updated user
## Documentation for Models
- [PFXApiResponse](PFXApiResponse.md)
- [PFXCategory](PFXCategory.md)
- [PFXOrder](PFXOrder.md)
- [PFXPet](PFXPet.md)
- [PFXTag](PFXTag.md)
- [PFXUser](PFXUser.md)
## Documentation for Authorization
Authentication schemes defined for the API:
### api_key
- **Type**: API key
- **API key parameter name**: api_key
- **Location**: HTTP header
### petstore_auth
- **Type**: OAuth
- **Flow**: implicit
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
- **Scopes**:
- write:pets: modify pets in your account
- read:pets: read your pets
## Author

View File

@@ -42,6 +42,22 @@ void PFXPetApi::setPort(int port) {
_port = port;
}
void PFXPetApi::setApiKey(const QString &apiKeyName, const QString &apiKey){
_apiKeys.insert(apiKeyName,apiKey);
}
void PFXPetApi::setBearerToken(const QString &token){
_bearerToken = token;
}
void PFXPetApi::setUsername(const QString &username) {
_username = username;
}
void PFXPetApi::setPassword(const QString &password) {
_password = password;
}
void PFXPetApi::setBasePath(const QString &basePath) {
_basePath = basePath;
}
@@ -82,6 +98,7 @@ void PFXPetApi::addPet(const PFXPet &body) {
.arg(_basePath)
.arg("/pet");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
@@ -129,6 +146,7 @@ void PFXPetApi::deletePet(const qint64 &pet_id, const QString &api_key) {
QString pet_idPathParam("{");
pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
@@ -176,6 +194,7 @@ void PFXPetApi::findPetsByStatus(const QList<QString> &status) {
.arg(_basePath)
.arg("/pet/findByStatus");
if (status.size() > 0) {
if (QString("csv").indexOf("multi") == 0) {
foreach (QString t, status) {
@@ -266,6 +285,7 @@ void PFXPetApi::findPetsByTags(const QList<QString> &tags) {
.arg(_basePath)
.arg("/pet/findByTags");
if (tags.size() > 0) {
if (QString("csv").indexOf("multi") == 0) {
foreach (QString t, tags) {
@@ -358,6 +378,11 @@ void PFXPetApi::getPetById(const qint64 &pet_id) {
QString pet_idPathParam("{");
pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
if(_apiKeys.contains("api_key")){
addHeaders("api_key",_apiKeys.find("api_key").value());
}
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
@@ -402,6 +427,7 @@ void PFXPetApi::updatePet(const PFXPet &body) {
.arg(_basePath)
.arg("/pet");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
@@ -449,6 +475,7 @@ void PFXPetApi::updatePetWithForm(const qint64 &pet_id, const QString &name, con
QString pet_idPathParam("{");
pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
@@ -496,6 +523,7 @@ void PFXPetApi::uploadFile(const qint64 &pet_id, const QString &additional_metad
QString pet_idPathParam("{");
pet_idPathParam.append("petId").append("}");
fullPath.replace(pet_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(pet_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);

View File

@@ -20,6 +20,8 @@
#include <QString>
#include <QObject>
#include <QByteArray>
#include <QStringList>
#include <QNetworkAccessManager>
namespace test_namespace {
@@ -34,6 +36,10 @@ public:
void setScheme(const QString &scheme);
void setHost(const QString &host);
void setPort(int port);
void setApiKey(const QString &apiKeyName, const QString &apiKey);
void setBearerToken(const QString &token);
void setUsername(const QString &username);
void setPassword(const QString &password);
void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path);
@@ -55,6 +61,10 @@ public:
private:
QString _scheme, _host;
int _port;
QMap<QString, QString> _apiKeys;
QString _bearerToken;
QString _username;
QString _password;
QString _basePath;
int _timeOut;
QString _workingDirectory;

View File

@@ -42,6 +42,22 @@ void PFXStoreApi::setPort(int port) {
_port = port;
}
void PFXStoreApi::setApiKey(const QString &apiKeyName, const QString &apiKey){
_apiKeys.insert(apiKeyName,apiKey);
}
void PFXStoreApi::setBearerToken(const QString &token){
_bearerToken = token;
}
void PFXStoreApi::setUsername(const QString &username) {
_username = username;
}
void PFXStoreApi::setPassword(const QString &password) {
_password = password;
}
void PFXStoreApi::setBasePath(const QString &basePath) {
_basePath = basePath;
}
@@ -84,6 +100,7 @@ void PFXStoreApi::deleteOrder(const QString &order_id) {
QString order_idPathParam("{");
order_idPathParam.append("orderId").append("}");
fullPath.replace(order_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(order_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
@@ -127,6 +144,11 @@ void PFXStoreApi::getInventory() {
.arg(_basePath)
.arg("/store/inventory");
if(_apiKeys.contains("api_key")){
addHeaders("api_key",_apiKeys.find("api_key").value());
}
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
@@ -181,6 +203,7 @@ void PFXStoreApi::getOrderById(const qint64 &order_id) {
QString order_idPathParam("{");
order_idPathParam.append("orderId").append("}");
fullPath.replace(order_idPathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(order_id)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
@@ -225,6 +248,7 @@ void PFXStoreApi::placeOrder(const PFXOrder &body) {
.arg(_basePath)
.arg("/store/order");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);

View File

@@ -19,6 +19,8 @@
#include <QString>
#include <QObject>
#include <QByteArray>
#include <QStringList>
#include <QNetworkAccessManager>
namespace test_namespace {
@@ -33,6 +35,10 @@ public:
void setScheme(const QString &scheme);
void setHost(const QString &host);
void setPort(int port);
void setApiKey(const QString &apiKeyName, const QString &apiKey);
void setBearerToken(const QString &token);
void setUsername(const QString &username);
void setPassword(const QString &password);
void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path);
@@ -50,6 +56,10 @@ public:
private:
QString _scheme, _host;
int _port;
QMap<QString, QString> _apiKeys;
QString _bearerToken;
QString _username;
QString _password;
QString _basePath;
int _timeOut;
QString _workingDirectory;

View File

@@ -42,6 +42,22 @@ void PFXUserApi::setPort(int port) {
_port = port;
}
void PFXUserApi::setApiKey(const QString &apiKeyName, const QString &apiKey){
_apiKeys.insert(apiKeyName,apiKey);
}
void PFXUserApi::setBearerToken(const QString &token){
_bearerToken = token;
}
void PFXUserApi::setUsername(const QString &username) {
_username = username;
}
void PFXUserApi::setPassword(const QString &password) {
_password = password;
}
void PFXUserApi::setBasePath(const QString &basePath) {
_basePath = basePath;
}
@@ -82,6 +98,7 @@ void PFXUserApi::createUser(const PFXUser &body) {
.arg(_basePath)
.arg("/user");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
@@ -127,6 +144,7 @@ void PFXUserApi::createUsersWithArrayInput(const QList<PFXUser> &body) {
.arg(_basePath)
.arg("/user/createWithArray");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
@@ -173,6 +191,7 @@ void PFXUserApi::createUsersWithListInput(const QList<PFXUser> &body) {
.arg(_basePath)
.arg("/user/createWithList");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
@@ -221,6 +240,7 @@ void PFXUserApi::deleteUser(const QString &username) {
QString usernamePathParam("{");
usernamePathParam.append("username").append("}");
fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
@@ -266,6 +286,7 @@ void PFXUserApi::getUserByName(const QString &username) {
QString usernamePathParam("{");
usernamePathParam.append("username").append("}");
fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
@@ -310,6 +331,7 @@ void PFXUserApi::loginUser(const QString &username, const QString &password) {
.arg(_basePath)
.arg("/user/login");
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
@@ -366,6 +388,7 @@ void PFXUserApi::logoutUser() {
.arg(_basePath)
.arg("/user/logout");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
@@ -410,6 +433,7 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &body) {
QString usernamePathParam("{");
usernamePathParam.append("username").append("}");
fullPath.replace(usernamePathParam, QUrl::toPercentEncoding(::test_namespace::toStringValue(username)));
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);

View File

@@ -19,6 +19,8 @@
#include <QString>
#include <QObject>
#include <QByteArray>
#include <QStringList>
#include <QNetworkAccessManager>
namespace test_namespace {
@@ -33,6 +35,10 @@ public:
void setScheme(const QString &scheme);
void setHost(const QString &host);
void setPort(int port);
void setApiKey(const QString &apiKeyName, const QString &apiKey);
void setBearerToken(const QString &token);
void setUsername(const QString &username);
void setPassword(const QString &password);
void setBasePath(const QString &basePath);
void setTimeOut(const int timeOut);
void setWorkingDirectory(const QString &path);
@@ -54,6 +60,10 @@ public:
private:
QString _scheme, _host;
int _port;
QMap<QString, QString> _apiKeys;
QString _bearerToken;
QString _username;
QString _password;
QString _basePath;
int _timeOut;
QString _workingDirectory;