mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 20:50:55 +00:00
* fixed missing package, fixed namespace issues * using older function to check if token is valid * using time.h to check if token is valid
181 lines
4.3 KiB
C++
181 lines
4.3 KiB
C++
/**
|
|
* OpenAPI Petstore
|
|
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
/**
|
|
* Providing a Oauth2 Class and a ReplyServer for the Oauth2 authorization code flow.
|
|
*/
|
|
#ifndef PFX_OAUTH2_H
|
|
#define PFX_OAUTH2_H
|
|
|
|
#include <QObject>
|
|
#include <QtCore>
|
|
#include <QtNetwork>
|
|
#include <QDesktopServices>
|
|
#include <QNetworkRequest>
|
|
#include <QNetworkReply>
|
|
#include <QNetworkAccessManager>
|
|
#include <QtDebug>
|
|
#include <QTcpServer>
|
|
#include <QTcpSocket>
|
|
#include <QByteArray>
|
|
#include <QString>
|
|
#include <QMap>
|
|
#include <QUrl>
|
|
#include <QUrlQuery>
|
|
#include <QDateTime>
|
|
#include <time.h>
|
|
|
|
namespace test_namespace {
|
|
|
|
class oauthToken
|
|
{
|
|
public:
|
|
oauthToken(QString token, int expiresIn, QString scope, QString tokenType) : m_token(token), m_scope(scope), m_type(tokenType){
|
|
m_validUntil = time(0) + expiresIn;
|
|
}
|
|
oauthToken(){
|
|
m_validUntil = time(0) - 1;
|
|
}
|
|
QString getToken(){return m_token;};
|
|
QString getScope(){return m_scope;};
|
|
QString getType(){return m_type;};
|
|
bool isValid(){return time(0) < m_validUntil;};
|
|
|
|
private:
|
|
QString m_token;
|
|
time_t m_validUntil;
|
|
QString m_scope;
|
|
QString m_type;
|
|
};
|
|
|
|
class ReplyServer : public QTcpServer
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit ReplyServer(QObject *parent = nullptr);
|
|
void setReply(QByteArray reply){m_reply = reply;};
|
|
void run();
|
|
private:
|
|
QByteArray m_reply;
|
|
|
|
signals:
|
|
void dataReceived(QMap<QString, QString>);
|
|
|
|
public slots:
|
|
void onConnected();
|
|
void read();
|
|
void start();
|
|
void stop();
|
|
};
|
|
|
|
//Base class
|
|
class OauthBase : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
OauthBase(QObject* parent = nullptr) : QObject(parent) {};
|
|
oauthToken getToken(QString scope);
|
|
void addToken(oauthToken token);
|
|
void removeToken(QString scope);
|
|
bool linked(){return m_linked;};
|
|
virtual void link()=0;
|
|
virtual void unlink()=0;
|
|
|
|
protected:
|
|
QMap<QString, oauthToken> m_oauthTokenMap;
|
|
QUrl m_authUrl;
|
|
QUrl m_tokenUrl;
|
|
QString m_scope, m_accessType, m_state, m_redirectUri, m_clientId, m_clientSecret;
|
|
bool m_linked;
|
|
|
|
public slots:
|
|
virtual void authenticationNeededCallback()=0;
|
|
void onFinish(QNetworkReply *rep);
|
|
|
|
signals:
|
|
void authenticationNeeded();
|
|
void tokenReceived();
|
|
};
|
|
|
|
// Authorization code flow
|
|
class OauthCode : public OauthBase
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
OauthCode(QObject *parent = nullptr);
|
|
void link() override;
|
|
void unlink() override;
|
|
void setVariables(QString authUrl, QString tokenUrl, QString scope, QString state, QString redirectUri, QString clientId, QString clientSecret, QString accessType = "");
|
|
|
|
private:
|
|
ReplyServer m_server;
|
|
|
|
public slots:
|
|
void authenticationNeededCallback() override;
|
|
void onVerificationReceived(const QMap<QString, QString> response);
|
|
|
|
};
|
|
|
|
// Implicit flow
|
|
class OauthImplicit : public OauthBase
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
OauthImplicit(QObject *parent = nullptr);
|
|
void link() override;
|
|
void unlink() override;
|
|
void setVariables(QString authUrl, QString scope, QString state, QString redirectUri, QString clientId, QString accessType = "");
|
|
|
|
private:
|
|
ReplyServer m_server;
|
|
|
|
public slots:
|
|
void authenticationNeededCallback() override;
|
|
void ImplicitTokenReceived(const QMap<QString, QString> response);
|
|
};
|
|
|
|
//client credentials flow
|
|
class OauthCredentials : public OauthBase
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
OauthCredentials(QObject *parent = nullptr);
|
|
void link() override;
|
|
void unlink() override;
|
|
void setVariables(QString tokenUrl, QString scope, QString clientId, QString clientSecret);
|
|
|
|
public slots:
|
|
void authenticationNeededCallback() override;
|
|
|
|
};
|
|
|
|
//resource owner password flow
|
|
class OauthPassword : public OauthBase
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
OauthPassword(QObject *parent = nullptr);
|
|
void link() override;
|
|
void unlink() override;
|
|
void setVariables(QString tokenUrl, QString scope, QString clientId, QString clientSecret, QString username, QString password);
|
|
|
|
private:
|
|
QString m_username, m_password;
|
|
|
|
public slots:
|
|
void authenticationNeededCallback() override;
|
|
|
|
};
|
|
|
|
|
|
} // namespace test_namespace
|
|
#endif // PFX_OAUTH2_H
|