Skip to content
This repository was archived by the owner on Jun 12, 2021. It is now read-only.

Commit 93bbd93

Browse files
committed
Simple persistent black list DB based on a DBM database.
1 parent 4511cd1 commit 93bbd93

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/oidcendpoint/list_db.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Persistent storage of a list of values.
3+
"""
4+
import dbm
5+
6+
7+
class ListDB:
8+
def __init__(self, filename):
9+
self.db = dbm.open(filename, "c")
10+
11+
def append(self, val):
12+
self.db[val] = "" # value is immaterial since it's never used
13+
14+
def remove(self, val):
15+
del self.db[val]
16+
17+
def __contains__(self, item):
18+
return item in self.db
19+
20+
def list(self):
21+
return list(self.db.keys())
22+
23+
def close(self):
24+
self.db.close()

tests/test_50_list_db.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
3+
from oidcendpoint.list_db import ListDB
4+
from oidcendpoint.token_handler import DefaultToken
5+
6+
7+
def full_path(local_file):
8+
_dirname = os.path.dirname(os.path.abspath(__file__))
9+
return os.path.join(_dirname, local_file)
10+
11+
12+
def test():
13+
lst = ListDB("temp")
14+
lst.append("xyz")
15+
lst.append("foo")
16+
lst.append("bar")
17+
18+
assert "foo" in lst
19+
lst.close()
20+
21+
22+
def test_blacklist_in_handler():
23+
password = "The longer the better. Is this close to enough ?"
24+
grant_expires_in = 600
25+
26+
code_handler = DefaultToken(password, typ="A", lifetime=grant_expires_in,
27+
black_list=ListDB(full_path('code_bl')))
28+
29+
code_handler.black_list('mamba')
30+
31+
assert code_handler.is_black_listed("mamba")
32+
33+
# Close the DBM database
34+
code_handler.blist.close()
35+
36+
# reopen
37+
code_handler.blist = ListDB(full_path('code_bl'))
38+
39+
# Should still be there
40+
assert code_handler.is_black_listed("mamba")

0 commit comments

Comments
 (0)