forked from pipacs/o2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho2.h
170 lines (134 loc) · 5.25 KB
/
o2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef O2_H
#define O2_H
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QPair>
#include "o0baseauth.h"
#include "o2reply.h"
#include "o0abstractstore.h"
class O2ReplyServer;
/// Simple OAuth2 authenticator.
class O2: public O0BaseAuth {
Q_OBJECT
Q_ENUMS(GrantFlow)
public:
/// Authorization flow types.
enum GrantFlow {
GrantFlowAuthorizationCode, ///< @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.1
GrantFlowImplicit, ///< @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.2
GrantFlowResourceOwnerPasswordCredentials,
};
/// Authorization flow.
Q_PROPERTY(GrantFlow grantFlow READ grantFlow WRITE setGrantFlow NOTIFY grantFlowChanged)
GrantFlow grantFlow();
void setGrantFlow(GrantFlow value);
/// Resource owner username.
/// O2 instances with the same (username, password) share the same "linked" and "token" properties.
Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged)
QString username();
void setUsername(const QString &value);
/// Resource owner password.
/// O2 instances with the same (username, password) share the same "linked" and "token" properties.
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
QString password();
void setPassword(const QString &value);
/// Scope of authentication.
Q_PROPERTY(QString scope READ scope WRITE setScope NOTIFY scopeChanged)
QString scope();
void setScope(const QString &value);
/// Localhost policy. By default it's value is http://127.0.0.1:%1/, however some services may
/// require the use of http://localhost:%1/ or any other value.
Q_PROPERTY(QString localhostPolicy READ localhostPolicy WRITE setLocalhostPolicy)
QString localhostPolicy() const;
void setLocalhostPolicy(const QString &value);
/// API key.
Q_PROPERTY(QString apiKey READ apiKey WRITE setApiKey)
QString apiKey();
void setApiKey(const QString &value);
/// Page content on local host after successful oauth.
/// Provide it in case you do not want to close the browser, but display something
Q_PROPERTY(QByteArray replyContent READ replyContent WRITE setReplyContent)
QByteArray replyContent();
void setReplyContent(const QByteArray &value);
/// Allow ignoring SSL errors?
/// E.g. SurveyMonkey fails on Mac due to SSL error. Ignoring the error circumvents the problem
Q_PROPERTY(bool ignoreSslErrors READ ignoreSslErrors WRITE setIgnoreSslErrors)
bool ignoreSslErrors();
void setIgnoreSslErrors(bool ignoreSslErrors);
/// Request URL.
Q_PROPERTY(QString requestUrl READ requestUrl WRITE setRequestUrl NOTIFY requestUrlChanged)
QString requestUrl();
void setRequestUrl(const QString &value);
/// Token request URL.
Q_PROPERTY(QString tokenUrl READ tokenUrl WRITE setTokenUrl NOTIFY tokenUrlChanged)
QString tokenUrl();
void setTokenUrl(const QString &value);
/// Token refresh URL.
Q_PROPERTY(QString refreshTokenUrl READ refreshTokenUrl WRITE setRefreshTokenUrl NOTIFY refreshTokenUrlChanged)
QString refreshTokenUrl();
void setRefreshTokenUrl(const QString &value);
public:
/// Constructor.
/// @param parent Parent object.
explicit O2(QObject *parent = 0);
/// Get authentication code.
QString code();
/// Get refresh token.
QString refreshToken();
/// Get token expiration time (seconds from Epoch).
int expires();
public Q_SLOTS:
/// Authenticate.
Q_INVOKABLE virtual void link();
/// De-authenticate.
Q_INVOKABLE virtual void unlink();
/// Refresh token.
Q_INVOKABLE void refresh();
Q_SIGNALS:
/// Emitted when a token refresh has been completed or failed.
void refreshFinished(QNetworkReply::NetworkError error);
// Property change signals
void grantFlowChanged();
void scopeChanged();
void usernameChanged();
void passwordChanged();
void requestUrlChanged();
void refreshTokenUrlChanged();
void tokenUrlChanged();
protected Q_SLOTS:
/// Handle verification response.
virtual void onVerificationReceived(QMap<QString, QString>);
/// Handle completion of a token request.
virtual void onTokenReplyFinished();
/// Handle failure of a token request.
virtual void onTokenReplyError(QNetworkReply::NetworkError error);
/// Handle completion of a refresh request.
virtual void onRefreshFinished();
/// Handle failure of a refresh request.
virtual void onRefreshError(QNetworkReply::NetworkError error);
protected:
/// Build HTTP request body.
QByteArray buildRequestBody(const QMap<QString, QString> ¶meters);
/// Set authentication code.
void setCode(const QString &v);
/// Set refresh token.
void setRefreshToken(const QString &v);
/// Set token expiration time.
void setExpires(int v);
protected:
QString username_;
QString password_;
QUrl requestUrl_;
QUrl tokenUrl_;
QUrl refreshTokenUrl_;
QString scope_;
QString code_;
QString localhostPolicy_;
QString apiKey_;
QNetworkAccessManager *manager_;
O2ReplyServer *replyServer_;
O2ReplyList timedReplies_;
GrantFlow grantFlow_;
};
#endif // O2_H