Commit 1243c433 authored by Jesus Fernandez's avatar Jesus Fernandez

WIP

parent b8461709
......@@ -61,7 +61,6 @@ QOAuth2ImplicitGrantFlowPrivate::~QOAuth2ImplicitGrantFlowPrivate()
void QOAuth2ImplicitGrantFlowPrivate::_q_handleCallback(const QVariantMap &data)
{
Q_Q(QOAuth2ImplicitGrantFlow);
typedef QAbstractOAuth2Private::OAuth2KeyString Key;
if (status != QAbstractOAuth::Status::NotAuthenticated) {
......@@ -72,12 +71,12 @@ void QOAuth2ImplicitGrantFlowPrivate::_q_handleCallback(const QVariantMap &data)
Q_ASSERT(!state.isEmpty());
const QString error = data.value(Key::error).toString();
const QString token = data.value(Key::accessToken).toString();
const QString tokenType = data.value(Key::tokenType).toString();
bool ok;
int expiresIn = data.value(Key::expiresIn).toInt(&ok);
if (!ok)
expiresIn = -1;
// const QString token = data.value(Key::accessToken).toString();
// const QString tokenType = data.value(Key::tokenType).toString();
// bool ok;
// int expiresIn = data.value(Key::expiresIn).toInt(&ok);
// if (!ok)
// expiresIn = -1;
const QString receivedState = data.value(Key::state).toString();
if (!error.isEmpty()) {
const QString uri = data.value(Key::errorUri).toString();
......@@ -85,27 +84,20 @@ void QOAuth2ImplicitGrantFlowPrivate::_q_handleCallback(const QVariantMap &data)
qCritical("QOAuth2ImplicitGrantFlow: AuthenticationError: %s(%s): %s",
qPrintable(error), qPrintable(uri), qPrintable(description));
return;
} else if (token.isEmpty()) {
qCritical("QOAuth2ImplicitGrantFlow: AuthenticationError: Token not received");
return;
} else if (tokenType != QStringLiteral("bearer")) {
qCritical("QOAuth2ImplicitGrantFlow: Invalid Token type");
return;
} else if (receivedState.isEmpty()) {
qCritical("QOAuth2ImplicitGrantFlow: State not received");
return;
} else if (state != receivedState) {
qCritical("QOAuth2ImplicitGrantFlow: State mismatch");
return;
} else if (!receivedState.isEmpty()) {
if (state != receivedState) {
qCritical("QOAuth2ImplicitGrantFlow: State mismatch");
return;
}
}
setStatus(QAbstractOAuth::Status::TemporaryCredentialsReceived);
q->setToken(token);
expiresAt = expiresIn > 0 ? QDateTime::currentDateTime().addSecs(expiresIn) : QDateTime();
Q_EMIT q->expirationAtChanged(expiresAt);
// q->setToken(token);
// expiresAt = expiresIn > 0 ? QDateTime::currentDateTime().addSecs(expiresIn) : QDateTime();
// Q_EMIT q->expirationAtChanged(expiresAt);
setStatus(QAbstractOAuth::Status::Granted);
// setStatus(QAbstractOAuth::Status::Granted);
}
QOAuth2ImplicitGrantFlow::QOAuth2ImplicitGrantFlow(QObject *parent) :
......
......@@ -56,7 +56,7 @@ public:
explicit QOAuth2ImplicitGrantFlow(QNetworkAccessManager *manager,
QObject *parent = nullptr);
QOAuth2ImplicitGrantFlow(const QString &clientIdentifier,
QNetworkAccessManager *manager,
QNetworkAccessManager *manager = nullptr,
QObject *parent = nullptr);
virtual ~QOAuth2ImplicitGrantFlow();
......
......@@ -86,10 +86,7 @@ void QOAuthHttpServerReplyHandlerPrivate::_q_answerClient()
QTcpSocket *socket = static_cast<QTcpSocket*>(currentSender->sender);
const QByteArray data = socket->readAll();
Q_EMIT q->callbackDataReceived(data);
if (!data.startsWith("GET " CALLBACK_ADDRESS "?")) {
qWarning("QOAuthHttpServerReplyHandlerPrivate::_q_answerClient: Invalid request: %s",
qPrintable(data));
} else {
if (data.startsWith("GET " CALLBACK_ADDRESS "?")) {
QVariantMap receivedData;
const int queryStart = strlen("GET " CALLBACK_ADDRESS "?"),
queryEnd = data.indexOf(" HTTP/1.1");
......
......@@ -5,4 +5,5 @@ SUBDIRS += \
redditclient \
qtgcal \
googlecalendar \
oauthtool
oauthtool \
imgur
unix:!android {
isEmpty(target.path) {
qnx {
target.path = /tmp/$${TARGET}/bin
} else {
target.path = /opt/$${TARGET}/bin
}
export(target.path)
}
INSTALLS += target
}
export(INSTALLS)
QT += gui
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH = $$OUT_PWD/../../qml
include(../../QtOAuth.pri)
# Default rules for deployment.
include(deployment.pri)
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtNetwork module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include <QtCore>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QtOAuth/qoauth2implicitgrantflow.h>
#include <QtOAuth/qoauthhttpserverreplyhandler.h>
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
QCommandLineParser parser;
const QCommandLineOption clientId(QStringList() << "i" << "client-id",
"Specifies the application client id", "client_id");
parser.addOptions({clientId});
parser.process(app);
if (parser.isSet(clientId)) {
QOAuthHttpServerReplyHandler replyHandler(1337);
QOAuth2ImplicitGrantFlow oauth2(parser.value(clientId));
oauth2.setReplyHandler(&replyHandler);
oauth2.setAuthorizationUrl(QUrl("https://api.imgur.com/oauth2/authorize"));
QObject::connect(&oauth2, &QOAuth2ImplicitGrantFlow::authorizeWithBrowser,
[](const QUrl &url) {
qDebug() << url;
});
oauth2.grant();
return app.exec();
} else {
parser.showHelp();
}
return 0;
}
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.0
import Qt.labs.oauth 1.0
import QtWebEngine 1.1
ApplicationWindow {
visible: true
width: 640
height: 480
property bool cacheCredentials: false
property url calendarListUrl: "https://www.googleapis.com/calendar/v3/users/me/calendarList"
property var calendars: []
title: qsTr("Google Calendar Example")
function updateCalendar(calendar) {
var id = calendar.id;
var current = (new Date()).toISOString();
var client = new XMLHttpRequest;
var calendarUrl = "https://www.googleapis.com/calendar/v3/calendars/" + id + "/events";
var parameters = {
"timeMin" : (new Date()).toISOString(),
"maxResults" : 50
}
var url = oauth2.createAuthenticatedUrl(calendarUrl, parameters);
client.onreadystatechange = function() {
if (client.readyState == 4 && client.status == 200) {
var text = client.responseText;
var obj = JSON.parse(text);
for (var i in obj.items) {
function copyAll(source, dest) {
for (var key in source)
dest[key] = source[key];
}
var object = {};
copyAll(calendar, object);
copyAll(obj.items[i], object);
calendarEventsModel.append(object);
}
}
}
client.open("GET", url, true);
client.send();
}
function update() {
calendarEventsModel.clear();
for (var index in calendars)
updateCalendar(calendars[index]);
}
ListModel { id: calendarEventsModel }
ListView {
id: calendarEventsTableView
anchors.fill: parent
model: calendarEventsModel
delegate: Rectangle {
id: rectangle
width: calendarEventsTableView.width
height: 50
color: backgroundColor
RowLayout {
Rectangle {
id: datesRectangle
width: 100
height: rectangle.height
color: "transparent"
Text {
property date startDateTime: {
var date;
if (start.date)
date = new Date(Date.parse(start.date));
else
date = new Date(Date.parse(start.dateTime));
return date;
}
property var endDateTime: {
var date;
if (end.date)
date = new Date(Date.parse(end.date));
else
date = new Date(Date.parse(end.dateTime));
return date;
}
text: startDateTime.toLocaleString(Qt.locale(), Locale.NarrowFormat) +
'\n-\n' + endDateTime.toLocaleString(Qt.locale(), Locale.NarrowFormat)
horizontalAlignment: Text.AlignHCenter
anchors.centerIn: parent
color: foregroundColor
}
}
Rectangle {
width: calendarEventsTableView.width - datesRectangle.width
height: rectangle.height
color: "transparent"
Text {
property string preffix: location ? ' (' + location + ')' : ''
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: parent.right
anchors.margins: { top: 5; left: 5; right: 5; bottom: 5 }
text: summary + preffix
color: foregroundColor
}
}
}
}
}
OAuth2 {
id: oauth2
clientIdentifier: client_id
clientIdentifierSharedKey: client_secret
authorizationUrl: "https://accounts.google.com/o/oauth2/auth"
accessTokenUrl: "https://accounts.google.com/o/oauth2/token"
scope: "https://www.googleapis.com/auth/calendar.readonly"
port: 1965
onAuthorizeWithBrowser: {
webView.url = url;
console.log("Url: " + url);
webView.visible = true;
}
Component.onCompleted: grant();
onGranted: {
webView.visible = false;
var url = oauth2.createAuthenticatedUrl(calendarListUrl);
var client = new XMLHttpRequest;
client.onreadystatechange = function() {
if (client.readyState == 4 && client.status == 200) {
var obj = JSON.parse(client.responseText);
for (var i in obj.items)
calendars.push(obj.items[i]);
update();
}
}
client.open("GET", url, true);
client.send();
}
onFinished: reply.readAll();
}
WebEngineView {
id: webView
visible: false
anchors.centerIn: parent
profile.offTheRecord: cacheCredentials
width: parent.width - 100
height: parent.height - 100
}
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment