forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2f017b
commit d83a639
Showing
6 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "toauth2client.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "../src/toauth2client.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |