summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/core
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2017-02-09 17:24:25 +0100
committerRuben Pollan <meskio@sindominio.net>2017-02-23 00:26:46 +0100
commit59cd23bd3e23bf2b439ad26271733a1b5c8edf68 (patch)
treef8c82c7cc5e0e2fbda93e6fc92a08852a60d1961 /src/leap/bitmask/core
parent9f2b3b55ef08d908220f0b401aeec375d1c5ea07 (diff)
[feat] eliminate the active user from bonafide
Active user is now only a concept of the cli. For it we add a ~/.config/leap/bitmaskctl.cfg file. - Resolves: #8769
Diffstat (limited to 'src/leap/bitmask/core')
-rw-r--r--src/leap/bitmask/core/configurable.py74
-rw-r--r--src/leap/bitmask/core/dispatcher.py45
2 files changed, 22 insertions, 97 deletions
diff --git a/src/leap/bitmask/core/configurable.py b/src/leap/bitmask/core/configurable.py
index f305cc3c..51a100d6 100644
--- a/src/leap/bitmask/core/configurable.py
+++ b/src/leap/bitmask/core/configurable.py
@@ -17,22 +17,13 @@
"""
Configurable Backend for Bitmask Service.
"""
-import ConfigParser
-import os
-
from twisted.application import service
-from leap.common import files
-from leap.common.config import get_path_prefix
-
-
-DEFAULT_BASEDIR = os.path.join(get_path_prefix(), 'leap')
-
-
-class MissingConfigEntry(Exception):
- """
- A required config entry was not found.
- """
+from leap.bitmask.config import (
+ Configuration,
+ DEFAULT_BASEDIR,
+ MissingConfigEntry
+)
class ConfigurableService(service.MultiService):
@@ -42,59 +33,14 @@ class ConfigurableService(service.MultiService):
def __init__(self, basedir=DEFAULT_BASEDIR):
service.MultiService.__init__(self)
-
- path = os.path.abspath(os.path.expanduser(basedir))
- if not os.path.isdir(path):
- files.mkdir_p(path)
- self.basedir = path
-
- # creates self.config
- self.read_config()
+ self.cfg = Configuration(self.config_file, basedir, DEFAULT_CONFIG)
+ self.basedir = basedir
def get_config(self, section, option, default=None, boolean=False):
- try:
- if boolean:
- return self.config.getboolean(section, option)
-
- item = self.config.get(section, option)
- return item
-
- except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
- if default is None:
- fn = self._get_config_path()
- raise MissingConfigEntry("%s is missing the [%s]%s entry"
- % fn, section, option)
- return default
+ return self.cfg.get(section, option, default=default, boolean=boolean)
def set_config(self, section, option, value):
- if not self.config.has_section(section):
- self.config.add_section(section)
- self.config.set(section, option, value)
- self.save_config()
- self.read_config()
- assert self.config.get(section, option) == value
-
- def read_config(self):
- self.config = ConfigParser.SafeConfigParser()
- bitmaskd_cfg = self._get_config_path()
-
- if not os.path.isfile(bitmaskd_cfg):
- self._create_default_config(bitmaskd_cfg)
-
- with open(bitmaskd_cfg, "rb") as f:
- self.config.readfp(f)
-
- def save_config(self):
- bitmaskd_cfg = self._get_config_path()
- with open(bitmaskd_cfg, 'wb') as f:
- self.config.write(f)
-
- def _create_default_config(self, path):
- with open(path, 'w') as outf:
- outf.write(DEFAULT_CONFIG)
-
- def _get_config_path(self):
- return os.path.join(self.basedir, self.config_file)
+ return self.cfg.set(section, option, value)
DEFAULT_CONFIG = """
@@ -106,3 +52,5 @@ web = True
onion = False
websockets = False
"""
+
+__all__ = ["ConfigurableService", DEFAULT_BASEDIR, MissingConfigEntry]
diff --git a/src/leap/bitmask/core/dispatcher.py b/src/leap/bitmask/core/dispatcher.py
index 2777d9a9..1e95ce28 100644
--- a/src/leap/bitmask/core/dispatcher.py
+++ b/src/leap/bitmask/core/dispatcher.py
@@ -174,10 +174,6 @@ class UserCmd(SubCommand):
return bonafide.do_change_password(
user, current_password, new_password)
- @register_method('str')
- def do_ACTIVE(self, bonafide, *parts):
- return bonafide.do_get_active_user()
-
class EIPCmd(SubCommand):
@@ -200,9 +196,11 @@ class EIPCmd(SubCommand):
@register_method('dict')
def do_START(self, eip, *parts):
- # TODO --- attempt to get active provider
- # TODO or catch the exception and send error
- provider = parts[2]
+ try:
+ provider = parts[2]
+ except IndexError:
+ raise DispatchError(
+ 'wrong number of arguments: expected 1, got none')
d = eip.do_start(provider)
return d
@@ -267,17 +265,15 @@ class KeysCmd(SubCommand):
@register_method("[dict]")
def do_LIST(self, service, *parts, **kw):
+ if len(parts) < 3:
+ raise ValueError("A uid is needed")
uid = parts[2]
private = False
if parts[-1] == 'private':
private = True
- d = defer.succeed(uid)
- if not uid:
- d = self._get_active_user(kw['bonafide'])
- d.addCallback(service.do_list_keys, private)
- return d
+ return service.do_list_keys(uid, private)
@register_method('dict')
def do_EXPORT(self, service, *parts, **kw):
@@ -290,14 +286,9 @@ class KeysCmd(SubCommand):
if parts[-1] == 'private':
private = True
- d = defer.succeed(uid)
- if not uid:
- d = self._get_active_user(kw['bonafide'])
- d.addCallback(service.do_export, address, private)
- return d
+ return service.do_export(uid, address, private)
@register_method('dict')
- @defer.inlineCallbacks
def do_INSERT(self, service, *parts, **kw):
if len(parts) < 6:
raise ValueError("An email address is needed")
@@ -306,14 +297,9 @@ class KeysCmd(SubCommand):
validation = parts[4]
rawkey = parts[5]
- d = defer.succeed(uid)
- if not uid:
- d = self._get_active_user(kw['bonafide'])
- d.addCallback(service.do_insert, address, rawkey, validation)
- return d
+ return service.do_insert(uid, address, rawkey, validation)
@register_method('str')
- @defer.inlineCallbacks
def do_DELETE(self, service, *parts, **kw):
if len(parts) < 4:
raise ValueError("An email address is needed")
@@ -324,16 +310,7 @@ class KeysCmd(SubCommand):
if parts[-1] == 'private':
private = True
- d = defer.succeed(uid)
- if not uid:
- d = self._get_active_user(kw['bonafide'])
- d.addCallback(service.do_delete, address, private)
- return d
-
- def _get_active_user(self, bonafide):
- d = bonafide.do_get_active_user()
- d.addCallback(lambda active: active['user'])
- return d
+ return service.do_delete(uid, address, private)
class EventsCmd(SubCommand):