qoauth2implicitgrantflow.cpp 4.13 KB
Newer Older
Jesus Fernandez's avatar
Jesus Fernandez committed
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
/****************************************************************************
**
** 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$
**
****************************************************************************/

#ifndef QT_NO_HTTP

#include <qoauth2implicitgrantflow.h>
#include <qoauthhttpserverreplyhandler.h>

#include <private/qoauth2implicitgrantflow_p.h>

#include <QtCore/qurl.h>
#include <QtCore/qstring.h>
Jesus Fernandez's avatar
Jesus Fernandez committed
49
#include <QtCore/qurlquery.h>
Jesus Fernandez's avatar
Jesus Fernandez committed
50 51 52 53 54

#include <QtNetwork/qnetworkaccessmanager.h>

QT_BEGIN_NAMESPACE

Jesus Fernandez's avatar
Jesus Fernandez committed
55 56
QOAuth2ImplicitGrantFlowPrivate::QOAuth2ImplicitGrantFlowPrivate(QNetworkAccessManager *manager) :
    QAbstractOAuth2Private(manager)
Jesus Fernandez's avatar
Jesus Fernandez committed
57 58 59 60 61
{}

QOAuth2ImplicitGrantFlowPrivate::~QOAuth2ImplicitGrantFlowPrivate()
{}

Jesus Fernandez's avatar
Jesus Fernandez committed
62 63
QOAuth2ImplicitGrantFlow::QOAuth2ImplicitGrantFlow(QObject *parent) :
    QAbstractOAuth2(*new QOAuth2ImplicitGrantFlowPrivate(nullptr), parent)
Jesus Fernandez's avatar
Jesus Fernandez committed
64 65
{}

Jesus Fernandez's avatar
Jesus Fernandez committed
66
QOAuth2ImplicitGrantFlow::QOAuth2ImplicitGrantFlow(QNetworkAccessManager *manager,
Jesus Fernandez's avatar
Jesus Fernandez committed
67 68
                                                   QObject *parent) :
    QAbstractOAuth2(*new QOAuth2ImplicitGrantFlowPrivate(manager), parent)
Jesus Fernandez's avatar
Jesus Fernandez committed
69
{}
Jesus Fernandez's avatar
Jesus Fernandez committed
70

Jesus Fernandez's avatar
Jesus Fernandez committed
71
QOAuth2ImplicitGrantFlow::QOAuth2ImplicitGrantFlow(const QString &clientIdentifier,
Jesus Fernandez's avatar
Jesus Fernandez committed
72
                                                   QNetworkAccessManager *manager,
Jesus Fernandez's avatar
Jesus Fernandez committed
73 74
                                                   QObject *parent) :
    QAbstractOAuth2(*new QOAuth2ImplicitGrantFlowPrivate(manager), parent)
Jesus Fernandez's avatar
Jesus Fernandez committed
75 76
{
    Q_D(QOAuth2ImplicitGrantFlow);
Jesus Fernandez's avatar
Jesus Fernandez committed
77
    d->clientCredentials.first = clientIdentifier;
Jesus Fernandez's avatar
Jesus Fernandez committed
78 79 80 81 82
}

QOAuth2ImplicitGrantFlow::~QOAuth2ImplicitGrantFlow()
{}

Jesus Fernandez's avatar
Jesus Fernandez committed
83
QString QOAuth2ImplicitGrantFlow::responseType() const
Jesus Fernandez's avatar
Jesus Fernandez committed
84 85 86 87
{
    return QStringLiteral("token");
}

Jesus Fernandez's avatar
Jesus Fernandez committed
88
void QOAuth2ImplicitGrantFlow::grant()
Jesus Fernandez's avatar
Jesus Fernandez committed
89
{
Jesus Fernandez's avatar
Jesus Fernandez committed
90 91
    const QString state = QAbstractOAuth2Private::generateRandomState();
    const QUrl url = authorizationRequestUrl(state);
Jesus Fernandez's avatar
Jesus Fernandez committed
92
    Q_EMIT authorizeWithBrowser(url);
Jesus Fernandez's avatar
Jesus Fernandez committed
93 94
}

Jesus Fernandez's avatar
Jesus Fernandez committed
95
QUrl QOAuth2ImplicitGrantFlow::authorizationRequestUrl(const QString &state) const
Jesus Fernandez's avatar
Jesus Fernandez committed
96
{
Jesus Fernandez's avatar
Jesus Fernandez committed
97 98 99 100
    Q_D(const QOAuth2ImplicitGrantFlow);
    typedef QAbstractOAuth2Private::OAuth2KeyString Key;

    QVariantMap parameters;
Jesus Fernandez's avatar
Jesus Fernandez committed
101
    QUrl url(d->authorizationUrl);
Jesus Fernandez's avatar
Jesus Fernandez committed
102
    parameters.insert(Key::responseType, responseType());
Jesus Fernandez's avatar
Jesus Fernandez committed
103
    parameters.insert(Key::clientIdentifier, d->clientCredentials.first);
Jesus Fernandez's avatar
Jesus Fernandez committed
104 105 106 107 108
    parameters.insert(Key::redirectUri, callback());
    parameters.insert(Key::scope, d->scope);
    parameters.insert(Key::state, state);
    url.setQuery(QAbstractOAuth2Private::createQuery(parameters));
    return url;
Jesus Fernandez's avatar
Jesus Fernandez committed
109 110 111 112 113
}

QT_END_NAMESPACE

#endif // QT_NO_HTTP