diff --git a/client/thin/src/UDSClient.py b/client/thin/src/UDSClient.py index 129a443a7..be9faaa6d 100644 --- a/client/thin/src/UDSClient.py +++ b/client/thin/src/UDSClient.py @@ -33,7 +33,7 @@ from __future__ import unicode_literals from uds import ui from uds import browser -from uds.rest import RestRequest +from uds.rest import RestRequest, RetryException from uds.forward import forward from uds import VERSION from uds.log import logger # @UnresolvedImport @@ -78,6 +78,16 @@ def approveHost(host): return approved +def getWithRetry(rest, url): + while True: + try: + res = rest.get(url) + return res + except RetryException as e: + if ui.question('Service not available', '{}\nPlease, wait a minute and press "OK" to retry, or CANCEL to abort') is True: + continue + raise Exception('Cancelled by user') + if __name__ == "__main__": logger.debug('Initializing connector') @@ -113,16 +123,18 @@ if __name__ == "__main__": # Main requests part # First, get version try: - res = rest.get('')['result'] + res = getWithRetry(rest, '') + if res['requiredVersion'] > VERSION: ui.message("New UDS Client available", "A new uds version is needed in order to access this version of UDS. A browser will be openend for this download.") webbrowser.open(res['downloadUrl']) sys.exit(1) # Now get ticket - res = rest.get('/{}/{}'.format(ticket, scrambler), params={'hostname': tools.getHostName(), 'version': VERSION}) + res = getWithRetry(rest, '/{}/{}'.format(ticket, scrambler), params={'hostname': tools.getHostName(), 'version': VERSION}) - except KeyError as e: - logger.error('Got an exception access RESULT: {}'.format(e)) except Exception as e: - logger.error('Got an unexpected exception: {}'.format(e)) + error = 'ERROR: {}'.format(e) + logger.error(error) + ui.message('Error', error) + sys.exit(2) diff --git a/client/thin/src/uds/rest.py b/client/thin/src/uds/rest.py index 14cab73dc..cd7d039e4 100644 --- a/client/thin/src/uds/rest.py +++ b/client/thin/src/uds/rest.py @@ -39,8 +39,13 @@ import json import six import urllib + from .log import logger +class RetryException(Exception): + pass + + class RestRequest(object): restApiUrl = '' @@ -54,19 +59,24 @@ class RestRequest(object): if params is not None: url += '?' + '&'.join('{}={}'.format(k, urllib.quote(six.text_type(v).encode('utf8'))) for k, v in params.iteritems()) + logger.debug('Requesting {}'.format(url)) try: - logger.debug('Requesting {}'.format(url)) r = requests.get(url, headers={'Content-type': 'application/json'}) - if r.ok: - logger.debug('Request was OK. {}'.format(r.text)) - data = json.loads(r.text) - else: - logger.error('Error requesting {}: {}, {}'.format(url, r.code. r.text)) - raise Exception('Error {}: {}'.format(r.code, r.text)) - except Exception as e: - data = { - 'result': None, - 'error': six.text_type(e) - } + except requests.exceptions.ConnectionError as e: + raise Exception('Error connecting to UDS Server at {}'.format(self.restApiUrl[0:-11])) + + if r.ok: + logger.debug('Request was OK. {}'.format(r.text)) + data = json.loads(r.text) + if not 'error' in data: + return data['result'] + # Has error + if data.get('retryable', '0') == '1': + raise RetryException(data['error']) + + raise Exception(data['error']) + else: + logger.error('Error requesting {}: {}, {}'.format(url, r.code. r.text)) + raise Exception('Error {}: {}'.format(r.code, r.text)) return data diff --git a/server/src/uds/core/util/Config.py b/server/src/uds/core/util/Config.py index 4dc581bec..865ba5e19 100644 --- a/server/src/uds/core/util/Config.py +++ b/server/src/uds/core/util/Config.py @@ -321,6 +321,9 @@ class GlobalConfig(object): # Custom message for error when limiting by calendar LIMITED_BY_CALENDAR_TEXT = Config.section(GLOBAL_SECTION).value('Calendar access denied text', '', type=Config.TEXT_FIELD) # Defaults to Nothing + # This is used so templates can change "styles" from admin interface + LOWERCASE_USERNAME = Config.section(SECURITY_SECTION).value('Convert username to lowercase', '1', type=Config.BOOLEAN_FIELD) + initDone = False @staticmethod diff --git a/server/src/uds/web/views/login.py b/server/src/uds/web/views/login.py index 844162fc3..f243896bf 100644 --- a/server/src/uds/web/views/login.py +++ b/server/src/uds/web/views/login.py @@ -50,7 +50,7 @@ import uds.web.errors as errors import logging logger = logging.getLogger(__name__) -__updated__ = '2017-04-19' +__updated__ = '2017-06-01' # Allow cross-domain login # @csrf_exempt @@ -93,6 +93,8 @@ def login(request, tag=None): except Exception: authenticator = Authenticator() userName = form.cleaned_data['user'] + if GlobalConfig.LOWERCASE_USERNAME.getBool(True) is True: + userName = userName.lower() cache = Cache('auth') cacheKey = str(authenticator.id) + userName