Merge lp:~abreu-alexandre/unity-webapps-qml/hmac-tool into lp:unity-webapps-qml

Proposed by Alexandre Abreu
Status: Merged
Approved by: Alberto Mardegan
Approved revision: 159
Merged at revision: 147
Proposed branch: lp:~abreu-alexandre/unity-webapps-qml/hmac-tool
Merge into: lp:unity-webapps-qml
Diff against target: 547 lines (+401/-6)
10 files modified
src/Ubuntu/UnityWebApps/UnityWebApps.js (+2/-2)
src/Ubuntu/UnityWebApps/UnityWebApps.pro (+5/-2)
src/Ubuntu/UnityWebApps/UnityWebApps.qml (+5/-0)
src/Ubuntu/UnityWebApps/bindings/tools/backend/tools.js (+95/-0)
src/Ubuntu/UnityWebApps/bindings/tools/client/tools.js (+106/-0)
src/Ubuntu/UnityWebApps/plugin/plugin.pro (+2/-0)
src/Ubuntu/UnityWebApps/plugin/qml-plugin.cpp (+13/-2)
src/Ubuntu/UnityWebApps/plugin/tools-api.cpp (+120/-0)
src/Ubuntu/UnityWebApps/plugin/tools-api.h (+51/-0)
src/Ubuntu/UnityWebApps/unity-webapps-api.js.in (+2/-0)
To merge this branch: bzr merge lp:~abreu-alexandre/unity-webapps-qml/hmac-tool
Reviewer Review Type Date Requested Status
Alberto Mardegan (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+251467@code.launchpad.net

Commit message

Add tools API that exposed helpers to webaps

Description of the change

Add tools API that exposed helpers to webaps

To post a comment you must log in.
147. By Alexandre Abreu

teaks

148. By Alexandre Abreu

tweaks

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
149. By Alexandre Abreu

tweak

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
150. By Alexandre Abreu

tweaks

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
151. By Alexandre Abreu

tweaks

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
152. By Alexandre Abreu

updated

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
153. By Alexandre Abreu

tweaks

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
154. By Alexandre Abreu

file

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
155. By Alexandre Abreu

tweaks

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
156. By Alexandre Abreu

improvements

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
157. By Alexandre Abreu

tweak

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alberto Mardegan (mardy) wrote :

Looks good, just some minor suggestions.

review: Needs Fixing
158. By Alexandre Abreu

tweaks

159. By Alexandre Abreu

tweaks

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alberto Mardegan (mardy) wrote :

I didn't test it, but the code looks fine.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Ubuntu/UnityWebApps/UnityWebApps.js'
2--- src/Ubuntu/UnityWebApps/UnityWebApps.js 2015-01-08 19:36:38 +0000
3+++ src/Ubuntu/UnityWebApps/UnityWebApps.js 2015-03-19 13:31:08 +0000
4@@ -115,8 +115,6 @@
5 return;
6 }
7
8- this._log ('WebApps API message received: ' + json.stringify(msg));
9-
10 var self = this;
11 var args = json.parse(msg.args);
12 args = args.map (function (arg) {
13@@ -154,6 +152,8 @@
14 return;
15 }
16
17+ this._log ('WebApps API message being dispatch: ' + apiCallName);
18+
19 this._dispatchApiCall (message.name, params);
20
21 } else if (target === UnityWebAppsUtils.UBUNTU_WEBAPPS_BINDING_OBJECT_METHOD_CALL_MESSAGE) {
22
23=== modified file 'src/Ubuntu/UnityWebApps/UnityWebApps.pro'
24--- src/Ubuntu/UnityWebApps/UnityWebApps.pro 2015-03-04 17:00:45 +0000
25+++ src/Ubuntu/UnityWebApps/UnityWebApps.pro 2015-03-19 13:31:08 +0000
26@@ -70,6 +70,9 @@
27 download_api_binding_backend_js_files.path = $$installPath/bindings/download-manager/backend/
28 download_api_binding_backend_js_files.files = ./bindings/download-manager/backend/download-api.js
29
30+tools_api_binding_backend_js_files.path = $$installPath/bindings/tools/backend/
31+tools_api_binding_backend_js_files.files = ./bindings/tools/backend/tools.js
32+
33 INSTALLS += qmldir_file \
34 qml_files \
35 js_files \
36@@ -77,5 +80,5 @@
37 alarm_binding_backend_js_files \
38 online_accounts_binding_backend_js_files \
39 runtime_api_binding_backend_js_files \
40- download_api_binding_backend_js_files
41-
42+ download_api_binding_backend_js_files \
43+ tools_api_binding_backend_js_files
44
45=== modified file 'src/Ubuntu/UnityWebApps/UnityWebApps.qml'
46--- src/Ubuntu/UnityWebApps/UnityWebApps.qml 2015-03-04 17:00:45 +0000
47+++ src/Ubuntu/UnityWebApps/UnityWebApps.qml 2015-03-19 13:31:08 +0000
48@@ -28,6 +28,7 @@
49 import "./bindings/online-accounts/backend/online-accounts.js" as OnlineAccountsApiBackend
50 import "./bindings/online-accounts/backend/online-accounts-client.js" as OnlineAccountsClientApiBackend
51 import "./bindings/download-manager/backend/download-api.js" as DownloadApiBackend
52+import "./bindings/tools/backend/tools.js" as ToolsApiBackend
53
54 /*!
55 \qmltype UnityWebApps
56@@ -502,6 +503,8 @@
57 if (settings.injectExtraContentShareCapabilities) {
58 policy.add("launchEmbeddedUI");
59 policy.add("ContentHub.onShareRequested");
60+ policy.add("ToolsApi.getHmacHash");
61+ policy.add("ToolsApi.sendHttpRequest");
62 }
63 return policy;
64 }
65@@ -796,6 +799,8 @@
66 return DownloadApiBackend.createDownloadApi(UnityBackends.backendDelegate)
67 }),
68
69+ ToolsApi: ToolsApiBackend.createToolsApi(UnityBackends.backendDelegate),
70+
71 Launcher: {
72 setCount: function (count) {
73 if (!initialized)
74
75=== added directory 'src/Ubuntu/UnityWebApps/bindings/tools'
76=== added directory 'src/Ubuntu/UnityWebApps/bindings/tools/backend'
77=== added file 'src/Ubuntu/UnityWebApps/bindings/tools/backend/tools.js'
78--- src/Ubuntu/UnityWebApps/bindings/tools/backend/tools.js 1970-01-01 00:00:00 +0000
79+++ src/Ubuntu/UnityWebApps/bindings/tools/backend/tools.js 2015-03-19 13:31:08 +0000
80@@ -0,0 +1,95 @@
81+/*
82+ * Copyright 20145 Canonical Ltd.
83+ *
84+ * This file is part of unity-webapps-qml.
85+ *
86+ * unity-webapps-qml is free software; you can redistribute it and/or modify
87+ * it under the terms of the GNU General Public License as published by
88+ * the Free Software Foundation; version 3.
89+ *
90+ * unity-webapps-qml is distributed in the hope that it will be useful,
91+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
92+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
93+ * GNU General Public License for more details.
94+ *
95+ * You should have received a copy of the GNU General Public License
96+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
97+ */
98+
99+.import Ubuntu.UnityWebApps 0.2 as UnityWebAppsBridge
100+
101+
102+/**
103+ *
104+ * Tools API backend binding
105+ *
106+ */
107+function createToolsApi(backendDelegate) {
108+ var PLUGIN_URI = 'Ubuntu.UnityWebApps';
109+ var VERSION = 0.2;
110+
111+ var toolsApiInstance = UnityWebAppsBridge.ToolsApi;
112+
113+ function isValidAlgorithm(algorithm) {
114+ var algos = ["MD5", "SHA1", "SHA256", "SHA512"]
115+ return algos.some(function(e) { return e === algorithm; })
116+ };
117+
118+ function stringToCryptoAlgorithm(algorithm) {
119+ var assoc = {
120+ "MD5": toolsApiInstance.MD5
121+ , "SHA1": toolsApiInstance.SHA1
122+ , "SHA256": toolsApiInstance.SHA256
123+ , "SHA512": toolsApiInstance.SHA512
124+ };
125+ return assoc[algorithm]
126+ };
127+
128+ return {
129+ getHmacHash: function(message, algorithm, key, callback) {
130+ if ( ! isValidAlgorithm(algorithm)) {
131+ callback({errorMsg: "Invalid algorithm",
132+ result: null});
133+ return;
134+ }
135+ callback({errorMsg: "",
136+ result: toolsApiInstance.getHmacHash(
137+ message, stringToCryptoAlgorithm(algorithm), key)});
138+ },
139+ sendHttpRequest: function(url, location, request, payload, callback) {
140+ if ( ! toolsApiInstance.areCompatibleCorsUrl(url, location)) {
141+ console.error('sendHttpRequest: incompatible CORS request urls')
142+ return;
143+ }
144+
145+ var xmlrequest = new XMLHttpRequest();
146+
147+ var verb = payload && payload.length !== 0
148+ ? "POST" : "GET"
149+
150+ xmlrequest.open(verb, url, true);
151+
152+ xmlrequest.onreadystatechange = function() {
153+ if (xmlrequest.readyState === XMLHttpRequest.DONE) {
154+ callback({
155+ errorMsg: xmlrequest.statusText,
156+ success: xmlrequest.status == 200,
157+ response: xmlrequest.responseText
158+ });
159+ }
160+ };
161+
162+ for (var header in request.headers) {
163+ if (request.headers.hasOwnProperty(header)) {
164+ xmlrequest.setRequestHeader(header, request.headers[header])
165+ }
166+ }
167+
168+ xmlrequest.setRequestHeader(
169+ "Content-Length",
170+ String(payload.length));
171+
172+ xmlrequest.send(payload);
173+ }
174+ };
175+}
176
177=== added directory 'src/Ubuntu/UnityWebApps/bindings/tools/client'
178=== added file 'src/Ubuntu/UnityWebApps/bindings/tools/client/tools.js'
179--- src/Ubuntu/UnityWebApps/bindings/tools/client/tools.js 1970-01-01 00:00:00 +0000
180+++ src/Ubuntu/UnityWebApps/bindings/tools/client/tools.js 2015-03-19 13:31:08 +0000
181@@ -0,0 +1,106 @@
182+/*
183+ * Copyright 2015 Canonical Ltd.
184+ *
185+ * This file is part of unity-webapps-qml.
186+ *
187+ * unity-webapps-qml is free software; you can redistribute it and/or modify
188+ * it under the terms of the GNU General Public License as published by
189+ * the Free Software Foundation; version 3.
190+ *
191+ * unity-webapps-qml is distributed in the hope that it will be useful,
192+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
193+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
194+ * GNU General Public License for more details.
195+ *
196+ * You should have received a copy of the GNU General Public License
197+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
198+ */
199+
200+
201+/**
202+ * Tools gives access to various helpers/tools.
203+
204+ * @module Tools
205+ */
206+function createToolsApi(backendBridge) {
207+ var PLUGIN_URI = 'Tools';
208+
209+/**
210+ * The Tools object
211+
212+ * @class Tools
213+ * @constructor
214+ * @example
215+
216+ var api = external.getUnityObject('1.0');
217+ api.Tools.getHmacHash(hmac, algorithm, key, function(result) {
218+ console.log('Application name: ' + result);
219+ });
220+ */
221+ return {
222+ /**
223+ Enumeration of the available types of CryptographicAlgorithm.
224+
225+ Values:
226+
227+ MD5: MD5 hash function
228+
229+ SHA1: SHA1 hash function
230+
231+ SHA256: SHA-256 hash function
232+
233+ SHA512: SHA-512 hash function
234+
235+ @static
236+ @property CryptographicAlgorithm {Object}
237+
238+ @example
239+
240+ var api = external.getUnityObject('1.0');
241+ var algorithm = api.Tools.CryptographicAlgorithm;
242+ // use algorithm.MD5, algorithm.SHA-1, ...
243+ */
244+ CryptographicAlgorithm: {
245+ MD5: "MD5",
246+
247+ SHA1: "SHA1",
248+
249+ SHA256: "SHA256",
250+
251+ SHA512: "SHA512"
252+ },
253+
254+ /**
255+ * Generates a .
256+ *
257+ * @method getHmacHash
258+ * @param hmac {Function (Application)}
259+ * @param algorithm {CryptographicAlgorithm}
260+ * @param key {Function (Application)}
261+ * @param callback {Function (Application)}
262+ */
263+ getHmacHash: function(message, algorithm, key, callback) {
264+ if (! callback || typeof(callback) !== 'function') {
265+ return;
266+ }
267+ backendBridge.call('ToolsApi.getHmacHash'
268+ , [message, algorithm, key]
269+ , callback);
270+ },
271+
272+ /**
273+ * @internal
274+ */
275+ __private__: {
276+ sendHttpRequest: function(url, request, payload, callback) {
277+ if (! callback || typeof(callback) !== 'function') {
278+ return;
279+ }
280+ var location = window && window.location ? window.location.href : ""
281+ backendBridge.call('ToolsApi.sendHttpRequest'
282+ , [url, location, request, payload]
283+ , callback);
284+ }
285+ }
286+ };
287+};
288
289=== modified file 'src/Ubuntu/UnityWebApps/plugin/plugin.pro'
290--- src/Ubuntu/UnityWebApps/plugin/plugin.pro 2014-07-07 18:59:19 +0000
291+++ src/Ubuntu/UnityWebApps/plugin/plugin.pro 2015-03-19 13:31:08 +0000
292@@ -41,6 +41,7 @@
293 callback.cpp \
294 abstract-item-model-adaptor.cpp \
295 application-api.cpp \
296+ tools-api.cpp \
297 application-signal-to-qt-bridge.cpp
298
299 HEADERS += \
300@@ -59,6 +60,7 @@
301 callback.h \
302 abstract-item-model-adaptor.h \
303 application-api.h \
304+ tools-api.h \
305 application-signal-to-qt-bridge.h
306
307 DEFINES += \
308
309=== modified file 'src/Ubuntu/UnityWebApps/plugin/qml-plugin.cpp'
310--- src/Ubuntu/UnityWebApps/plugin/qml-plugin.cpp 2014-07-07 18:59:19 +0000
311+++ src/Ubuntu/UnityWebApps/plugin/qml-plugin.cpp 2015-03-19 13:31:08 +0000
312@@ -30,6 +30,7 @@
313 #include "unity-webapps-app-infos.h"
314
315 #include "application-api.h"
316+#include "tools-api.h"
317 #include "abstract-item-model-adaptor.h"
318 #include "callback.h"
319
320@@ -43,6 +44,14 @@
321 return new ApplicationApi();
322 }
323
324+static QObject *createToolsApi(QQmlEngine *engine, QJSEngine *scriptEngine)
325+{
326+ Q_UNUSED(engine);
327+ Q_UNUSED(scriptEngine);
328+
329+ return new ToolsApi();
330+}
331+
332 void WebappsQmlPlugin::registerTypes(const char *uri)
333 {
334 // bindings
335@@ -60,7 +69,9 @@
336 // TODO bump version
337 qmlRegisterType<AbstractItemModelAdaptor> (uri, 0, 1, "AbstractItemModelAdaptor");
338
339- //
340+ // Application Api entry point
341 qmlRegisterSingletonType<ApplicationApi>(uri, 0, 1, "ApplicationApi", createApplicationApi);
342+
343+ // Tools Api entry point
344+ qmlRegisterSingletonType<ToolsApi>(uri, 0, 2, "ToolsApi", createToolsApi);
345 }
346-
347
348=== added file 'src/Ubuntu/UnityWebApps/plugin/tools-api.cpp'
349--- src/Ubuntu/UnityWebApps/plugin/tools-api.cpp 1970-01-01 00:00:00 +0000
350+++ src/Ubuntu/UnityWebApps/plugin/tools-api.cpp 2015-03-19 13:31:08 +0000
351@@ -0,0 +1,120 @@
352+/*
353+ * Copyright 2014 Canonical Ltd.
354+ *
355+ * This file is part of unity-webapps-qml.
356+ *
357+ * unity-webapps-qml is free software; you can redistribute it and/or modify
358+ * it under the terms of the GNU General Public License as published by
359+ * the Free Software Foundation; version 3.
360+ *
361+ * unity-webapps-qml is distributed in the hope that it will be useful,
362+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
363+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
364+ * GNU General Public License for more details.
365+ *
366+ * You should have received a copy of the GNU General Public License
367+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
368+ */
369+
370+#include "tools-api.h"
371+
372+#include <QCryptographicHash>
373+#include <QDebug>
374+#include <QMessageAuthenticationCode>
375+#include <QUrl>
376+#include <QString>
377+#include <QFileInfo>
378+#include <QFile>
379+
380+
381+namespace {
382+
383+bool enumToQtCryptoAlgorithm(
384+ ToolsApi::CryptographicAlgorithm algorithm,
385+ QCryptographicHash::Algorithm & out)
386+{
387+ switch(algorithm)
388+ {
389+ case ToolsApi::MD5:
390+ out = QCryptographicHash::Md5;
391+ return true;
392+ break;
393+ case ToolsApi::SHA1:
394+ out = QCryptographicHash::Sha1;
395+ return true;
396+ break;
397+ case ToolsApi::SHA256:
398+ out = QCryptographicHash::Sha256;
399+ return true;
400+ break;
401+ case ToolsApi::SHA512:
402+ out = QCryptographicHash::Sha512;
403+ return true;
404+ break;
405+ }
406+ return false;
407+}
408+
409+QString getSecondLevelDomain(const QUrl& url)
410+{
411+ QString tld = url.topLevelDomain();
412+ QString host = url.host().left(url.host().length() - tld.length());
413+ QStringList s = host.split(".", QString::SkipEmptyParts);
414+ return s.isEmpty() ? tld : (s.last() + tld);
415+}
416+
417+}
418+
419+
420+/**
421+ * @brief ToolsApi::ToolsApi
422+ * @param parent
423+ */
424+ToolsApi::ToolsApi(QObject *parent) :
425+ QObject(parent)
426+{}
427+
428+
429+/**
430+ * Compute a HMAC for a given message given a cryptographic key
431+ * and specific crypto algorithm.
432+ *
433+ * @brief ToolsApi::getHmacHash
434+ * @param message
435+ * @param algorithm
436+ * @param key
437+ * @return HMAC of the message
438+ */
439+QString ToolsApi::getHmacHash(
440+ const QString& message,
441+ ToolsApi::CryptographicAlgorithm algorithm,
442+ const QString& key) const
443+{
444+ QCryptographicHash::Algorithm
445+ method = QCryptographicHash::Md5;
446+ if ( ! enumToQtCryptoAlgorithm(algorithm, method))
447+ {
448+ qCritical() << "Invalid HMAC method algorithm";
449+ return QString();
450+ }
451+ QMessageAuthenticationCode
452+ code(method, key.toUtf8());
453+ code.addData(message.toUtf8());
454+ return QString::fromUtf8(code.result().toBase64());
455+}
456+
457+
458+/**
459+ * @brief ToolsApi::isCompatibleCorsRequest
460+ * @param requestUrl
461+ * @param locationUrl
462+ * @return
463+ */
464+bool ToolsApi::areCompatibleCorsUrl(
465+ const QUrl& url1,
466+ const QUrl& url2) const
467+{
468+ return url1.scheme() == url2.scheme()
469+ && url1.topLevelDomain() == url2.topLevelDomain()
470+ && getSecondLevelDomain(url1) == getSecondLevelDomain(url2);
471+}
472
473=== added file 'src/Ubuntu/UnityWebApps/plugin/tools-api.h'
474--- src/Ubuntu/UnityWebApps/plugin/tools-api.h 1970-01-01 00:00:00 +0000
475+++ src/Ubuntu/UnityWebApps/plugin/tools-api.h 2015-03-19 13:31:08 +0000
476@@ -0,0 +1,51 @@
477+/*
478+ * Copyright 2015 Canonical Ltd.
479+ *
480+ * This file is part of unity-webapps-qml.
481+ *
482+ * unity-webapps-qml is free software; you can redistribute it and/or modify
483+ * it under the terms of the GNU General Public License as published by
484+ * the Free Software Foundation; version 3.
485+ *
486+ * unity-webapps-qml is distributed in the hope that it will be useful,
487+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
488+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
489+ * GNU General Public License for more details.
490+ *
491+ * You should have received a copy of the GNU General Public License
492+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
493+ */
494+
495+#ifndef UNITY_WEBAPPS_TOOLSAPI_H
496+#define UNITY_WEBAPPS_TOOLSAPI_H
497+
498+#include <QObject>
499+
500+
501+class ToolsApi : public QObject
502+{
503+ Q_OBJECT
504+ Q_ENUMS(CryptographicAlgorithm)
505+
506+public:
507+ explicit ToolsApi(QObject *parent = 0);
508+
509+ enum CryptographicAlgorithm
510+ {
511+ MD5,
512+ SHA1,
513+ SHA256,
514+ SHA512
515+ };
516+
517+ Q_INVOKABLE QString getHmacHash(
518+ const QString &hmac,
519+ CryptographicAlgorithm algorithm,
520+ const QString& key) const;
521+
522+ Q_INVOKABLE bool areCompatibleCorsUrl(
523+ const QUrl& url1,
524+ const QUrl& url2) const;
525+};
526+
527+#endif // UNITY_WEBAPPS_TOOLSAPI_H
528
529=== modified file 'src/Ubuntu/UnityWebApps/unity-webapps-api.js.in'
530--- src/Ubuntu/UnityWebApps/unity-webapps-api.js.in 2014-09-25 19:07:53 +0000
531+++ src/Ubuntu/UnityWebApps/unity-webapps-api.js.in 2015-03-19 13:31:08 +0000
532@@ -39,6 +39,7 @@
533 //@include ./bindings/online-accounts/client/online-accounts.js
534 //@include ./bindings/runtime-api/client/runtime-api.js
535 //@include ./bindings/download-manager/client/download-api.js
536+ //@include ./bindings/tools/client/tools.js
537 //@include ./common/js/unity-backend-messaging-proxy.js
538 //@include ./common/js/unity-binding-proxy.js
539 //@include ./common/js/unity-binding-bridge.js
540@@ -294,6 +295,7 @@
541 ContentHub: createContentHubApi(backend),
542 RuntimeApi: createRuntimeApi(backend),
543 DownloadApi: createDownloadApi(backend),
544+ ToolsApi: createToolsApi(backend)
545 };
546
547 return api;

Subscribers

People subscribed via source and target branches

to all changes: