Skip to content

Commit

Permalink
1123/optimize performance (privacyidea#1139)
Browse files Browse the repository at this point in the history
* 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 privacyidea#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
  • Loading branch information
cornelinux authored and fredreichbier committed Jul 31, 2018
1 parent 83e1477 commit 457f3d8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion privacyidea/api/before_after.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion privacyidea/lib/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -461,6 +465,7 @@ def get_token_type(serial):

return tokentype


@log_with(log)
def check_serial(serial):
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/test_lib_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 457f3d8

Please sign in to comment.