From 6a43276c45fef0537c261ee44b687601393809d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= Date: Tue, 9 Dec 2014 17:22:09 +0100 Subject: [PATCH] authform: add an url to test credentials post a json with post values on /mandaye/check/credentials and this request will return a json dict with a data entry OK or KO Closes #6127 --- mandaye/auth/authform.py | 54 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/mandaye/auth/authform.py b/mandaye/auth/authform.py index 642acb3..7f18f49 100644 --- a/mandaye/auth/authform.py +++ b/mandaye/auth/authform.py @@ -17,7 +17,7 @@ from mandaye import config, __version__ from mandaye.exceptions import MandayeException from mandaye.log import logger from mandaye.http import HTTPHeader, HTTPRequest -from mandaye.response import _500, _302, _401, json_response, json_error +from mandaye.response import _500, _302, _401, json_response, json_bad_request from mandaye.server import get_response from mandaye.backends.default import Association @@ -69,6 +69,13 @@ a password_field key if you want to encode a password.") def get_default_mapping(self): mapping = [ + { + 'path': r'%s$' % self.urls.get('check_credentials', '/mandaye/check/credentials'), + 'response': { + 'auth': 'check_identifiers', + 'method': 'POST', + } + }, { 'path': r'%s$' % self.urls.get('logout_url', '/mandaye/logout'), 'on_response': [{'auth': 'slo'}] @@ -433,3 +440,48 @@ a password_field key if you want to encode a password.") return True return False + def check_credentials(self, env, values, request, response): + """ + This method is designed to be called like a json webservice + The http client must make a POST with the following json : + { + "post_values": { + "foo": "bar", + "password": "monsecret", + "birthdate": "01/01/1970" + } + } + This method will response in json : + {"data": "OK|KO", err: 1|0 [,"err_msg": "blabla"]} + """ + try: + content = json.loads(request.msg.read()) + except ValueError: + return json_bad_request({ + "data": "KO", + "err": 1, + "err_msg": "invalid json" + }) + if not content.has_key('post_values'): + return json_bad_request({ + "data": "KO", + "err": 1, + "err_msg": "post_values parameter missing"}) + if not isinstance(content['post_values'], dict): + return json_bad_request({ + "data": "KO", + "err": 1, + "err_msg": "post_values parameter must be a dictionary" + }) + response = self.replay(env, content['post_values']) + if self.replay_condition and eval(self.replay_condition): + return json_response({ + "data": "OK", + "err": 0 + }) + else: + return json_response({ + "data": "KO", + "err": 0 + }) +