summaryrefslogtreecommitdiff
path: root/src/enginio_client/enginioidentity.cpp
blob: 4bbf4fe1be780bded418ab0ab4ad35bfff84ca66 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtEnginio module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://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 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <Enginio/enginioidentity.h>
#include <Enginio/enginiooauth2authentication.h>
#include <Enginio/private/enginioclient_p.h>
#include <Enginio/enginioreply.h>

#include <QtCore/qjsonobject.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qstring.h>
#include <QtNetwork/qnetworkreply.h>

QT_BEGIN_NAMESPACE

/*!
  \class EnginioIdentity
  \since 5.3
  \inmodule enginio-qt
  \ingroup enginio-client
  \brief Represents a user that is authenticated with the backend
  This class is an abstract base class for the different authentication methods and is never used directly.
*/

/*!
  \fn EnginioIdentity::dataChanged()
  \internal
*/

/*!
  \fn EnginioIdentity::aboutToDestroy()
  \internal
*/

class EnginioIdentityPrivate : public QObjectPrivate {};

/*!
    Constructs a new EnginioIdentity with \a dd pointer \a parent as QObject parent.
    \internal
*/
EnginioIdentity::EnginioIdentity(EnginioIdentityPrivate &dd, QObject *parent)
    : QObject(dd, parent)
{}

/*!
    \fn EnginioIdentity::prepareSessionToken(EnginioClientConnectionPrivate *enginio);
    \brief EnginioIdentity::prepareSessionToken is called by enginio client to initialize authentication
    \param enginio client pointer
    \internal
*/
/*!
    \fn EnginioIdentity::removeSessionToken(EnginioClientConnectionPrivate *enginio);
    \brief This method is called by enginio client to finalize de-authentication
    \param enginio client pointer
    \internal
*/

class EnginioUserPassAuthenticationPrivate;
struct DisconnectConnection
{
    EnginioUserPassAuthenticationPrivate *auth;

    DisconnectConnection(EnginioUserPassAuthenticationPrivate *authentication)
        : auth(authentication)
    {}

    inline void operator ()() const;
};

class EnginioUserPassAuthenticationPrivate : public EnginioIdentityPrivate
{
    template<typename T>
    class SessionSetterFunctor
    {
        EnginioClientConnectionPrivate *_enginio;
        QNetworkReply *_reply;
        EnginioUserPassAuthenticationPrivate *_auth;
    public:
        SessionSetterFunctor(EnginioClientConnectionPrivate *enginio, QNetworkReply *reply, EnginioUserPassAuthenticationPrivate *auth)
            : _enginio(enginio)
            , _reply(reply)
            , _auth(auth)
        {}
        void operator ()()
        {
            EnginioReplyState *ereply = _enginio->createReply(_reply);
            if (_reply->error() != QNetworkReply::NoError) {
                emit _enginio->emitSessionAuthenticationError(ereply);
            } else {
                _auth->thisAs<T>()->proccessToken(_enginio, ereply);
                _enginio->emitSessionAuthenticated(ereply);
            }
        }
    };

    QPointer<QNetworkReply> _reply;
    QMetaObject::Connection _replyFinished;
    QMetaObject::Connection _enginioDestroyed;

public:
    QString _user;
    QString _pass;

    ~EnginioUserPassAuthenticationPrivate();

    template<class Derived>
    Derived *thisAs() { return static_cast<Derived*>(this); }

    void cleanupConnections()
    {
        if (_reply) {
            QObject::disconnect(_replyFinished);
            QObject::disconnect(_enginioDestroyed);
            QObject::connect(_reply.data(), &QNetworkReply::finished, _reply.data(), &QNetworkReply::deleteLater);
            _reply = 0;
        }
    }

    template<typename Derived>
    void prepareSessionToken(EnginioClientConnectionPrivate *enginio)
    {
        cleanupConnections();

        _reply = thisAs<Derived>()->makeRequest(enginio);
        enginio->setAuthenticationState(Enginio::Authenticating);
        _replyFinished = QObject::connect(_reply.data(), &QNetworkReply::finished, SessionSetterFunctor<Derived>(enginio, _reply.data(), this));
        _enginioDestroyed = QObject::connect(enginio->q_ptr, &EnginioClient::destroyed, DisconnectConnection(this));
    }

    template<typename Derived>
    void removeSessionToken(EnginioClientConnectionPrivate *enginio)
    {
        cleanupConnections();
        thisAs<Derived>()->cleanupClient(enginio);
        _reply = 0;
        enginio->emitSessionTerminated();
    }
};

