added some comments for DirectLogin

This commit is contained in:
hongwei1 2019-08-10 23:20:33 +02:00
parent 3f5be140b8
commit 954d60164d
3 changed files with 15 additions and 5 deletions

View File

@ -19,7 +19,10 @@ class DirectLoginAuthenticator(Authenticator):
def __init__(self, token=None):
self.token = token
def login_to_api(self, data):
# This method will call '/my/logins/direct' endpoint and get the directLogin token back, store it to self.token filed.
# the requestheaders are from the home.html form. eg:
# username="susan.uk.29@example.com",password="2b78e8", consumer_key="my5qhma1cfig5wstj5poa355onjchk0enkf3boq4"
def prepare_direct_login_token(self, requestheaders):
"""
Logs into the API and returns the token
@ -27,12 +30,16 @@ class DirectLoginAuthenticator(Authenticator):
"""
url = settings.API_HOST + settings.DIRECTLOGIN_PATH
authorization = 'DirectLogin username="{}",password="{}",consumer_key="{}"'.format( # noqa
data['username'],
data['password'],
data['consumer_key'])
requestheaders['username'],
requestheaders['password'],
requestheaders['consumer_key'])
headers = {'Authorization': authorization}
try:
# 'http://127.0.0.1:8080/my/logins/direct'
# Headers:{'Authorization': 'DirectLogin username="susan.uk.29@example.com",password="2b78e8",
# consumer_key="my5qhma1cfig5wstj5poa355onjchk0enkf3boq4"'}
# This will get the directLogin Token back.
response = requests.post(url, headers=headers)
except requests.exceptions.ConnectionError as err:
raise AuthenticatorError(Exception("OBP-API server is not running or do not response properly. "
@ -41,6 +48,8 @@ class DirectLoginAuthenticator(Authenticator):
except BaseException as err:
raise AuthenticatorError(Exception("Unknown Error. Details:"+ str(err)))
# This is the direct-Login Token:
# <class 'dict'>: {'token': 'eyJhbGciOiJIUzI1NiJ9.eyIiOiIifQ.HURJVvyGgcPcjvrfRCSbRyk1_ssjlAUk8fP0leKx8kw'}
result = response.json()
if response.status_code != 201:
raise AuthenticatorError(result['message'])

View File

@ -25,7 +25,7 @@ class DirectLoginForm(forms.Form):
cleaned_data = super(DirectLoginForm, self).clean()
authenticator = DirectLoginAuthenticator()
try:
authenticator.login_to_api(cleaned_data)
authenticator.prepare_direct_login_token(cleaned_data)
cleaned_data['authenticator'] = authenticator
except AuthenticatorError as err:
raise forms.ValidationError(err)

View File

@ -25,6 +25,7 @@ class LoginToDjangoMixin(object):
Logs the user into Django
Kind of faking it to establish if a user is authenticated later on
"""
# Here, we already get the Token for the api call.
api = API(self.request.session.get('obp'))
try:
data = api.get('/users/current')