Merge lp:~mikemc/ubuntuone-credentials/add-examplecode into lp:ubuntuone-credentials

Proposed by Mike McCracken
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 54
Merged at revision: 54
Proposed branch: lp:~mikemc/ubuntuone-credentials/add-examplecode
Merge into: lp:ubuntuone-credentials
Diff against target: 284 lines (+255/-0)
5 files modified
libubuntuoneauth/CMakeLists.txt (+1/-0)
libubuntuoneauth/examples/CMakeLists.txt (+31/-0)
libubuntuoneauth/examples/README (+26/-0)
libubuntuoneauth/examples/signing-main.cpp (+121/-0)
libubuntuoneauth/examples/signing.h (+76/-0)
To merge this branch: bzr merge lp:~mikemc/ubuntuone-credentials/add-examplecode
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) Approve
Roberto Alsina (community) Approve
Review via email: mp+183550@code.launchpad.net

Commit message

- Add minimal Qt/C++ example app and README describing how to use libubuntuoneauth to access creds and sign API requests.

Description of the change

- Add minimal Qt/C++ example app and README describing how to use libubuntuoneauth to access creds and sign API requests.

To post a comment you must log in.
Revision history for this message
Roberto Alsina (ralsina) wrote :

It looks clean and understandable to me

review: Approve
Revision history for this message
Alejandro J. Cura (alecu) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libubuntuoneauth/CMakeLists.txt'
2--- libubuntuoneauth/CMakeLists.txt 2013-08-20 15:58:32 +0000
3+++ libubuntuoneauth/CMakeLists.txt 2013-09-02 23:59:34 +0000
4@@ -49,3 +49,4 @@
5 )
6
7 add_subdirectory(tests)
8+add_subdirectory(examples)
9\ No newline at end of file
10
11=== added directory 'libubuntuoneauth/examples'
12=== added file 'libubuntuoneauth/examples/CMakeLists.txt'
13--- libubuntuoneauth/examples/CMakeLists.txt 1970-01-01 00:00:00 +0000
14+++ libubuntuoneauth/examples/CMakeLists.txt 2013-09-02 23:59:34 +0000
15@@ -0,0 +1,31 @@
16+SET (EXAMPLES_TARGET ubuntuoneauth-examples)
17+
18+SET (SIGNING_EXE "signing-example")
19+
20+# Qt5 bits
21+SET (CMAKE_INCLUDE_CURRENT_DIR ON)
22+SET (CMAKE_AUTOMOC ON)
23+find_package(Qt5Core REQUIRED)
24+
25+FILE (GLOB SIGNING_SOURCES signing*.cpp)
26+FILE (GLOB SIGNING_HEADERS signing*.h)
27+
28+add_executable (${SIGNING_EXE}
29+ ${SIGNING_SOURCES}
30+ ${SIGNING_HEADERS})
31+qt5_use_modules (${SIGNING_EXE} DBus Network)
32+
33+target_link_libraries (${SIGNING_EXE}
34+ -Wl,-rpath,${CMAKE_BINARY_DIR}/libubuntuoneauth
35+ -L${CMAKE_BINARY_DIR}/libubuntuoneauth
36+ ${AUTH_LIB_NAME})
37+
38+add_custom_target(examples-valgrind
39+ COMMAND "valgrind --tool=memcheck ${CMAKE_CURRENT_BINARY_DIR}/${SIGNING_EXE}"
40+ DEPENDS ${SIGNING_EXE}
41+)
42+
43+add_custom_target(examples-valgrind-leaks
44+ COMMAND "valgrind --tool=memcheck --track-origins=yes --num-callers=40 --leak-resolution=high --leak-check=full ${CMAKE_CURRENT_BINARY_DIR}/${SIGNING_EXE}"
45+ DEPENDS ${SIGNING_EXE}
46+)
47
48=== added file 'libubuntuoneauth/examples/README'
49--- libubuntuoneauth/examples/README 1970-01-01 00:00:00 +0000
50+++ libubuntuoneauth/examples/README 2013-09-02 23:59:34 +0000
51@@ -0,0 +1,26 @@
52+libubuntuoneauth/examples
53+
54+This directory contains example code for accessing a user's ubuntuone
55+credentials and signing URLs using them in Qt/C++ code.
56+
57+The basic flow is to use an SSOService object to get the credentials,
58+by calling getCredentials. It will respond with either
59+credentialsNotFound or credentialsFound. The credentialsFound signal
60+has a Token object that has a signURL method that takes a URL and HTTP
61+scheme, and returns a properly signed OAuth header string. This string
62+can then be used with the usual Qt network stack.
63+
64+The file signing-main.cpp has a minimal Qt application that attempts
65+to get credentials from the online accounts DB and use them to sign a
66+request for account info from the one.ubuntu.com account API.
67+
68+Run it with U1_DEBUG=1 in the environment to see the output.
69+
70+If you have valid credentials, it will print a bunch of progress info
71+followed by a JSON dict with some
72+account info.
73+
74+If you don't have valid credentials, it will simply mention that.
75+
76+To get valid credentials for testing, use the ubuntuone-music-login
77+app from the music-login directory at the top level of this project.
78
79=== added file 'libubuntuoneauth/examples/signing-main.cpp'
80--- libubuntuoneauth/examples/signing-main.cpp 1970-01-01 00:00:00 +0000
81+++ libubuntuoneauth/examples/signing-main.cpp 2013-09-02 23:59:34 +0000
82@@ -0,0 +1,121 @@
83+/*
84+ * Copyright (C) 2013 Canonical Ltd.
85+ *
86+ * This program is free software: you can redistribute it and/or modify it
87+ * under the terms of the GNU General Public License version 3, as published
88+ * by the Free Software Foundation.
89+ *
90+ * This program is distributed in the hope that it will be useful, but
91+ * WITHOUT ANY WARRANTY; without even the implied warranties of
92+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
93+ * PURPOSE. See the GNU General Public License for more details.
94+ *
95+ * You should have received a copy of the GNU General Public License along
96+ * with this program. If not, see <http://www.gnu.org/licenses/>.
97+ *
98+ * In addition, as a special exception, the copyright holders give
99+ * permission to link the code of portions of this program with the
100+ * OpenSSL library under certain conditions as described in each
101+ * individual source file, and distribute linked combinations
102+ * including the two.
103+ * You must obey the GNU General Public License in all respects
104+ * for all of the code used other than OpenSSL. If you modify
105+ * file(s) with this exception, you may extend this exception to your
106+ * version of the file(s), but you are not obligated to do so. If you
107+ * do not wish to do so, delete this exception statement from your
108+ * version. If you delete this exception statement from all source
109+ * files in the program, then also delete it here.
110+ */
111+
112+#include <QCoreApplication>
113+#include <QDebug>
114+#include <QObject>
115+#include <QString>
116+#include <QTimer>
117+
118+#include "ssoservice.h"
119+#include "token.h"
120+
121+#include "signing.h"
122+
123+namespace UbuntuOne {
124+
125+ SigningExample::SigningExample(QObject *parent) :
126+ QObject(parent)
127+ {
128+ QObject::connect(&service, SIGNAL(credentialsFound(const Token&)),
129+ this, SLOT(handleCredentialsFound(Token)));
130+ QObject::connect(&service, SIGNAL(credentialsNotFound()),
131+ this, SLOT(handleCredentialsNotFound()));
132+ QObject::connect(&nam, SIGNAL(finished(QNetworkReply*)),
133+ this, SLOT(handleNetworkFinished(QNetworkReply*)));
134+
135+ }
136+
137+ SigningExample::~SigningExample(){
138+ }
139+
140+ void SigningExample::doExample()
141+ {
142+ service.getCredentials();
143+ }
144+
145+ void SigningExample::handleCredentialsFound(Token token)
146+ {
147+ qDebug() << "Credentials found, signing url.";
148+
149+ QString url = "https://one.ubuntu.com/api/account/";
150+ QString authHeader = token.signUrl(url, QStringLiteral("GET"));
151+
152+ qDebug() << "URL Signed, authHeader is:" << authHeader;
153+
154+ QNetworkRequest req;
155+ req.setRawHeader(QStringLiteral("Authorization").toUtf8(),
156+ authHeader.toUtf8());
157+ req.setUrl(url);
158+ nam.get(req);
159+ }
160+
161+ void SigningExample::handleCredentialsNotFound()
162+ {
163+ qDebug() << "No credentials were found.";
164+ }
165+
166+ void SigningExample::handleNetworkFinished(QNetworkReply *reply)
167+ {
168+ QVariant statusAttr = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
169+ if(!statusAttr.isValid()) {
170+ qDebug() << "Invalid HTTP response.";
171+ return;
172+ }
173+
174+ int status = statusAttr.toInt();
175+ qDebug() << "HTTP Status " << status;
176+
177+ if (status != 200){
178+ qDebug() << reply->rawHeaderPairs();
179+ }
180+
181+ qDebug() << reply->readAll();
182+
183+ emit finished();
184+ }
185+
186+} // namespace UbuntuOne
187+
188+
189+int main(int argc, char *argv[])
190+{
191+
192+ QCoreApplication a(argc, argv);
193+
194+ UbuntuOne::SigningExample *example = new UbuntuOne::SigningExample(&a);
195+
196+ QObject::connect(example, SIGNAL(finished()), &a, SLOT(quit()));
197+
198+ QTimer::singleShot(0, example, SLOT(doExample()));
199+
200+ return a.exec();
201+}
202+
203+
204
205=== added file 'libubuntuoneauth/examples/signing.h'
206--- libubuntuoneauth/examples/signing.h 1970-01-01 00:00:00 +0000
207+++ libubuntuoneauth/examples/signing.h 2013-09-02 23:59:34 +0000
208@@ -0,0 +1,76 @@
209+/*
210+ * Copyright (C) 2013 Canonical Ltd.
211+ *
212+ * This program is free software: you can redistribute it and/or modify it
213+ * under the terms of the GNU General Public License version 3, as published
214+ * by the Free Software Foundation.
215+ *
216+ * This program is distributed in the hope that it will be useful, but
217+ * WITHOUT ANY WARRANTY; without even the implied warranties of
218+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
219+ * PURPOSE. See the GNU General Public License for more details.
220+ *
221+ * You should have received a copy of the GNU General Public License along
222+ * with this program. If not, see <http://www.gnu.org/licenses/>.
223+ *
224+ * In addition, as a special exception, the copyright holders give
225+ * permission to link the code of portions of this program with the
226+ * OpenSSL library under certain conditions as described in each
227+ * individual source file, and distribute linked combinations
228+ * including the two.
229+ * You must obey the GNU General Public License in all respects
230+ * for all of the code used other than OpenSSL. If you modify
231+ * file(s) with this exception, you may extend this exception to your
232+ * version of the file(s), but you are not obligated to do so. If you
233+ * do not wish to do so, delete this exception statement from your
234+ * version. If you delete this exception statement from all source
235+ * files in the program, then also delete it here.
236+ */
237+
238+#ifndef _SIGNING_H_
239+#define _SIGNING_H_
240+
241+#include <QDebug>
242+#include <QNetworkReply>
243+#include <QObject>
244+#include <QString>
245+
246+#include "ssoservice.h"
247+#include "token.h"
248+#include "requests.h"
249+#include "errormessages.h"
250+
251+namespace UbuntuOne {
252+
253+class SigningExample : public QObject
254+{
255+ Q_OBJECT
256+
257+public:
258+
259+ explicit SigningExample(QObject *parent = 0);
260+ ~SigningExample();
261+
262+public slots:
263+
264+ void doExample();
265+
266+signals:
267+
268+ void finished();
269+
270+private slots:
271+
272+ void handleCredentialsFound(Token token);
273+ void handleCredentialsNotFound();
274+ void handleNetworkFinished(QNetworkReply *reply);
275+
276+private:
277+
278+ SSOService service;
279+ QNetworkAccessManager nam;
280+
281+};
282+
283+}
284+#endif /* _SIGNING_H_ */

Subscribers

People subscribed via source and target branches

to all changes: