@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QDateTime>
|
||||
#include <QMap>
|
||||
#include <QString>
|
||||
#include <QVariantMap>
|
||||
|
||||
namespace Katabasis {
|
||||
@ -11,17 +11,13 @@ enum class Activity {
|
||||
LoggingIn,
|
||||
LoggingOut,
|
||||
Refreshing,
|
||||
FailedSoft, //!< soft failure. this generally means the user auth details haven't been invalidated
|
||||
FailedHard, //!< hard failure. auth is invalid
|
||||
FailedGone, //!< hard failure. auth is invalid, and the account no longer exists
|
||||
FailedSoft, //!< soft failure. this generally means the user auth details haven't been invalidated
|
||||
FailedHard, //!< hard failure. auth is invalid
|
||||
FailedGone, //!< hard failure. auth is invalid, and the account no longer exists
|
||||
Succeeded
|
||||
};
|
||||
|
||||
enum class Validity {
|
||||
None,
|
||||
Assumed,
|
||||
Certain
|
||||
};
|
||||
enum class Validity { None, Assumed, Certain };
|
||||
|
||||
struct Token {
|
||||
QDateTime issueInstant;
|
||||
@ -34,4 +30,4 @@ struct Token {
|
||||
bool persistent = true;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Katabasis
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
#include <QLoggingCategory>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QPair>
|
||||
|
||||
#include "Bits.h"
|
||||
#include "Reply.h"
|
||||
#include "RequestParameter.h"
|
||||
#include "Bits.h"
|
||||
|
||||
namespace Katabasis {
|
||||
|
||||
@ -16,14 +16,12 @@ class ReplyServer;
|
||||
class PollServer;
|
||||
|
||||
/// Simple OAuth2 Device Flow authenticator.
|
||||
class DeviceFlow: public QObject
|
||||
{
|
||||
class DeviceFlow : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
public:
|
||||
Q_ENUMS(GrantFlow)
|
||||
|
||||
public:
|
||||
|
||||
public:
|
||||
struct Options {
|
||||
QString userAgent = QStringLiteral("Katabasis/1.0");
|
||||
QString responseType = QStringLiteral("code");
|
||||
@ -34,7 +32,7 @@ public:
|
||||
QUrl accessTokenUrl;
|
||||
};
|
||||
|
||||
public:
|
||||
public:
|
||||
/// Are we authenticated?
|
||||
bool linked();
|
||||
|
||||
@ -44,21 +42,21 @@ public:
|
||||
/// Provider-specific extra tokens, available after a successful authentication
|
||||
QVariantMap extraTokens();
|
||||
|
||||
public:
|
||||
public:
|
||||
// TODO: put in `Options`
|
||||
/// User-defined extra parameters to append to request URL
|
||||
QVariantMap extraRequestParams();
|
||||
void setExtraRequestParams(const QVariantMap &value);
|
||||
void setExtraRequestParams(const QVariantMap& value);
|
||||
|
||||
// TODO: split up the class into multiple, each implementing one OAuth2 flow
|
||||
/// Grant type (if non-standard)
|
||||
QString grantType();
|
||||
void setGrantType(const QString &value);
|
||||
void setGrantType(const QString& value);
|
||||
|
||||
public:
|
||||
public:
|
||||
/// Constructor.
|
||||
/// @param parent Parent object.
|
||||
explicit DeviceFlow(Options & opts, Token & token, QObject *parent = 0, QNetworkAccessManager *manager = 0);
|
||||
explicit DeviceFlow(Options& opts, Token& token, QObject* parent = 0, QNetworkAccessManager* manager = 0);
|
||||
|
||||
/// Get refresh token.
|
||||
QString refreshToken();
|
||||
@ -66,7 +64,7 @@ public:
|
||||
/// Get token expiration time
|
||||
QDateTime expires();
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
/// Authenticate.
|
||||
void login();
|
||||
|
||||
@ -79,24 +77,24 @@ public slots:
|
||||
/// Handle situation where reply server has opted to close its connection
|
||||
void serverHasClosed(bool paramsfound = false);
|
||||
|
||||
signals:
|
||||
signals:
|
||||
/// Emitted when client needs to open a web browser window, with the given URL.
|
||||
void openBrowser(const QUrl &url);
|
||||
void openBrowser(const QUrl& url);
|
||||
|
||||
/// Emitted when client can close the browser window.
|
||||
void closeBrowser();
|
||||
|
||||
/// Emitted when client needs to show a verification uri and user code
|
||||
void showVerificationUriAndCode(const QUrl &uri, const QString &code, int expiresIn);
|
||||
void showVerificationUriAndCode(const QUrl& uri, const QString& code, int expiresIn);
|
||||
|
||||
/// Emitted when the internal state changes
|
||||
void activityChanged(Activity activity);
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
/// Handle verification response.
|
||||
void onVerificationReceived(QMap<QString, QString>);
|
||||
|
||||
protected slots:
|
||||
protected slots:
|
||||
/// Handle completion of a Device Authorization Request
|
||||
void onDeviceAuthReplyFinished();
|
||||
|
||||
@ -104,20 +102,20 @@ protected slots:
|
||||
void onRefreshFinished();
|
||||
|
||||
/// Handle failure of a refresh request.
|
||||
void onRefreshError(QNetworkReply::NetworkError error, QNetworkReply *reply);
|
||||
void onRefreshError(QNetworkReply::NetworkError error, QNetworkReply* reply);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
/// Set refresh token.
|
||||
void setRefreshToken(const QString &v);
|
||||
void setRefreshToken(const QString& v);
|
||||
|
||||
/// Set token expiration time.
|
||||
void setExpires(QDateTime v);
|
||||
|
||||
/// Start polling authorization server
|
||||
void startPollServer(const QVariantMap ¶ms, int expiresIn);
|
||||
void startPollServer(const QVariantMap& params, int expiresIn);
|
||||
|
||||
/// Set authentication token.
|
||||
void setToken(const QString &v);
|
||||
void setToken(const QString& v);
|
||||
|
||||
/// Set the linked state
|
||||
void setLinked(bool v);
|
||||
@ -126,26 +124,26 @@ protected:
|
||||
void setExtraTokens(QVariantMap extraTokens);
|
||||
|
||||
/// Set local poll server
|
||||
void setPollServer(PollServer *server);
|
||||
void setPollServer(PollServer* server);
|
||||
|
||||
PollServer * pollServer() const;
|
||||
PollServer* pollServer() const;
|
||||
|
||||
void updateActivity(Activity activity);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
Options options_;
|
||||
|
||||
QVariantMap extraReqParams_;
|
||||
QNetworkAccessManager *manager_ = nullptr;
|
||||
QNetworkAccessManager* manager_ = nullptr;
|
||||
ReplyList timedReplies_;
|
||||
QString grantType_;
|
||||
|
||||
protected:
|
||||
Token &token_;
|
||||
protected:
|
||||
Token& token_;
|
||||
|
||||
private:
|
||||
PollServer *pollServer_ = nullptr;
|
||||
private:
|
||||
PollServer* pollServer_ = nullptr;
|
||||
Activity activity_ = Activity::Idle;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Katabasis
|
||||
|
@ -45,7 +45,7 @@ const char OAUTH2_EXPIRES_IN[] = "expires_in";
|
||||
const char OAUTH2_DEVICE_CODE[] = "device_code";
|
||||
const char OAUTH2_USER_CODE[] = "user_code";
|
||||
const char OAUTH2_VERIFICATION_URI[] = "verification_uri";
|
||||
const char OAUTH2_VERIFICATION_URL[] = "verification_url"; // Google sign-in
|
||||
const char OAUTH2_VERIFICATION_URL[] = "verification_url"; // Google sign-in
|
||||
const char OAUTH2_VERIFICATION_URI_COMPLETE[] = "verification_uri_complete";
|
||||
const char OAUTH2_INTERVAL[] = "interval";
|
||||
|
||||
@ -56,4 +56,4 @@ const char AUTHORIZATION_CODE[] = "authorization_code";
|
||||
const char HTTP_HTTP_HEADER[] = "HTTP";
|
||||
const char HTTP_AUTHORIZATION_HEADER[] = "Authorization";
|
||||
|
||||
}
|
||||
} // namespace Katabasis
|
||||
|
@ -12,32 +12,35 @@ class QNetworkAccessManager;
|
||||
namespace Katabasis {
|
||||
|
||||
/// Poll an authorization server for token
|
||||
class PollServer : public QObject
|
||||
{
|
||||
class PollServer : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PollServer(QNetworkAccessManager * manager, const QNetworkRequest &request, const QByteArray & payload, int expiresIn, QObject *parent = 0);
|
||||
public:
|
||||
explicit PollServer(QNetworkAccessManager* manager,
|
||||
const QNetworkRequest& request,
|
||||
const QByteArray& payload,
|
||||
int expiresIn,
|
||||
QObject* parent = 0);
|
||||
|
||||
/// Seconds to wait between polling requests
|
||||
Q_PROPERTY(int interval READ interval WRITE setInterval)
|
||||
int interval() const;
|
||||
void setInterval(int interval);
|
||||
|
||||
signals:
|
||||
signals:
|
||||
void verificationReceived(QMap<QString, QString>);
|
||||
void serverClosed(bool); // whether it has found parameters
|
||||
void serverClosed(bool); // whether it has found parameters
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
void startPolling();
|
||||
|
||||
protected slots:
|
||||
protected slots:
|
||||
void onPollTimeout();
|
||||
void onExpiration();
|
||||
void onReplyFinished();
|
||||
|
||||
protected:
|
||||
QNetworkAccessManager *manager_;
|
||||
protected:
|
||||
QNetworkAccessManager* manager_;
|
||||
const QNetworkRequest request_;
|
||||
const QByteArray payload_;
|
||||
const int expiresIn_;
|
||||
@ -45,4 +48,4 @@ protected:
|
||||
QTimer pollTimer;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Katabasis
|
||||
|
@ -1,38 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <QList>
|
||||
#include <QTimer>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QByteArray>
|
||||
#include <QList>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QTimer>
|
||||
|
||||
namespace Katabasis {
|
||||
|
||||
constexpr int defaultTimeout = 30 * 1000;
|
||||
|
||||
/// A network request/reply pair that can time out.
|
||||
class Reply: public QTimer {
|
||||
class Reply : public QTimer {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Reply(QNetworkReply *reply, int timeOut = defaultTimeout, QObject *parent = 0);
|
||||
public:
|
||||
Reply(QNetworkReply* reply, int timeOut = defaultTimeout, QObject* parent = 0);
|
||||
|
||||
signals:
|
||||
signals:
|
||||
void error(QNetworkReply::NetworkError);
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
/// When time out occurs, the QNetworkReply's error() signal is triggered.
|
||||
void onTimeOut();
|
||||
|
||||
public:
|
||||
QNetworkReply *reply;
|
||||
public:
|
||||
QNetworkReply* reply;
|
||||
bool timedOut = false;
|
||||
};
|
||||
|
||||
/// List of O2Replies.
|
||||
class ReplyList {
|
||||
public:
|
||||
public:
|
||||
ReplyList() { ignoreSslErrors_ = false; }
|
||||
|
||||
/// Destructor.
|
||||
@ -40,24 +40,24 @@ public:
|
||||
virtual ~ReplyList();
|
||||
|
||||
/// Create a new O2Reply from a QNetworkReply, and add it to this list.
|
||||
void add(QNetworkReply *reply, int timeOut = defaultTimeout);
|
||||
void add(QNetworkReply* reply, int timeOut = defaultTimeout);
|
||||
|
||||
/// Add an O2Reply to the list, while taking ownership of it.
|
||||
void add(Reply *reply);
|
||||
void add(Reply* reply);
|
||||
|
||||
/// Remove item from the list that corresponds to a QNetworkReply.
|
||||
void remove(QNetworkReply *reply);
|
||||
void remove(QNetworkReply* reply);
|
||||
|
||||
/// Find an O2Reply in the list, corresponding to a QNetworkReply.
|
||||
/// @return Matching O2Reply or NULL.
|
||||
Reply *find(QNetworkReply *reply);
|
||||
Reply* find(QNetworkReply* reply);
|
||||
|
||||
bool ignoreSslErrors();
|
||||
void setIgnoreSslErrors(bool ignoreSslErrors);
|
||||
|
||||
protected:
|
||||
QList<Reply *> replies_;
|
||||
protected:
|
||||
QList<Reply*> replies_;
|
||||
bool ignoreSslErrors_;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Katabasis
|
||||
|
@ -4,12 +4,10 @@ namespace Katabasis {
|
||||
|
||||
/// Request parameter (name-value pair) participating in authentication.
|
||||
struct RequestParameter {
|
||||
RequestParameter(const QByteArray &n, const QByteArray &v): name(n), value(v) {}
|
||||
bool operator <(const RequestParameter &other) const {
|
||||
return (name == other.name)? (value < other.value): (name < other.name);
|
||||
}
|
||||
RequestParameter(const QByteArray& n, const QByteArray& v) : name(n), value(v) {}
|
||||
bool operator<(const RequestParameter& other) const { return (name == other.name) ? (value < other.value) : (name < other.name); }
|
||||
QByteArray name;
|
||||
QByteArray value;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Katabasis
|
||||
|
Reference in New Issue
Block a user