Skip to content

Commit

Permalink
added modules for oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
treefrogframework committed Nov 14, 2019
1 parent f2f017b commit d83a639
Showing 6 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/TOAuth2Client
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "toauth2client.h"
4 changes: 2 additions & 2 deletions include/headers.pri
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
HEADER_CLASSES = ../include/TAbstractModel ../include/TAbstractUser ../include/TActionContext ../include/TActionController ../include/TActionHelper ../include/TActionThread ../include/TActionView ../include/TPrototypeAjaxHelper ../include/TApplicationServerBase ../include/TThreadApplicationServer ../include/TPreforkApplicationServer ../include/TContentHeader ../include/TCookie ../include/TCookieJar ../include/TCriteria ../include/TCriteriaConverter ../include/TCryptMac ../include/TDirectView ../include/TDispatcher ../include/TGlobal ../include/THtmlAttribute ../include/THtmlParser ../include/THttpHeader ../include/THttpRequest ../include/THttpRequestHeader ../include/THttpResponse ../include/THttpResponseHeader ../include/THttpUtility ../include/TInternetMessageHeader ../include/TJavaScriptObject ../include/TLog ../include/TLogger ../include/TLoggerPlugin ../include/TMailMessage ../include/TModelUtil ../include/TMultipartFormData ../include/TOption ../include/TSession ../include/TSessionStore ../include/TSessionStorePlugin ../include/TSharedMemoryLogStream ../include/TSmtpMailer ../include/TSqlORMapper ../include/TSqlORMapperIterator ../include/TSqlObject ../include/TSqlQuery ../include/TSqlQueryORMapper ../include/TSystemGlobal ../include/TTemporaryFile ../include/TViewHelper ../include/TWebApplication ../include/TfException ../include/TfNamespace ../include/TreeFrogController ../include/TreeFrogModel ../include/TreeFrogView ../include/TAbstractController ../include/TActionMailer ../include/TFormValidator ../include/TSqlQueryORMapperIterator ../include/TAccessValidator ../include/TSqlTransaction ../include/TPaginator ../include/TKvsDatabase ../include/TKvsDriver ../include/TModelObject ../include/TPopMailer ../include/TMultiplexingServer ../include/TAccessLog ../include/TActionWorker ../include/TAtomicQueue ../include/TJsonUtil ../include/TScheduler ../include/TApplicationScheduler ../include/TCommandLineInterface ../include/TSendmailMailer ../include/TAppSettings ../include/TWebSocketEndpoint ../include/TDatabaseContext ../include/TDatabaseContextThread ../include/TWebSocketSession ../include/TRedis ../include/TSqlJoin ../include/THazardPtrManager ../include/TAtomic ../include/TAtomicPtr ../include/TDebug ../include/TBackgroundProcess ../include/TBackgroundProcessHandler ../include/TCache ../include/THttpClient
HEADER_CLASSES = ../include/TAbstractModel ../include/TAbstractUser ../include/TActionContext ../include/TActionController ../include/TActionHelper ../include/TActionThread ../include/TActionView ../include/TPrototypeAjaxHelper ../include/TApplicationServerBase ../include/TThreadApplicationServer ../include/TPreforkApplicationServer ../include/TContentHeader ../include/TCookie ../include/TCookieJar ../include/TCriteria ../include/TCriteriaConverter ../include/TCryptMac ../include/TDirectView ../include/TDispatcher ../include/TGlobal ../include/THtmlAttribute ../include/THtmlParser ../include/THttpHeader ../include/THttpRequest ../include/THttpRequestHeader ../include/THttpResponse ../include/THttpResponseHeader ../include/THttpUtility ../include/TInternetMessageHeader ../include/TJavaScriptObject ../include/TLog ../include/TLogger ../include/TLoggerPlugin ../include/TMailMessage ../include/TModelUtil ../include/TMultipartFormData ../include/TOption ../include/TSession ../include/TSessionStore ../include/TSessionStorePlugin ../include/TSharedMemoryLogStream ../include/TSmtpMailer ../include/TSqlORMapper ../include/TSqlORMapperIterator ../include/TSqlObject ../include/TSqlQuery ../include/TSqlQueryORMapper ../include/TSystemGlobal ../include/TTemporaryFile ../include/TViewHelper ../include/TWebApplication ../include/TfException ../include/TfNamespace ../include/TreeFrogController ../include/TreeFrogModel ../include/TreeFrogView ../include/TAbstractController ../include/TActionMailer ../include/TFormValidator ../include/TSqlQueryORMapperIterator ../include/TAccessValidator ../include/TSqlTransaction ../include/TPaginator ../include/TKvsDatabase ../include/TKvsDriver ../include/TModelObject ../include/TPopMailer ../include/TMultiplexingServer ../include/TAccessLog ../include/TActionWorker ../include/TAtomicQueue ../include/TJsonUtil ../include/TScheduler ../include/TApplicationScheduler ../include/TCommandLineInterface ../include/TSendmailMailer ../include/TAppSettings ../include/TWebSocketEndpoint ../include/TDatabaseContext ../include/TDatabaseContextThread ../include/TWebSocketSession ../include/TRedis ../include/TSqlJoin ../include/THazardPtrManager ../include/TAtomic ../include/TAtomicPtr ../include/TDebug ../include/TBackgroundProcess ../include/TBackgroundProcessHandler ../include/TCache ../include/THttpClient ../include/TOAuth2Client

