Skip to content

Commit

Permalink
#135: Implement login function for python ICAT backend
Browse files Browse the repository at this point in the history
- Because this backend can use different mechanisms, I've changed the DB backend so it can use a mechanism as per the request body
- Any installed mechanism can be used, though if one isn't supplied, it defaults to simple
  • Loading branch information
MRichards99 committed Jun 19, 2020
1 parent c2e09bf commit 9755e0b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion common/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Backend(ABC):
def login(self, credentials):
"""
Attempt to log a user in using the provided credentials
:param credentials: The user's credentials
:param credentials: The user's credentials (including mechanism)
:returns: a session ID
"""
pass
Expand Down
6 changes: 6 additions & 0 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def get_db_url(self):
except:
sys.exit("Missing config value, DB_URL")

def get_icat_url(self):
try:
return self.config["ICAT_URL"]
except:
sys.exit("Missing config value, ICAT_URL")

def get_log_level(self):
try:
return self.config["log_level"]
Expand Down
4 changes: 3 additions & 1 deletion common/database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from common.exceptions import AuthenticationError
import datetime

import logging
log = logging.getLogger()

class DatabaseBackend(Backend):
"""
Expand All @@ -18,7 +20,7 @@ class DatabaseBackend(Backend):
def login(self, credentials):
if credentials["username"] == "user" and credentials["password"] == "password":
session_id = str(uuid.uuid1())
insert_row_into_table(SESSION, SESSION(ID=session_id, USERNAME="simple/root",
insert_row_into_table(SESSION, SESSION(ID=session_id, USERNAME=f"{credentials['mechanism']}/root",
EXPIREDATETIME=datetime.datetime.now() + datetime.timedelta(days=1)))
return session_id
else:
Expand Down
26 changes: 21 additions & 5 deletions common/python_icat_backend.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import logging

import icat.client
from icat.exception import ICATSessionError

from common.backend import Backend
from common.helpers import requires_session_id, queries_records
#from common.python_icat_helpers import
from common.helpers import queries_records, requires_session_id
from common.config import config
from common.exceptions import AuthenticationError
from common.models.db_models import SESSION

log = logging.getLogger()

class PythonICATBackend(Backend):
"""
Class that contains functions to access and modify data in an ICAT database directly
"""

def login(self, credentials, mnemonic):
pass
def login(self, credentials):
icat_server_url = config.get_icat_url()
client = icat.client.Client(icat_server_url, checkCert=False)
# Syntax for Python ICAT
login_details = {'username': credentials['username'], 'password': credentials['password']}

try:
session_id = client.login(credentials["mechanism"], login_details)
return session_id
except ICATSessionError:
raise AuthenticationError("User credentials are incorrect")

@requires_session_id
def get_session_details(self, session_id):
Expand Down Expand Up @@ -86,4 +103,3 @@ def get_instrument_facilitycycle_investigations_with_filters(self, session_id, i
def count_instrument_facilitycycles_investigations_with_filters(self, session_id, instrument_id, facilitycycle_id, filters):
pass
#return get_investigations_for_instrument_in_facility_cycle_count(instrument_id, facilitycycle_id, filters)

1 change: 1 addition & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"backend": "db",
"DB_URL": "mysql+pymysql://root:rootpw@localhost:13306/icatdb",
"ICAT_URL": "https://localhost.localdomain:8181",
"log_level": "WARN",
"debug_mode": false,
"generate_swagger": false,
Expand Down
3 changes: 3 additions & 0 deletions src/resources/non_entities/sessions_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def post(self):
"""
if not (request.data and "username" in request.json and "password" in request.json):
return "Bad request", 400
# If no mechanism is present in request body, default to simple
if not ("mechanism" in request.json):
request.json["mechanism"] = "simple"
try:
return {"sessionID": backend.login(request.json)}, 201
except AuthenticationError:
Expand Down

0 comments on commit 9755e0b

Please sign in to comment.