3 #include <QNetworkRequest>
8 google_oauth2::google_oauth2(QString discovery_uri, QString client_id, QString client_secret, QString redirect_uri, QString tokeninfo_uri, QString hosted_domain,
bool debug)
11 , _client_id(client_id)
12 , _client_secret(client_secret)
13 , _redirect_uri(redirect_uri)
14 , _tokeninfo_uri(tokeninfo_uri)
15 , _hosted_domain(hosted_domain)
16 , _discovery_uri(discovery_uri)
23 printf(
"%s\nclient_id: %s\nclient_secret: %s\nredirect_uri: %s\ntokeninfo_uri: %s\n",
25 qUtf8Printable(client_id),
26 qUtf8Printable(client_secret),
27 qUtf8Printable(redirect_uri),
28 qUtf8Printable(tokeninfo_uri)
42 printf(
"OAUTH READ: %s\n",qUtf8Printable(r->
_data.toJson()));
52 QJsonObject d = r->
_data.object();
54 bool auth = r->
_path ==
"";
58 QString user = r->
_path;
60 QMutexLocker l(&
_lock);
71 r->
_reason =
"User data is unavailable for '"+user+
"'";
78 QString authcode = d[
"authtokenonce"].toString(
"");
81 printf(
"authcode:'%s' auth: %d\n",qUtf8Printable(authcode),auth);
83 if (authcode.isEmpty())
85 r->
_reason =
"No single use authcode present.";
175 for (
int i=0;i<errors.size();i++)
196 printf(
"Discovery response: %s\n",qUtf8Printable(data));
198 QJsonParseError error;
199 QJsonDocument doc = QJsonDocument::fromJson(data,&error);
200 if (error.error == QJsonParseError::NoError)
219 VFS::ERROR( QString(
"Failed to parse discovery response: '%1' : %2\n%3").arg(
_discovery_reply->url().toString()).arg(error.errorString()).arg(QString(data)), 0,
className() );
245 if (
_auths.contains(auth))
251 QJsonObject d = r->
_data.object();
254 d[
"uidnumber"] = auth->
_id;
260 r->
_data.setObject(d);
272 VFS::ERROR(
"Received an auth response but can't associate with a VFS_request.");
281 , _authcode(authcode)
283 , _authenticated(false)
284 , _reason(
"Unspecified reason")
302 params.addQueryItem(
"grant_type",
"authorization_code");
308 request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
310 request.setAttribute(QNetworkRequest::FollowRedirectsAttribute,
true);
314 connect(
_reply, SIGNAL(finished()),
318 this, SLOT(
sslErrors(QList<QSslError>)) );
327 printf(
"REDIRECTED: %s\n",qUtf8Printable(url.toString()));
332 for (
int i=0;i<errors.size();i++)
341 QByteArray data =
_reply->readAll();
346 printf(
"Authentication Request completed: %s\n", qUtf8Printable(
_reply->url().toString(QUrl::FullyEncoded)));
347 printf(
"Status: %d\n",
_reply->error());
349 printf(
"\nHeaders:\n");
350 const QList<QNetworkReply::RawHeaderPair> headerPairs =
_reply->rawHeaderPairs();
351 foreach (QNetworkReply::RawHeaderPair h,headerPairs)
352 printf(
" %s ::: %s\n", qUtf8Printable(h.first), qUtf8Printable(h.second) );
354 printf(
"\nAuthentication response: %s\n",qUtf8Printable(data));
373 QJsonParseError error;
374 QJsonDocument doc = QJsonDocument::fromJson(data,&error);
376 if (error.error == QJsonParseError::NoError)
390 QJsonObject d = doc.object();
396 request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
398 request.setAttribute(QNetworkRequest::FollowRedirectsAttribute,
true);
401 params.addQueryItem(
"id_token",
_id_token);
404 connect(
_reply, SIGNAL(finished()),
408 this, SLOT(
sslErrors(QList<QSslError>)) );
418 _reason = QString(
"Failed to parse authResponse: '%1' : %2\n%3").arg(
_reply->url().toString()).arg(error.errorString()).arg(QString(data));
430 QByteArray data =
_reply->readAll();
434 printf(
"Tokeninfo Request completed: %s\n", qUtf8Printable(
_reply->url().toString(QUrl::FullyEncoded)));
435 printf(
"Status: %d\n",
_reply->error());
437 printf(
"\nHeaders:\n");
438 const QList<QNetworkReply::RawHeaderPair> headerPairs =
_reply->rawHeaderPairs();
439 foreach (QNetworkReply::RawHeaderPair h,headerPairs)
440 printf(
" %s ::: %s\n", qUtf8Printable(h.first), qUtf8Printable(h.second) );
442 printf(
"\nAuthentication response: %s\n",qUtf8Printable(data));
453 QJsonParseError error;
454 QJsonDocument doc = QJsonDocument::fromJson(data,&error);
456 if (error.error == QJsonParseError::NoError)
458 QJsonObject o = doc.object();
465 _id = o[
"sub"].toString();
473 _reason =
"Tokeninfo response did not contain the correct client_id";
480 _reason = QString(
"Failed to parse tokeninfo response: '%1' : %2\n%3").arg(
_reply->url().toString()).arg(error.errorString()).arg(QString(data));
The base class for authenticating users.
virtual void issueResponse(VFS_request *t)
Once a request has been completed, issue a response.
QString className()
Return the class name of a node.
QMutex _lock
A recursive mutex that is local to this node.
void finished(bool andDelete=false)
Emitted if a thread fails to create its node, or a node is rm()'d, or any other reason a node has com...
The base class for all requests between nodes.
QString _reason
if something (probably bad) happened, this is the reason
QString _path
the target path remnant... the remaining path element once the request has found its target
bool _isCallback
whether or not to issue a response (IE, another request is chained to this request,...
bool _success
if the request was successfully completed
QJsonDocument _data
the request payload
static VFS * root()
Return the root node of the VFS filesystem.
static void LOG(QString message, int level=0, QString user="server")
Send a message to the VFS::_messages VFS_stream.
static void ERROR(QString message, int level=0, QString user="server")
Send a message to the VFS::_errors VFS_stream.
void redirected(const QUrl &url)
virtual void sslErrors(const QList< QSslError > &errors)
void authResult(google_oauth2_auth *auth)
google_oauth2_auth(QString authcode, google_oauth2 *auth)
QJsonObject _discovery_data
QMap< QString, QJsonObject > _userCache
virtual void read(VFS_request *r)
Return the data contents of this node, or if it's a container call ls()
friend class google_oauth2_auth
virtual void discoverySslErrors(const QList< QSslError > &errors)
QMap< google_oauth2_auth *, VFS_request * > _auths
Q_INVOKABLE google_oauth2(QString discovery_uri, QString client_id, QString client_secret, QString redirect_uri, QString tokeninfo_uri, QString hosted_domain, bool debug=false)
void authResult(google_oauth2_auth *auth)
virtual void ls(VFS_request *r)
List the contents of this node.
virtual QString reportDetails()
Additional details for a generated report.
QNetworkAccessManager _manager
QNetworkReply * _discovery_reply