HEADER_FILES = tabstractmodel.h tabstractuser.h tactioncontext.h tactioncontroller.h tactionhelper.h tactionthread.h tactionview.h tprototypeajaxhelper.h tapplicationserverbase.h tthreadapplicationserver.h tpreforkapplicationserver.h tcontentheader.h tcookie.h tcookiejar.h tcriteria.h tcriteriaconverter.h tcryptmac.h tdirectview.h tdispatcher.h tfcore.h tfexception.h tfnamespace.h tglobal.h thtmlattribute.h thtmlparser.h thttpheader.h thttprequest.h thttprequestheader.h thttpresponse.h thttpresponseheader.h thttputility.h tinternetmessageheader.h tjavascriptobject.h tlog.h tlogger.h tloggerplugin.h tmailmessage.h tmodelutil.h tmultipartformdata.h toption.h tsession.h tsessionstore.h tsessionstoreplugin.h tsharedmemorylogstream.h tsmtpmailer.h tsqlobject.h tsqlormapper.h tsqlormapperiterator.h tsqlquery.h tsqlqueryormapper.h tsystemglobal.h ttemporaryfile.h tviewhelper.h twebapplication.h tabstractcontroller.h tactionmailer.h tformvalidator.h tsqlqueryormapperiterator.h taccessvalidator.h tsqltransaction.h tpaginator.h tkvsdatabase.h tkvsdriver.h tmodelobject.h tpopmailer.h tmultiplexingserver.h taccesslog.h tactionworker.h tatomicqueue.h tjsonutil.h tscheduler.h tapplicationscheduler.h tcommandlineinterface.h tsendmailmailer.h tappsettings.h twebsocketendpoint.h tdatabasecontext.h tdatabasecontextthread.h tsystembus.h tprocessinfo.h twebsocketsession.h tredis.h tsqljoin.h thazardptrmanager.h tatomic.h tatomicptr.h tdebug.h tbackgroundprocess.h tbackgroundprocesshandler.h tcache.h thttpclient.h
HEADER_FILES = tabstractmodel.h tabstractuser.h tactioncontext.h tactioncontroller.h tactionhelper.h tactionthread.h tactionview.h tprototypeajaxhelper.h tapplicationserverbase.h tthreadapplicationserver.h tpreforkapplicationserver.h tcontentheader.h tcookie.h tcookiejar.h tcriteria.h tcriteriaconverter.h tcryptmac.h tdirectview.h tdispatcher.h tfcore.h tfexception.h tfnamespace.h tglobal.h thtmlattribute.h thtmlparser.h thttpheader.h thttprequest.h thttprequestheader.h thttpresponse.h thttpresponseheader.h thttputility.h tinternetmessageheader.h tjavascriptobject.h tlog.h tlogger.h tloggerplugin.h tmailmessage.h tmodelutil.h tmultipartformdata.h toption.h tsession.h tsessionstore.h tsessionstoreplugin.h tsharedmemorylogstream.h tsmtpmailer.h tsqlobject.h tsqlormapper.h tsqlormapperiterator.h tsqlquery.h tsqlqueryormapper.h tsystemglobal.h ttemporaryfile.h tviewhelper.h twebapplication.h tabstractcontroller.h tactionmailer.h tformvalidator.h tsqlqueryormapperiterator.h taccessvalidator.h tsqltransaction.h tpaginator.h tkvsdatabase.h tkvsdriver.h tmodelobject.h tpopmailer.h tmultiplexingserver.h taccesslog.h tactionworker.h tatomicqueue.h tjsonutil.h tscheduler.h tapplicationscheduler.h tcommandlineinterface.h tsendmailmailer.h tappsettings.h twebsocketendpoint.h tdatabasecontext.h tdatabasecontextthread.h tsystembus.h tprocessinfo.h twebsocketsession.h tredis.h tsqljoin.h thazardptrmanager.h tatomic.h tatomicptr.h tdebug.h tbackgroundprocess.h tbackgroundprocesshandler.h tcache.h thttpclient.h toauth2client.h

HEADER_FILES += tsqldatabasepool.h tkvsdatabasepool.h tstack.h thazardobject.h thazardptr.h

