2 * KQOAuth - An OAuth authentication library for Qt.
4 * Author: Johan Paul (johan.paul@d-pointer.com)
5 * http://www.d-pointer.com
7 * KQOAuth is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * KQOAuth is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with KQOAuth. If not, see <http://www.gnu.org/licenses/>.
21 #include <QStringList>
24 #include "kqoauthauthreplyserver.h"
25 #include "kqoauthauthreplyserver_p.h"
27 KQOAuthAuthReplyServerPrivate::KQOAuthAuthReplyServerPrivate(KQOAuthAuthReplyServer *parent):
33 KQOAuthAuthReplyServerPrivate::~KQOAuthAuthReplyServerPrivate()
38 void KQOAuthAuthReplyServerPrivate::onIncomingConnection() {
39 Q_Q(KQOAuthAuthReplyServer);
41 socket = q->nextPendingConnection();
42 connect(socket, SIGNAL(readyRead()),
43 this, SLOT(onBytesReady()), Qt::UniqueConnection);
46 void KQOAuthAuthReplyServerPrivate::onBytesReady() {
47 Q_Q(KQOAuthAuthReplyServer);
51 content.append("<html><head><title>OAuth Finished!</title></head><body bgcolor='white' text='black'><center><h1>OAuth finished, return to the application!</h1></center></body></html>");
53 reply.append("HTTP/1.0 200 OK \r\n");
54 reply.append("Content-Type: text/html; charset=\"utf-8\"\r\n");
55 reply.append(QString("Content-Length: %1\r\n").arg(content.size()));
57 reply.append(content);
60 QByteArray data = socket->readAll();
61 QMultiMap<QString, QString> queryParams = parseQueryParams(&data);
63 socket->disconnectFromHost();
65 emit q->verificationReceived(queryParams);
68 QMultiMap<QString, QString> KQOAuthAuthReplyServerPrivate::parseQueryParams(QByteArray *data) {
69 QString splitGetLine = QString(*data).split("\r\n").first(); // Retrieve the first line with query params.
70 splitGetLine.remove("GET "); // Clean the line from GET
71 splitGetLine.remove("HTTP/1.1"); // From HTTP
72 splitGetLine.remove("\r\n"); // And from rest.
73 splitGetLine.prepend("http://localhost"); // Now, make it a URL
75 QUrl getTokenUrl(splitGetLine);
76 QList< QPair<QString, QString> > tokens = getTokenUrl.queryItems(); // Ask QUrl to do our work.
78 QMultiMap<QString, QString> queryParams;
79 QPair<QString, QString> tokenPair;
80 foreach (tokenPair, tokens) {
81 queryParams.insert(tokenPair.first.trimmed(), tokenPair.second.trimmed());
89 KQOAuthAuthReplyServer::KQOAuthAuthReplyServer(QObject *parent) :
91 d_ptr( new KQOAuthAuthReplyServerPrivate(this) )
93 Q_D(KQOAuthAuthReplyServer);
95 connect(this, SIGNAL(newConnection()),
96 d, SLOT(onIncomingConnection()));
99 KQOAuthAuthReplyServer::~KQOAuthAuthReplyServer()