From 457f3d8a168194f08cb71c6e2931058cee59c6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cornelius=20K=C3=B6lbel?= Date: Tue, 31 Jul 2018 10:53:19 +0200 Subject: [PATCH] 1123/optimize performance (#1139) * Avoid querying the database at all We can avoid querying the database for the tokentype if we have no desting serial number. We would not get a tokentype anyways! Working on #1123 * If serial is None, the tokentype is None anyway! * move logic to lib function * Failsafe if serial is None * always return a string as tokentype * Add unit tests --- privacyidea/api/before_after.py | 2 +- privacyidea/lib/token.py | 7 ++++++- tests/test_lib_token.py | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/privacyidea/api/before_after.py b/privacyidea/api/before_after.py index 32bde201be..9b8dbeaeb9 100644 --- a/privacyidea/api/before_after.py +++ b/privacyidea/api/before_after.py @@ -130,7 +130,7 @@ def before_request(): request.host # Already get some typical parameters to log serial = getParam(request.all_data, "serial") - if serial and "**" not in serial: + if serial: tokentype = get_token_type(serial) else: tokentype = None diff --git a/privacyidea/lib/token.py b/privacyidea/lib/token.py index 4c92b74a22..f640344702 100644 --- a/privacyidea/lib/token.py +++ b/privacyidea/lib/token.py @@ -446,13 +446,17 @@ def get_tokens_paginate(tokentype=None, realm=None, assigned=None, user=None, @log_with(log) def get_token_type(serial): """ - Returns the tokentype of a given serial number + Returns the tokentype of a given serial number. If the token does + not exist or can not be deterimined, an empty string is returned. :param serial: the serial number of the to be searched token :type serial: string :return: tokentype :rtype: string """ + if serial and "*" in serial: + return "" + tokenobject_list = get_tokens(serial=serial) tokentype = "" @@ -461,6 +465,7 @@ def get_token_type(serial): return tokentype + @log_with(log) def check_serial(serial): """ diff --git a/tests/test_lib_token.py b/tests/test_lib_token.py index 1b5b21e854..0dcac56855 100644 --- a/tests/test_lib_token.py +++ b/tests/test_lib_token.py @@ -177,6 +177,12 @@ def test_03_get_token_type(self): ttype = get_token_type("hotptoken") self.assertTrue(ttype == "hotp", ttype) + # test correct behavior with wildcards + self.assertEqual(get_token_type("SE1"), "totp") + self.assertEqual(get_token_type("SE*"), "") + self.assertEqual(get_token_type("*1"), "") + self.assertEqual(get_token_type("hotptoke*"), "") + def test_04_check_serial(self): r, nserial = check_serial("hotptoken") self.assertFalse(r, (r, nserial))