From 9bc93d30ef4b963c79707a65e504c4dc2f31529d Mon Sep 17 00:00:00 2001 From: Stanislav Levin Date: Sat, 14 Jul 2018 13:18:57 +0300 Subject: [PATCH] Return the result of "password migration" procedure So far "migration" end point redirected to "error"/"invalid" page as a result of the client request. To use ajax requests and to not reload/load the whole page the response should include the result of request. Fixes: https://pagure.io/freeipa/issue/7641 Reviewed-By: Alexander Bokovoy Reviewed-By: Petr Vobornik Reviewed-By: Serhii Tsymbaliuk --- install/migration/migration.py | 43 ++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/install/migration/migration.py b/install/migration/migration.py index 9aace8c07..9b614fc9f 100644 --- a/install/migration/migration.py +++ b/install/migration/migration.py @@ -25,7 +25,6 @@ import cgi import errno import logging import os.path -from wsgiref.util import request_uri from ipaplatform.paths import paths from ipapython.dn import DN @@ -35,18 +34,21 @@ from ipalib import errors, create_api logger = logging.getLogger(os.path.basename(__file__)) +def bad_request(start_response): + """ + Return a 400 Bad Request error. + """ + status = '400 Bad Request' + response_headers = [] + response = b'' + + start_response(status, response_headers) + return [response] + def wsgi_redirect(start_response, loc): start_response('302 Found', [('Location', loc)]) return [] -def get_ui_url(environ): - full_url = request_uri(environ) - index = full_url.rfind(environ.get('SCRIPT_NAME','')) - if index == -1: - raise ValueError('Cannot strip the script URL from full URL "%s"' % full_url) - return full_url[:index] + "/ipa/ui" - - def bind(ldap_uri, base_dn, username, password): if not base_dn: logger.error('migration unable to get base dn') @@ -71,9 +73,18 @@ def application(environ, start_response): if environ.get('REQUEST_METHOD', None) != 'POST': return wsgi_redirect(start_response, 'index.html') + content_type = environ.get('CONTENT_TYPE', '').lower() + if not content_type.startswith('application/x-www-form-urlencoded'): + return bad_request(start_response) + form_data = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) if 'username' not in form_data or 'password' not in form_data: - return wsgi_redirect(start_response, 'invalid.html') + return bad_request(start_response) + + status = '200 Success' + response_headers = [] + result = 'error' + response = b'' # API object only for configuration, finalize() not needed api = create_api(mode=None) @@ -83,9 +94,11 @@ def application(environ, start_response): form_data['username'].value, form_data['password'].value) except IOError as err: if err.errno == errno.EPERM: - return wsgi_redirect(start_response, 'invalid.html') + result = 'invalid-password' if err.errno == errno.EIO: - return wsgi_redirect(start_response, 'error.html') - - ui_url = get_ui_url(environ) - return wsgi_redirect(start_response, ui_url) + result = 'migration-error' + else: + result = 'ok' + response_headers.append(('X-IPA-Migrate-Result', result)) + start_response(status, response_headers) + return [response]