From c3acb3ca45480d3a4d72731ca68b69bec6db4e2c Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Wed, 9 Nov 2016 15:42:04 +0100 Subject: [refactor] remove duplication of the bitmask.js library --- src/leap/bitmask/core/_web.py | 19 +- src/leap/bitmask/core/web/README | 15 +- src/leap/bitmask/core/web/bitmask.js | 326 ----------------------------------- 3 files changed, 27 insertions(+), 333 deletions(-) delete mode 100644 src/leap/bitmask/core/web/bitmask.js diff --git a/src/leap/bitmask/core/_web.py b/src/leap/bitmask/core/_web.py index 715356d..1cbba19 100644 --- a/src/leap/bitmask/core/_web.py +++ b/src/leap/bitmask/core/_web.py @@ -32,6 +32,7 @@ from twisted.web.server import Site, NOT_DONE_YET from twisted.web.static import File from twisted.logger import Logger +from leap.bitmask.util import here from leap.bitmask.core.dispatcher import CommandDispatcher try: @@ -52,8 +53,14 @@ class HTTPDispatcherService(service.Service): """ A Dispatcher for BitmaskCore exposing a REST API. - If the leap.bitmask_js package is available in the search path, it will - serve the UI under this same service too. + + The API itself is served under the API/ route. + + If the package ``leap.bitmask_js`` is found in the import path, we'll serve + the whole JS UI in the root resource too (under the ``public`` path). + + If that package cannot be found, we'll serve just the javascript wrapper + around the REST API. """ def __init__(self, core, port=7070, debug=False, onion=False): @@ -72,14 +79,22 @@ class HTTPDispatcherService(service.Service): log.warn('bitmask_js not found, serving bitmask.core ui') webdir = os.path.abspath( pkg_resources.resource_filename('leap.bitmask.core', 'web')) + jspath = os.path.join( + here(), '..', '..', '..', + 'ui', 'app', 'lib', 'bitmask.js') + jsapi = File(os.path.abspath(jspath)) root = File(webdir) api = Api(CommandDispatcher(self._core)) root.putChild(u'API', api) + # TODO --- pass requestFactory for header authentication factory = Site(root) self.site = factory + if not HAS_WEB_UI: + root.putChild('bitmask.js', jsapi) + if self.onion: try: import txtorcon diff --git a/src/leap/bitmask/core/web/README b/src/leap/bitmask/core/web/README index 5826c52..2b99926 100644 --- a/src/leap/bitmask/core/web/README +++ b/src/leap/bitmask/core/web/README @@ -1,9 +1,14 @@ -This is the original implementation of the bitmask.js library, which uses the -REST api exposed by the HTTPRequestDispatcher. +This is a simple html based console that uses the bitmask.js library, which +uses the REST api exposed by the HTTPRequestDispatcher. This html should be +served when leap.bitmask_js package is not found on the import path from where +bitmask.core is executed. The development of bitmask_js is in the ui/ folder in this bitmask-dev repo. -A pre-compiled version of the html+js web-ui can be found in the bitmask_js package. +A pre-compiled version of the whole html+js ui can be found in the +leap.bitmask_js package. To have it served from the same endpoint than the REST +api, just install leap.bitmask_js in your environment. -This remains here to be able to develop against the REST api without the need -of installing the full-fledged bitmask_js package. +This 'web console' remains here to be able to develop against the REST api +without the need of installing the full-fledged bitmask_js package. However, +it's only going to work when running from the git repository. diff --git a/src/leap/bitmask/core/web/bitmask.js b/src/leap/bitmask/core/web/bitmask.js deleted file mode 100644 index 4a837a0..0000000 --- a/src/leap/bitmask/core/web/bitmask.js +++ /dev/null @@ -1,326 +0,0 @@ -// bitmask.js -// Copyright (C) 2016 LEAP -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -/** - * bitmask object - * - * Contains all the bitmask API mapped by sections - * - user. User management like login, creation, ... - * - mail. Email service control. - * - keys. Keyring operations. - * - events. For registering to events. - * - * Every function returns a Promise that will be triggered once the request is - * finished or will fail if there was any error. Errors are always user readable - * strings. - */ - -try { - // Use Promises in non-ES6 compliant engines. - eval('import "babel-polyfill";') -} -catch (err) {} - -var bitmask = function(){ - var event_handlers = {}; - - var api_url = '/API/'; - if (window.location.protocol === "file:") { - api_url = 'http://localhost:7070/API/'; - } - - function call(command) { - var url = api_url + command.slice(0, 2).join('/'); - var data = JSON.stringify(command.slice(2)); - - return new Promise(function(resolve, reject) { - var req = new XMLHttpRequest(); - req.open('POST', url); - - req.onload = function() { - if (req.status == 200) { - parseResponse(req.response, resolve, reject); - } - else { - reject(Error(req.statusText)); - } - }; - - req.onerror = function() { - reject(Error("Network Error")); - }; - - req.send(data); - }); - }; - - function parseResponse(raw_response, resolve, reject) { - var response = JSON.parse(raw_response); - if (response.error === null) { - resolve(response.result); - } else { - reject(response.error); - } - }; - - function event_polling() { - call(['events', 'poll']).then(function(response) { - if (response !== null) { - evnt = response[0]; - content = response[1]; - if (evnt in event_handlers) { - event_handlers[evnt](evnt, content); - } - } - event_polling(); - }, function(error) { - setTimeout(event_polling, 5000); - }); - }; - event_polling(); - - function private_str(priv) { - if (priv) { - return 'private' - } - return 'public' - }; - - return { - bonafide: { - provider: { - create: function(domain) { - return call(['bonafide', 'provider', 'create', domain]); - }, - - read: function(domain) { - return call(['bonafide', 'provider', 'read', domain]); - }, - - delete: function(domain) { - return call(['bonafide', 'provider', 'delete', domain]); - }, - - list: function(seeded) { - if (typeof seeded !== 'boolean') { - seeded = false; - } - return call(['bonafide', 'provider', 'list', seeded]); - } - }, - - /** - * uids are of the form user@provider.net - */ - user: { - /** - * Check wich user is active - * - * @return {Promise} The uid of the active user - */ - active: function() { - return call(['bonafide', 'user', 'active']); - }, - - /** - * Register a new user - * - * @param {string} uid The uid to be created - * @param {string} password The user password - * @param {boolean} autoconf If the provider should be autoconfigured if it's not allready known - * If it's not provided it will default to false - */ - create: function(uid, password, autoconf) { - if (typeof autoconf !== 'boolean') { - autoconf = false; - } - return call(['bonafide', 'user', 'create', uid, password, autoconf]); - }, - - /** - * Login - * - * @param {string} uid The uid to log in - * @param {string} password The user password - * @param {boolean} autoconf If the provider should be autoconfigured if it's not allready known - * If it's not provided it will default to false - */ - auth: function(uid, password, autoconf) { - if (typeof autoconf !== 'boolean') { - autoconf = false; - } - return call(['bonafide', 'user', 'authenticate', uid, password, autoconf]); - }, - - /** - * Logout - * - * @param {string} uid The uid to log out. - * If no uid is provided the active user will be used - */ - logout: function(uid) { - if (typeof uid !== 'string') { - uid = ""; - } - return call(['bonafide', 'user', 'logout', uid]); - }, - - /** - * List users - * - * @return {Promise} [{'userid': str, 'authenticated': boolean}] - */ - list: function() { - return call(['bonafide', 'user', 'list']); - }, - - /** - * Change password - * - * @param {string} uid The uid to log in - * @param {string} current_password The current user password - * @param {string} new_password The new user password - */ - update: function(uid, current_password, new_password) { - return call(['bonafide', 'user', 'update', uid, current_password, new_password]); - } - } - }, - - mail: { - /** - * Check the status of the email service - * - * @return {Promise} User readable status - */ - status: function() { - return call(['mail', 'status']); - }, - - /** - * Get the token of the active user. - * - * This token is used as password to authenticate in the IMAP and SMTP services. - * - * @return {Promise} The token - */ - get_token: function() { - return call(['mail', 'get_token']); - } - }, - - /** - * A KeyObject have the following attributes: - * - address {string} the email address for wich this key is active - * - fingerprint {string} the fingerprint of the key - * - length {number} the size of the key bits - * - private {bool} if the key is private - * - uids {[string]} the uids in the key - * - key_data {string} the key content - * - validation {string} the validation level which this key was found - * - expiry_date {string} date when the key expires - * - refreshed_at {string} date of the last refresh of the key - * - audited_at {string} date of the last audit (unused for now) - * - sign_used {bool} if has being used to checking signatures - * - enc_used {bool} if has being used to encrypt - */ - keys: { - /** - * List all the keys in the keyring - * - * @param {boolean} priv Should list private keys? - * If it's not provided the public ones will be listed. - * - * @return {Promise<[KeyObject]>} List of keys in the keyring - */ - list: function(priv) { - return call(['keys', 'list', private_str(priv)]); - }, - - /** - * Export key - * - * @param {string} address The email address of the key - * @param {boolean} priv Should get the private key? - * If it's not provided the public one will be fetched. - * - * @return {Promise} The key - */ - exprt: function(address, priv) { - return call(['keys', 'export', address, private_str(priv)]); - }, - - /** - * Insert key - * - * @param {string} address The email address of the key - * @param {string} rawkey The key material - * @param {string} validation The validation level of the key - * If it's not provided 'Fingerprint' level will be used. - * - * @return {Promise} The key - */ - insert: function(address, rawkey, validation) { - if (typeof validation !== 'string') { - validation = 'Fingerprint'; - } - return call(['keys', 'insert', address, validation, rawkey]); - }, - - /** - * Delete a key - * - * @param {string} address The email address of the key - * @param {boolean} priv Should get the private key? - * If it's not provided the public one will be deleted. - * - * @return {Promise} The key - */ - del: function(address, priv) { - return call(['keys', 'delete', address, private_str(priv)]); - } - }, - - events: { - /** - * Register func for an event - * - * @param {string} evnt The event to register - * @param {function} func The function that will be called on each event. - * It has to be like: function(event, content) {} - * Where content will be a list of strings. - */ - register: function(evnt, func) { - event_handlers[evnt] = func; - return call(['events', 'register', evnt]) - }, - - /** - * Unregister from an event - * - * @param {string} evnt The event to unregister - */ - unregister: function(evnt) { - delete event_handlers[evnt]; - return call(['events', 'unregister', evnt]) - } - } - }; -}(); - -try { - module.exports = bitmask -} catch(err) {} -- cgit v1.2.3