]> git.mxchange.org Git - fba.git/blob - fba/helpers/cookies.py
15f4fc77f1ea08295d773fc0ccc52ac8c37a7a1f
[fba.git] / fba / helpers / cookies.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17 import logging
18
19 from fba.helpers import blacklist
20 from fba.helpers import domain as domain_helper
21
22 logging.basicConfig(level=logging.INFO)
23 logger = logging.getLogger(__name__)
24
25 # Cookies storage
26 _cookies = {}
27
28 def store(domain: str, cookies: dict):
29     logger.debug("domain='%s',cookies()=%d - CALLED!", domain, len(cookies))
30     domain_helper.raise_on(domain)
31
32     if blacklist.is_blacklisted(domain):
33         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
34     elif not isinstance(cookies, dict):
35         raise ValueError(f"Parameter cookies[]='{type(cookies)}' is not of type 'dict'")
36
37     _cookies[domain] = cookies
38
39     logger.debug("EXIT!")
40
41 def get_all(domain: str) -> dict:
42     logger.debug("domain='%s' - CALLED!", domain)
43     domain_helper.raise_on(domain)
44
45     if blacklist.is_blacklisted(domain):
46         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
47
48     if domain not in _cookies:
49         return dict()
50
51     logger.debug("_cookies[%s]()=%d - EXIT!", domain, len(_cookies[domain]))
52     return _cookies[domain]
53
54 def has(domain: str) -> bool:
55     logger.debug("domain='%s' - CALLED!", domain)
56     domain_helper.raise_on(domain)
57
58     if blacklist.is_blacklisted(domain):
59         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
60
61     has_cookies = domain in _cookies
62
63     logger.debug("has_cookies='%s' - EXIT!", has_cookies)
64     return has_cookies
65
66 def clear(domain: str):
67     logger.debug("domain='%s' - CALLED!", domain)
68     domain_helper.raise_on(domain)
69
70     if blacklist.is_blacklisted(domain):
71         raise ValueError(f"domain='{domain}' is blacklisted but function was invoked")
72
73     if has(domain):
74         logger.debug("Removing cookies for domain='%s' ...", domain)
75         del _cookies[domain]
76
77     logger.debug("EXIT!")