From 6e2655e6e521c50b60b69d2f05d99cda2fc06645 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Wed, 21 Mar 2018 00:29:13 +0100 Subject: [PATCH] doorlockd.py: Add compatibility layer for old doorlockd-app This commit will be reverted once everyone has updated their app. Signed-off-by: Ralf Ramsauer --- doorlockd-new/doorlockd.py | 40 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/doorlockd-new/doorlockd.py b/doorlockd-new/doorlockd.py index aa09606..5f515e4 100755 --- a/doorlockd-new/doorlockd.py +++ b/doorlockd-new/doorlockd.py @@ -315,6 +315,42 @@ def home(): authentication_form = AuthenticationForm() response = None + # detect old API if the 'api' POST variable is set. + # NOTE: THESE BITS WILL BE REMOVED ONCE EVERYONE UPDATED THEIR APP + if request.method == 'POST' and request.form.get('api'): + log.info('Deprecated API usage detected') + user = request.form.get('user') + password = request.form.get('pass') + command = request.form.get('command') + + if any(v is None for v in [user, password, command]): + log.warning('Incomplete deprecated API request') + abort(400) + + desired_state = DoorState.Close + if command == 'unlock': + desired_state = DoorState.Open + credentials = AuthMethod.LDAP_USER_PW, user, password + + log.info('Incoming request from %s' % user.encode('utf-8')) + log.info(' desired state: %s' % desired_state) + log.info(' current state: %s' % logic.state) + log.info(' -> Knock knock, %s, please update your app!' % user) + + response = logic.request(desired_state, credentials) + if response == LogicResponse.Success: + return '0' + elif response == LogicResponse.Perm: + return '7' + elif response == LogicResponse.AlreadyLocked: + return '3' + elif response == LogicResponse.AlreadyOpen: + return '2' + elif response == LogicResponse.LDAP: + return '10' + else: + return '1' # Fail-mode + if request.method == 'POST' and authentication_form.validate(): user = authentication_form.username.data password = authentication_form.password.data @@ -327,8 +363,8 @@ def home(): response = logic.request(desired_state, credentials) log.info(' response: %s' % response) - # Don't trust python, zero credentials - user = password = credentials = None + # Don't trust python, zero credentials + user = password = credentials = None return render_template('index.html', authentication_form=authentication_form,