]> git.mar77i.info Git - hublib/blob - hub/utils.py
serve other hubapps too, consolidate and a lot more...
[hublib] / hub / utils.py
1 from base64 import urlsafe_b64encode
2 from hashlib import pbkdf2_hmac
3 from pathlib import Path
4
5 from jinja2_simple_tags import StandaloneTag
6
7
8 class StaticTag(StandaloneTag):
9 tags = {"static"}
10
11 def render(self, filename="/", hubapp=None):
12 if not hubapp:
13 hubapp = self.context["hubapp"]
14 elif isinstance(hubapp, str):
15 hubapp = self.context["hubapp"].app.get_hubapp_by_name(hubapp)
16 return hubapp.uri_from(Path(filename))
17
18
19 def scramble(secret, value):
20 if isinstance(value, str):
21 value = value.encode()
22 if isinstance(secret, str):
23 secret = secret.encode()
24 return urlsafe_b64encode(
25 pbkdf2_hmac("sha512", value, secret, 221100)
26 ).rstrip(b"=").decode("ascii")
27
28
29 def get_redis_pass(redis_conf):
30 prefix = "requirepass "
31 with open(redis_conf, "rt") as fh:
32 for line in fh:
33 if not line.startswith(prefix):
34 continue
35 return line[len(prefix) :].rstrip()
36 return None