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
This commit is contained in:
Jérôme Schneider 2014-12-09 17:22:09 +01:00
parent bbbe4c5561
commit 6a43276c45
1 changed files with 53 additions and 1 deletions

View File

@ -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
})