1 change: 1 addition & 0 deletions include/toauth2client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../src/toauth2client.h"
2 changes: 2 additions & 0 deletions src/corelib.pro
Original file line number Diff line number Diff line change
@@ -320,6 +320,8 @@ SOURCES += tcachemongostore.cpp
HEADERS += tcacheredisstore.h
SOURCES += tcacheredisstore.cpp
SOURCES += tactioncontroller_qt5.cpp
HEADERS += toauth2client.h
SOURCES += toauth2client.cpp

HEADERS += \
tfnamespace.h \
59 changes: 59 additions & 0 deletions src/toauth2client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "toauth2client.h"
#include "thttpclient.h"
#include <QMap>


class OAuth2ErrorCode : public QMap<QString, int>
{
public:
OAuth2ErrorCode() : QMap<QString, int>()
{
insert(QString("invalid_request"), TOAuth2Client::InvalidRequest);
insert(QString("invalid_client"), TOAuth2Client::InvalidClient);
insert(QString("invalid_grant"), TOAuth2Client::InvalidGrant);
insert(QString("unauthorized_client"), TOAuth2Client::UnauthorizedClient);
insert(QString("unsupported_grant_type"), TOAuth2Client::UnsupportedGrantType);
insert(QString("access_denied"), TOAuth2Client::AccessDenied);
insert(QString("unsupported_response_type"), TOAuth2Client::UnsupportedResponseType);
insert(QString("invalid_scope"), TOAuth2Client::InvalidScope);
insert(QString("server_error"), TOAuth2Client::ServerError);
insert(QString("temporarily_unavailable"), TOAuth2Client::TemporarilyUnavailable);
}
};
Q_GLOBAL_STATIC(OAuth2ErrorCode, oauth2ErrorCode);


TOAuth2Client::TOAuth2Client(const QString &clientId, const QString &clientSecret) :
_clientId(clientId),
_clientSecret(clientSecret)
{ }


bool TOAuth2Client::requestAccessToken(const QUrl &authorizeUrl, const QString &code, const QStringList &scopes, const QUrl &redirect, int msecs)
{
_authorizeUrl = authorizeUrl;
_code = code;
_scopes = scopes;
_redirect = redirect;

THttpClient client;
QUrl url = authorizeUrl;
QString querystr;

querystr = "client_id=" + _clientId;
querystr += "&scope=" + scopes.join(" ");
querystr += "&redirect_uri=" + redirect.toString(QUrl::None);
querystr += "&response_type=code";
url.setQuery(querystr);
tInfo() << "query:" << url.toEncoded();

auto *reply = client.get(url, msecs);
_networkError = reply->error();
auto location = reply->rawHeader("Location");
if (location.isEmpty()) {
// error
return false;
}
tInfo() << location;
return true;
}
67 changes: 67 additions & 0 deletions src/toauth2client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef TOAUTH2CLIENT_H
#define TOAUTH2CLIENT_H

#include <TGlobal>
#include <QStringList>
#include <QUrl>
#include <QNetworkReply>


// class T_CORE_EXPORT TOAuth2AccessToken
// {
// public:
// QString token;
// int expires {0};
// QString refreshToken;
// QStringList scopes;

// TOAuth2AccessToken() {}
// TOAuth2AccessToken(const TOAuth2AccessToken &other) = default;
// TOAuth2AccessToken &operator=(const TOAuth2AccessToken &other) = default;
// };


class T_CORE_EXPORT TOAuth2Client
{
public:
enum Error {
NoError = 0,
InvalidRequest,
InvalidClient,
InvalidGrant,
UnauthorizedClient,
UnsupportedGrantType,
AccessDenied,
UnsupportedResponseType,
InvalidScope,
ServerError,
TemporarilyUnavailable,
UnknownError,
};

TOAuth2Client(const QString &clientId, const QString &clientSecret);
TOAuth2Client(const TOAuth2Client &other) = default;
TOAuth2Client &operator=(const TOAuth2Client &other) = default;

bool requestAccessToken(const QUrl &authorizeUrl, const QString &code, const QStringList &scopes, const QUrl &redirect, int msecs);
QString accessToken() const { return _accessToken; }
int tokenExpires() const { return _expires; }
QStringList scopes() const { return _scopes; }
Error errorCode() { return _error; }
QNetworkReply::NetworkError networkError() const { return _networkError; }

private:
QString _clientId;
QString _clientSecret;
QUrl _authorizeUrl;
QStringList _scopes;
QString _code;
QUrl _redirect;
QString _accessToken;
int _expires {0};
QString _refreshToken;
Error _error {NoError};
QNetworkReply::NetworkError _networkError {QNetworkReply::NoError};
};

#endif // TOAUTH2CLIENT_H

0 comments on commit d83a639

Please sign in to comment.