mirror of
https://github.com/OpenBankProject/API-Manager.git
synced 2026-02-06 13:46:45 +00:00
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
DirectLogin authenticator for OBP app
|
|
"""
|
|
|
|
|
|
import requests
|
|
|
|
from django.conf import settings
|
|
|
|
from .authenticator import Authenticator, AuthenticatorError
|
|
|
|
|
|
class DirectLoginAuthenticator(Authenticator):
|
|
"""Implements a DirectLogin authenticator to the API"""
|
|
|
|
token = None
|
|
|
|
def __init__(self, token=None):
|
|
self.token = token
|
|
|
|
def login_to_api(self, data):
|
|
"""
|
|
Logs into the API and returns the token
|
|
|
|
data is a dict which contains keys username, password and consumer_key
|
|
"""
|
|
url = settings.API_HOST + settings.DIRECTLOGIN_PATH
|
|
authorization = 'DirectLogin username="{}",password="{}",consumer_key="{}"'.format( # noqa
|
|
data['username'],
|
|
data['password'],
|
|
data['consumer_key'])
|
|
headers = {'Authorization': authorization}
|
|
|
|
try:
|
|
response = requests.get(url, headers=headers)
|
|
except requests.exceptions.ConnectionError as err:
|
|
raise AuthenticatorError(err)
|
|
|
|
result = response.json()
|
|
if response.status_code != 200:
|
|
raise AuthenticatorError(result['error'])
|
|
else:
|
|
self.token = result['token']
|
|
|
|
def get_session(self):
|
|
"""Returns a session object to make authenticated requests"""
|
|
headers = {'Authorization': 'DirectLogin token={}'.format(self.token)}
|
|
session = requests.Session()
|
|
session.headers.update(headers)
|
|
return session
|