class EnginioOAuth2AuthenticationPrivate: public EnginioUserPassAuthenticationPrivate
{
public:
    QNetworkReply *makeRequest(EnginioClientConnectionPrivate *enginio)
    {
        QByteArray data;
        {
            QUrlQuery urlQuery;
            urlQuery.addQueryItem(EnginioString::grant_type, EnginioString::password);
            urlQuery.addQueryItem(EnginioString::username, _user);
            urlQuery.addQueryItem(EnginioString::password, _pass);
            data = urlQuery.query().toUtf8();
        }

        QUrl url(enginio->_serviceUrl);
        url.setPath(EnginioString::v1_auth_oauth2_token);

        QNetworkRequest request(enginio->prepareRequest(url));
        request.setHeader(QNetworkRequest::ContentTypeHeader, EnginioString::Application_x_www_form_urlencoded);
        request.setRawHeader(EnginioString::Accept, EnginioString::Application_json);

        return enginio->networkManager()->post(request, data);
    }

    void proccessToken(EnginioClientConnectionPrivate *enginio, EnginioReplyState *ereply)
    {
        QByteArray header;
        header = EnginioString::Bearer_ + ereply->data()[EnginioString::access_token].toString().toUtf8();
        enginio->_request.setRawHeader(EnginioString::Authorization, header);
    }

    void cleanupClient(EnginioClientConnectionPrivate *enginio)
    {
        enginio->_request.setRawHeader(EnginioString::Authorization, QByteArray());
    }
};

/*!
  \class EnginioOAuth2Authentication
  \since 5.3
  \inmodule enginio-qt
  \ingroup enginio-client
  \brief Represents a user that is authenticated directly by the backend using OAuth2 standard.

  This class can authenticate a user by verifying the user's login and password.
  The user has to exist in the backend already.

  To authenticate an instance of EnginioClient called \a client such code may be used:
  \code
    EnginioOAuth2Authentication identity;
    identity.setUser(_user);
    identity.setPassword(_user);

    client.setIdentity(&identity);
  \endcode

  Setting the identity will trigger an asynchronous request, resulting in EnginioClient::authenticationState()
  changing.

  \sa EnginioClientConnection::authenticationState() EnginioClientConnection::identity() EnginioClient::sessionAuthenticated()
  \sa EnginioClient::sessionAuthenticationError() EnginioClient::sessionTerminated()
*/

/*!
  Constructs a EnginioPasswordOAuth2 instance with \a parent as QObject parent.
*/
EnginioOAuth2Authentication::EnginioOAuth2Authentication(QObject *parent)
    : EnginioIdentity(*new EnginioOAuth2AuthenticationPrivate(), parent)
{
    connect(this, &EnginioOAuth2Authentication::userChanged, this, &EnginioIdentity::dataChanged);
    connect(this, &EnginioOAuth2Authentication::passwordChanged, this, &EnginioIdentity::dataChanged);
}

/*!
  Destructs this EnginioPasswordOAuth2 instance.
*/
EnginioOAuth2Authentication::~EnginioOAuth2Authentication()
{
    emit aboutToDestroy();
}

/*!
  \property EnginioOAuth2Authentication::user
  This property contains the user name used for authentication.
*/

QString EnginioOAuth2Authentication::user() const
{
    Q_D(const EnginioOAuth2Authentication);
    return d->_user;
}

void EnginioOAuth2Authentication::setUser(const QString &user)
{
    Q_D(EnginioOAuth2Authentication);
    if (d->_user == user)
        return;
    d->_user = user;
    emit userChanged(user);
}

/*!
  \property EnginioOAuth2Authentication::password
  This property contains the password used for authentication.
*/

QString EnginioOAuth2Authentication::password() const
{
    Q_D(const EnginioOAuth2Authentication);
    return d->_pass;
}

void EnginioOAuth2Authentication::setPassword(const QString &password)
{
    Q_D(EnginioOAuth2Authentication);
    if (d->_pass == password)
        return;
    d->_pass = password;
    emit passwordChanged(password);
}

/*!
  \internal
*/
void EnginioOAuth2Authentication::prepareSessionToken(EnginioClientConnectionPrivate *enginio)
{
    Q_ASSERT(enginio);
    Q_ASSERT(enginio->identity());
    Q_D(EnginioOAuth2Authentication);
    d->prepareSessionToken<EnginioOAuth2AuthenticationPrivate>(enginio);
}

/*!
  \internal
*/
void EnginioOAuth2Authentication::removeSessionToken(EnginioClientConnectionPrivate *enginio)
{
    Q_ASSERT(enginio);
    Q_ASSERT(enginio->identity());
    Q_D(EnginioOAuth2Authentication);
    d->removeSessionToken<EnginioOAuth2AuthenticationPrivate>(enginio);
}

void DisconnectConnection::operator ()() const
{
    auth->cleanupConnections();
}

EnginioUserPassAuthenticationPrivate::~EnginioUserPassAuthenticationPrivate()
{
    cleanupConnections();
}

QT_END_NAMESPACE